Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

# This file is part of daf_base 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org/). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

 

"""Test dict emulation""" 

 

import unittest 

import copy 

 

import lsst.daf.base 

 

 

class DictTestCase(unittest.TestCase): 

 

def setUp(self): 

 

pl = lsst.daf.base.PropertyList() 

pl.setBool("bool", True) 

pl.setShort("short", 42) 

pl.setInt("int", 2008) 

pl.setLongLong("int64_t", 0xfeeddeadbeef) 

pl.setFloat("float", 3.14159) 

pl.setDouble("double", 2.718281828459045) 

pl.set("char*", "foo", "char comment") 

pl.setString("string", "bar") 

pl.set("int2", 2009, "int comment") 

pl.set("dt", lsst.daf.base.DateTime("20090402T072639.314159265Z", lsst.daf.base.DateTime.UTC)) 

pl.set("undef", None) 

 

ps = lsst.daf.base.PropertySet() 

ps.setBool("bool", True) 

ps.setShort("short", 42) 

ps.setInt("int", 2008) 

ps.setLongLong("long", 5) # Deliberately small 

ps.setLongLong("int64_t", 0xfeeddeadbeef) 

ps.setFloat("float", 3.14159) 

ps.setDouble("double", 2.718281828459045) 

ps.set("char*", "foo") 

ps.setString("string", "bar") 

ps.set("char*", u"foo") 

ps.setString("string", u"bar") 

ps.set("int2", 2009) 

ps.set("dt", lsst.daf.base.DateTime("20090402T072639.314159265Z", lsst.daf.base.DateTime.UTC)) 

ps.set("blank", "") 

ps.addInt("int", 2009) 

ps.set("undef", None) 

 

ps2 = lsst.daf.base.PropertySet() 

ps2.setBool("bool2", False) 

ps2.setShort("short2", 16) 

ps2.setInt("int2", 2018) 

 

ps.setPropertySet("ps2", ps2) 

pl.setPropertySet("ps2", ps2) 

 

self.ps = ps 

self.pl = pl 

 

def testShallowCopyPropertySet(self): 

shallow = copy.copy(self.ps) 

self.assertIsInstance(shallow, lsst.daf.base.PropertySet) 

self.assertIn("ps2", shallow) 

self.assertEqual(shallow, self.ps) 

 

# Modifying the attached property set should change both views 

ps2 = self.ps.getScalar("ps2") 

ps2s = shallow.getScalar("ps2") 

self.assertEqual(ps2, ps2s) 

 

ps2["int2"] = 2017 

self.assertEqual(ps2, ps2s) 

 

def testDeepCopyPropertySet(self): 

deep = copy.deepcopy(self.ps) 

self.assertIsInstance(deep, lsst.daf.base.PropertySet) 

self.assertEqual(deep, self.ps) 

 

# Modifying the contents of ps2 should not affect the original 

ps2 = self.ps.getScalar("ps2") 

ps2d = deep.getScalar("ps2") 

self.assertEqual(ps2, ps2d) 

 

ps2["int2"] = 2017 

self.assertNotEqual(ps2, ps2d) 

 

def testDictPropertySet(self): 

container = self.ps 

self.assertIn("string", container) 

self.assertIn("ps2", container) 

self.assertNotIn("ps2.bool2", container) 

self.assertIn("bool2", container.getScalar("ps2")) 

 

# Compare dict-like interface to pure dict version 

d = container.toDict() 

self.assertEqual(len(d), len(container)) 

self.assertIsNone(d["undef"]) 

 

# Set some values 

container["new"] = "string" 

container["array"] = [1, 2, 3] 

container["dot.delimited"] = "delimited" 

self.assertIn("dot", container) 

 

keys = container.keys() 

self.assertEqual(len(keys), 17) 

for k in keys: 

self.assertIn(k, container) 

 

for k, v in container.items(): 

self.assertIn(k, container) 

self.assertEqual(v, container[k]) 

 

# Check that string form of the non-PropertySet values are present 

# when iterating over values. This is a simple test to ensure 

# that values() does do something useful 

values = {str(container[k]) for k in container if not isinstance(container[k], 

lsst.daf.base.PropertySet)} 

for v in container.values(): 

if not isinstance(v, lsst.daf.base.PropertySet): 

self.assertIn(str(v), values) 

 

# Assign a PropertySet 

ps2 = lsst.daf.base.PropertySet() 

ps2.setString("newstring", "stringValue") 

container["newps2"] = ps2 

ps2["newint"] = 5 

self.assertEqual(container.getScalar("newps2.newint"), ps2.getScalar("newint")) 

 

ps2["undef2"] = None 

self.assertIn("undef2", ps2) 

 

# Dict should be converted to a PropertySet 

container["dict"] = {"a": 1, "b": 2} 

self.assertEqual(container.getScalar("dict.b"), 2) 

 

container["a_property_list"] = self.pl 

 

def testDictPropertyList(self): 

container = self.pl 

self.assertIn("string", container) 

self.assertIn("ps2.bool2", container) 

 

# Set a comment 

container["int2#COMMENT"] = "new comment" 

self.assertEqual(container.getComment("int2"), "new comment") 

 

# Compare dict-like interface to pure dict version 

d = container.toDict() 

self.assertEqual(len(d), len(container)) 

self.assertIsNone(d["undef"]) 

 

# Set some values 

container["new"] = "string" 

container["array"] = [1, 2, 3] 

container["dot.delimited"] = "delimited" 

 

keys = container.keys() 

self.assertEqual(len(keys), 17) 

for k in keys: 

self.assertIn(k, container) 

 

for k, v in container.items(): 

self.assertIn(k, container) 

self.assertEqual(v, container[k]) 

 

# Assign a PropertySet 

ps2 = lsst.daf.base.PropertySet() 

ps2["newstring"] = "stringValue" 

container["newps2"] = ps2 

ps2["newint"] = 5 # This should have no effect on container 

self.assertEqual(container.getScalar("newps2.newstring"), ps2.getScalar("newstring")) 

self.assertNotIn("newps2.newinst", container) 

self.assertIn("newint", ps2) 

 

ps2["undef2"] = None 

self.assertIn("undef2", ps2) 

 

# Dict should be converted to a PropertySet 

container["dict"] = {"a": 1, "b": 2} 

self.assertEqual(container.getScalar("dict.b"), 2) 

 

def testCopyPropertyList(self): 

# For PropertyList shallow copy and deep copy are identical 

shallow = copy.copy(self.pl) 

self.assertIsInstance(shallow, lsst.daf.base.PropertyList) 

self.assertIn("dt", shallow) 

self.assertIn("int", shallow) 

self.assertEqual(shallow, self.pl) 

del shallow["dt"] 

self.assertNotIn("dt", shallow) 

self.assertIn("dt", self.pl) 

 

deep = copy.deepcopy(self.pl) 

self.assertIsInstance(deep, lsst.daf.base.PropertyList) 

self.assertEqual(deep, self.pl) 

del deep["dt"] 

self.assertNotIn("dt", deep) 

self.assertIn("dt", self.pl) 

 

 

218 ↛ 219line 218 didn't jump to line 219, because the condition on line 218 was never trueif __name__ == '__main__': 

unittest.main()