OrderedNamedTypedDict

class ipkiss3.all.OrderedNamedTypedDict

Dict with named items of a given type.

The name of the item becomes the key into the dictionary. Items can be retrieved with [] both by name and by int key Behaves partially as a list in that it supports the append() and extend() methods, as well as the += operator. The items are however not in a particular order

Enforces unique names/keys. When comparing two dictionaries, order is taken into account.

Can optionally observe name changes of its item’s names, changing the keys in the dictionary when a name of an item changes.

Notes

To make an OrderedNamedTypedDict that observes changes to its items’s names, inherit from the class and set the class attribute _observe_name_changes=True.

Examples

class MyNamedType(object):
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return "I am {0}".format(self.name)

class MyOrderedNamedDict(OrderedNamedTypedDict):
    _item_type = MyNamedType

my_list = MyOrderedNamedDict()
my_list.append(MyNamedType(name="one"))
my_list += MyNamedType(name="two")
my_list.extend([MyNamedType(name="three"),
                MyNamedType(name="four")])

print(my_list["one"])
print(my_list[0])