mirror of https://github.com/flaggo/pydu.git
add doc for dict.OrderedDict
This commit is contained in:
parent
d5cb5835c7
commit
f420fae4e3
|
@ -52,6 +52,27 @@ Dict
|
||||||
>>> d['key']
|
>>> d['key']
|
||||||
1
|
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)
|
.. py:function:: pydu.dict.attrify(obj)
|
||||||
|
|
||||||
|
|
11
pydu/dict.py
11
pydu/dict.py
|
@ -133,6 +133,17 @@ class LookupDict(dict):
|
||||||
|
|
||||||
# https://stackoverflow.com/questions/6190331/can-i-do-an-ordered-default-dict-in-python
|
# https://stackoverflow.com/questions/6190331/can-i-do-an-ordered-default-dict-in-python
|
||||||
class OrderedDefaultDict(collections.OrderedDict):
|
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):
|
def __init__(self, default_factory=None, *args, **kwds):
|
||||||
if (default_factory is not None and
|
if (default_factory is not None and
|
||||||
not isinstance(default_factory, collections.Callable)):
|
not isinstance(default_factory, collections.Callable)):
|
||||||
|
|
Loading…
Reference in New Issue