Skip to content

Commit

Permalink
Add toOrderedDict method to PropertyList.
Browse files Browse the repository at this point in the history
Method returns a (possibly nested) ordered dictionary of properties.
All propertary values are converted to their corresponding Python types.
  • Loading branch information
Pim Schellart authored and Pim Schellart committed May 6, 2016
1 parent 1d97412 commit e258346
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python/lsst/daf/base/baseLib.i
Original file line number Diff line number Diff line change
Expand Up @@ -322,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

0 comments on commit e258346

Please sign in to comment.