arbor icon indicating copy to clipboard operation
arbor copied to clipboard

`label_dict` does not behave like a `dict`

Open Helveg opened this issue 3 years ago • 2 comments

  • 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 the dict constructor (e.g. doesn't know what to do with a zip object, it does not even accept another label_dict as arg)
  • Converting a label_dict to a dict 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))) {}

Helveg avatar May 19 '21 13:05 Helveg

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?

thorstenhater avatar Oct 13 '21 07:10 thorstenhater

  • 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 incomplete dict 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.

Helveg avatar Oct 13 '21 07:10 Helveg