add doc for dict.OrderedDict

This commit is contained in:
Prodesire 2018-01-02 22:56:00 +08:00
parent d5cb5835c7
commit f420fae4e3
2 changed files with 59 additions and 27 deletions

View File

@ -52,6 +52,27 @@ Dict
>>> d['key']
1
.. py:class:: pydu.dict.OrderedDefaultDict(default_factory=None, *args, **kwds)
Dictionary that remembers insertion order and has default value
with default factory.
The default factory is called without arguments to produce
a new value when a key is not present, in ``__getitem__`` only.
An ``OrderedDefaultDict`` compares equal to a ``collections.defaultdict``
with the same items. All remaining arguments are treated the same
as if they were passed to the ``defaultdict`` constructor,
including keyword arguments.
>>> from pydu.dict import OrderedDefaultDict
>>> d = OrderedDefaultDict(int)
>>> d['b']
0
>>> d['a']
0
>>> d.keys()
odict_keys(['b', 'a'])
.. py:function:: pydu.dict.attrify(obj)

View File

@ -133,6 +133,17 @@ class LookupDict(dict):
# https://stackoverflow.com/questions/6190331/can-i-do-an-ordered-default-dict-in-python
class OrderedDefaultDict(collections.OrderedDict):
"""
Dictionary that remembers insertion order and has default value
with default factory.
The default factory is called without arguments to produce
a new value when a key is not present, in `__getitem__` only.
An `OrderedDefaultDict` compares equal to a `collections.defaultdict`
with the same items. All remaining arguments are treated the same
as if they were passed to the `defaultdict` constructor,
including keyword arguments.
"""
def __init__(self, default_factory=None, *args, **kwds):
if (default_factory is not None and
not isinstance(default_factory, collections.Callable)):