Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tickets/dm 5643 #3

Merged
merged 2 commits into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions python/lsst/daf/base/baseLib.i
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,28 @@ def _PS_setValue(self, name, value):
def _PS_addValue(self, name, value):
return _propertyContainerAdd(self, name, value, _PS_typeMenu)

def _PS_toDict(self):
"""Returns a (possibly nested) dictionary with all properties.
"""

d = {}
for name in self.names():
v = self.get(name)

if isinstance(v, PropertySet):
d[name] = PropertySet.toDict(v)
else:
d[name] = v
return d

PropertySet.get = _PS_getValue
PropertySet.set = _PS_setValue
PropertySet.add = _PS_addValue
PropertySet.toDict = _PS_toDict
del _PS_getValue
del _PS_setValue
del _PS_addValue
del _PS_toDict
%}

// This has to come after PropertyList.h
Expand Down Expand Up @@ -306,8 +322,21 @@ def _PL_toList(self):
ret.append((name, self.get(name), self.getComment(name)))
return ret

def _PL_toOrderedDict(self):
"""Return an ordered dictionary with all properties in the order that
they were inserted.
"""
from collections import OrderedDict

d = OrderedDict()
for name in self.getOrderedNames():
d[name] = self.get(name)
return d

PropertyList.toList = _PL_toList
PropertyList.toOrderedDict = _PL_toOrderedDict
del _PL_toList
del _PL_toOrderedDict
%}


38 changes: 38 additions & 0 deletions tests/PropertyList.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,44 @@ def testOrder(self):

self.checkPickle(apl)

def testToOrderedDict(self):
from collections import OrderedDict

apl = dafBase.PropertyList()
apl.set("SIMPLE", True)
apl.set("BITPIX", -32)
apl.set("NAXIS", 2)
apl.set("RA", 3.14159)
apl.set("DEC", 2.71828)
apl.set("COMMENT", "This is a test")
apl.add("COMMENT", "This is a test line 2")
correct = OrderedDict([
("SIMPLE", True),
("BITPIX", -32),
("NAXIS", 2),
("RA", 3.14159),
("DEC", 2.71828),
("COMMENT", ("This is a test", "This is a test line 2"))
])
self.assertEqual(apl.toOrderedDict(), correct)

apl.set("NAXIS1", 513)
correct["NAXIS1"] = 513
self.assertEqual(apl.toOrderedDict(), correct)
apl.set("RA", 1.414, inPlace=False)
del correct["RA"]
correct["RA"] = 1.414
self.assertEqual(apl.toOrderedDict(), correct)
apl.set("DEC", 1.732)
correct["DEC"] = 1.732
self.assertEqual(apl.toOrderedDict(), correct)
apl.set("DEC", -6.28)
correct["DEC"] = -6.28
self.assertEqual(apl.toOrderedDict(), correct)
apl.add("COMMENT", "This is a test line 3")
correct["COMMENT"] = correct["COMMENT"] + ("This is a test line 3", )
self.assertEqual(apl.toOrderedDict(), correct)

def testHierarchy(self):
apl = dafBase.PropertyList()
apl.set("CURRENT", 49.5)
Expand Down
46 changes: 46 additions & 0 deletions tests/PropertySet_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,52 @@ def testSubPS(self):
self.assertEqual(ps.get("b.c"), 20)
self.assertEqual(ps.exists("b"), False)

def testToDict(self):
ps = dafBase.PropertySet()
ps.setBool("bool", True)
ps.setShort("short", 42)
ps.setInt("int", 2008)
ps.setLongLong("int64_t", 0xfeeddeadbeefL)
ps.setInt("ints", (10, 9, 8))

ps2 = dafBase.PropertySet()
ps2.set("ps", ps)
ps2.setFloat("float", 3.14159)
ps2.setDouble("double", 2.718281828459045)
ps2.set("char*", "foo")
ps2.setString("string", "bar")
ps2.set("dt", dafBase.DateTime("20090402T072639.314159265Z"))

d = ps2.toDict()
self.assertIsInstance(d, dict)

self.assertIsInstance(d["float"], float)
self.assertAlmostEqual(d["float"], 3.14159, 6)
self.assertIsInstance(d["double"], float)
self.assertEqual(d["double"], 2.718281828459045)

self.assertIsInstance(d["char*"], str)
self.assertEqual(d["char*"], "foo")
self.assertIsInstance(d["string"], str)
self.assertEqual(d["string"], "bar")
self.assertIsInstance(d["dt"], dafBase.DateTime)
self.assertEqual(d["dt"].nsecs(), 1238657233314159265L)

d2 = d["ps"]
self.assertIsInstance(d2, dict)

self.assertIsInstance(d2["bool"], bool)
self.assertEqual(d2["bool"], True)
self.assertIsInstance(d2["short"], long)
self.assertEqual(d2["short"], 42)
self.assertIsInstance(d2["int"], int)
self.assertEqual(d2["int"], 2008)
self.assertIsInstance(d2["int64_t"], long)
self.assertEqual(d2["int64_t"], 0xfeeddeadbeefL)
self.assertIsInstance(d2["ints"], tuple)
self.assertIsInstance(d2["ints"][0], int)
self.assertEqual(d2["ints"], (10, 9, 8))

def suite():
"""Returns a suite containing all the test cases in this module."""

Expand Down