MCPcopy Create free account
hub / github.com/grantjenks/python-sortedcontainers / SortedDictSub

Class SortedDictSub

tests/issue_147.py:42–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40from functools import partial
41
42class SortedDictSub(SortedDict):
43
44 def __init__(self, *args, **kwargs):
45 """Initialize sorted dict instance.
46 Optional key-function argument defines a callable that, like the `key`
47 argument to the built-in `sorted` function, extracts a comparison key
48 from each dictionary key. If no function is specified, the default
49 compares the dictionary keys directly. The key-function argument must
50 be provided as a positional argument and must come before all other
51 arguments.
52 Optional iterable argument provides an initial sequence of pairs to
53 initialize the sorted dict. Each pair in the sequence defines the key
54 and corresponding value. If a key is seen more than once, the last
55 value associated with it is stored in the new sorted dict.
56 Optional mapping argument provides an initial mapping of items to
57 initialize the sorted dict.
58 If keyword arguments are given, the keywords themselves, with their
59 associated values, are added as items to the dictionary. If a key is
60 specified both in the positional argument and as a keyword argument,
61 the value associated with the keyword is stored in the
62 sorted dict.
63 Sorted dict keys must be hashable, per the requirement for Python's
64 dictionaries. Keys (or the result of the key-function) must also be
65 comparable, per the requirement for sorted lists.
66 >>> d = {'alpha': 1, 'beta': 2}
67 >>> SortedDict([('alpha', 1), ('beta', 2)]) == d
68 True
69 >>> SortedDict({'alpha': 1, 'beta': 2}) == d
70 True
71 >>> SortedDict(alpha=1, beta=2) == d
72 True
73 """
74 if args and (args[0] is None or callable(args[0])):
75 _key = self._key = args[0]
76 args = args[1:]
77 else:
78 _key = self._key = None
79
80 self._list = SortedList(key=_key)
81
82 # Calls to super() are expensive so cache references to dict methods on
83 # sorted dict instances.
84
85 _dict = super(SortedDict, self)
86 # self._dict_clear = _dict.clear
87 # self._dict_delitem = _dict.__delitem__
88 self._dict_iter = partial(dict.__iter__, self) # _dict.__iter__
89 # self._dict_pop = _dict.pop
90 # self._dict_setitem = _dict.__setitem__
91 self._dict_update = partial(dict.update, self) # _dict.update
92
93 # Reaching through ``self._list`` repeatedly adds unnecessary overhead
94 # so cache references to sorted list methods.
95
96 _list = self._list
97 self._list_add = _list.add
98 self._list_clear = _list.clear
99 self._list_iter = _list.__iter__

Callers 1

issue_147.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected