In programming, certain dictionary-based data structures are very useful and important. For example:
Revision dictionary: Revision dictionary is a sub-type of dictionary that keeps keys sorted in the order of last modification. It is commonly used in cache handling which remembers most recently used items.
Default dictionary: Default dictionary can return a default element when the dictionary is accessed with a key that is not present in the dictionary. It already has an implementation in
collections.defaultdict.Infinite dictionary: An infinite dictionary can be accessed with infinitely layer of nested recursion, i.e., infinite chaining of keys, e.g.,
dct[person_name][gender], dct[company_name][employees], it can be used to create dynamic data structures, or even modeling file-system structures.
So my question is that: in Python, is it possible to write a dictionary that has all the 3 features, i.e., an infinite default revision dictionary? In particular, one can specify options during creation, e.g., whether the items should be sorted in the order of insertion, or keys' order, or revision order. And how to implement if possible?
It would be very cool if future-version Python has a built-in Dictionary class that supports all of these features and options.
Edits: Point 2 might not be in contradiction with Point 3, that in principle, an InfiniteDefaultRevisionDictionary can have a default key other than lambda of itself. For example, if the default key is 0, then:
dd=InfiniteDefaultRevisionDictionary(default=0, {})
print(dd['a']['b']['c']) # should give 0
dd['b']['c'][2] = [1, '2', 3.5] # should work fine
Thanks to all who have replied! Combining suggestions from various people as well as my own research, below is the most I can do.
It cannot set the default lambda other than
InfiniteDefaultRevisionDict. However, you can serialize it to JSON and de-serialize it from JSON in the form of a standard Pythondict. Hope someone can come up with a better solution that can satisfy more requirements.