arbor
arbor copied to clipboard
`label_dict` does not behave like a `dict`
- Misses parts of the
dict
interface:-
fromkeys
-
items
-
keys
-
values
-
pop
-
popitem
-
get
-
setdefault
-
update
-
clear
-
__reversed__
-
__contains__
-
- The
arbor.label_dict
function does not accept the same arguments as thedict
constructor (e.g. doesn't know what to do with azip
object, it does not even accept anotherlabel_dict
as arg) - Converting a
label_dict
to adict
yields an empty dictionary, or an error:
>>> labels = arbor.label_dict({'midpoint': '(location 0 0.5)'})
>>> print(labels, dict(labels))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 8; 2 is required
(label_dict (region "apic" (tag 4)) (region "dend" (tag 3)) (region "axon" (tag 2)) (region "soma" (tag 1))) {}
Implemented in #1711
- items, keys, values
- contains
- update
- conversion from iterator of kv pairs
- copy from other label_dict
As label_dict is essentially frozen, we cannot implement
- pop, popitem
- clear
These remain open/untested, but I am less sure we need all of them
- get: covered by
[]
? - setdefault: Is this required/needed?
- reversed: As this is essentially an unordered container, has this value?
- conversion to dict: Should be ok by converting to iterator
@Helveg does this fulfill your needs?
-
get
accepts a default argument that simplifies control flow:
val = labels.get("key", "(nil)")
try:
val = labels["key"]
except KeyError:
val = "(nil)"
- Same for
setdefault
- I don't see any need for
reversed
- Would
dict(label_dict)
work? With an incompletedict
API it can't be duck typed everywhere so the conversion might occur.
Why not implement __delitem__
? Imagine wanting to strip a certain set of keys from a factory label dict. I don't see a specific use case for pop(item)
since it is unordered, but del
certainly.