We would like to implement a dict-like interface for PropertySet and PropertyList.
I have some uncertainty over what I should implement, so I would really appreciate feedback on this issue here (before possibly going to RFC).
In particular I’d like feedback from @ktl, @RHL, @jbosch, @price, and @rowen.
If people want to look at code I have a prototype implementation in DM-15676.
At minimum this would include definitions for __iter__ (iterate through all the top level keys, also a keys() method), __len__ (how many toplevel keys are there), __delitem__ (delete a key), __eq__ (compare two PropertySets), __setitem__ (assign a new value by inferring the top if needed, and converting a dict to a PropertySet). I believe these are fairly straight forward and non-controversial.
Following from the discussion in RFC-434, it is not clear to me what __getitem__ should do (and correspondingly what get() and setdefault() should do (we do need to change get() to take a default)) since we have already decided that the return value should not dynamically change from scalar to list.
- Should
PropertySetalways return a list? (it really is a vector internally). The more Pythonic approach would seem to be to change the return type from list to scalar as needed. - Should
PropertyListalways return a scalar? (we shouldn’t be duplicating FITS headers).
The problem with these approaches is that it’s not symmetric. If you assign 5 to an element in the PropertySet you will get [5] back.
It may be that returning scalar/list as needed will work in a purely dict-like interface.
Confusion begins if people mix standard PropertySet API with dict accessors (ie if they use add()).
For PropertyList assigning a dict/PropertySet is also not symmetric because you get a flattened list out and can never retrieve the hierarchy. However, if necessary we could emulate this by retrieving all the keys with that root and returning a new PropertySet (which would be detached from the original PropertyList).
For update() should it always run combine (possibly converting a dict argument to a PropertySet) or should it work more like dict.update() and assign each item one at a time (so over-writing existing values)? add() would not be part of the dict-like interface.
To match FITS headers from Astropy it seems like PropertyList.__getitem__ should always be able to use an integer index into the ordered list.