def __init__(self, items=None, key_type=None, alg=RED_BLACK_TREE, key=None, compare=None, updator=None): """ :param items: Sequence or mapping of initial items. :param key_type: Optional keys' type, or ``None`` to show it is unknown or undefined (specifying the key type can greatly improve performance). :type key_type: ``int``, ``float``, ``str``, ``bytes``, ``unicode`` (for pre Py3) , ``(int, int)``, ``(float, float)``, or ``None`` :type items: iterable or ``None`` :param string alg: Underlying algorithm string. Should be one of RED_BLACK_TREE, SPLAY_TREE, or SORTED_LIST :param function key: Key function: transforms the set's keys into something that can be compared. :param compare: Comparison function. Should take two parameters, say x and y, and return the a negative number, 0, or positive number, for the cases x < y, x == y, and x > y, respectively. :type compare: Function or ``None`` :param updator: Node updator :type updator: Function or ``None`` .. Note:: The compare fuction is deprecated in favor of the key function. Example: >>> # (Sorted-list) frozen sorted dict with initial items >>> t = FrozenSortedDict([(1, 'a'), (2, 'b')]) >>> list(t) [1, 2] >>> assert 1 in t >>> assert 4 not in t >>> t[130] = 'koko' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'FrozenSortedDict' object does not support item assignment Key-Type Example: >>> # Almost no change! >>> t = FrozenSortedDict([(1, 'a'), (2, 'b')], key_type = int) >>> # Identical from here. """ try: items = items.items() except AttributeError: pass self._init_info = _CommonInitInfo(key_type, alg, key, compare, updator) metadata = _updator_metadata(0, self._init_info) _adopt_updator_methods(self, updator) FrozenDictTree.__init__(self, alg, items, key_type, 0, metadata, key, compare, 0)
def root(self): """ :returns: The root :py:class:`Node` of the dict. """ c_node = FrozenDictTree.root(self) return Node(c_node) if c_node is not None else None
def get(self, key, default=None): """ :param key: Key :param default: default value :returns: default if key is not in the dict, otherwise value mapped by key. Examples: >>> t = SortedDict([(2, 'b'), (3, 'c')]) >>> assert t.get(2, 'a') == 'b' >>> assert t.get(0, 'a') == 'a' """ try: return FrozenDictTree.__getitem__(self, key) except KeyError: return default
def get(self, key, default = None): """ :param key: Key :param default: default value :returns: default if key is not in the dict, otherwise value mapped by key. Examples: >>> t = SortedDict([(2, 'b'), (3, 'c')]) >>> assert t.get(2, 'a') == 'b' >>> assert t.get(0, 'a') == 'a' """ try: return FrozenDictTree.__getitem__(self, key) except KeyError: return default
def __init__( self, items = None, key_type = None, alg = RED_BLACK_TREE, key = None, compare = None, updator = None): """ :param items: Sequence or mapping of initial items. :param key_type: Optional keys' type, or ``None`` to show it is unknown or undefined (specifying the key type can greatly improve performance). :type key_type: ``int``, ``float``, ``str``, ``bytes``, ``unicode`` (for pre Py3) , ``(int, int)``, ``(float, float)``, or ``None`` :type items: iterable or ``None`` :param string alg: Underlying algorithm string. Should be one of RED_BLACK_TREE, SPLAY_TREE, or SORTED_LIST :param function key: Key function: transforms the set's keys into something that can be compared. :param compare: Comparison function. Should take two parameters, say x and y, and return the a negative number, 0, or positive number, for the cases x < y, x == y, and x > y, respectively. :type compare: Function or ``None`` :param updator: Node updator :type updator: Function or ``None`` .. Note:: The compare fuction is deprecated in favor of the key function. Example: >>> # (Sorted-list) frozen sorted dict with initial items >>> t = FrozenSortedDict([(1, 'a'), (2, 'b')]) >>> list(t) [1, 2] >>> assert 1 in t >>> assert 4 not in t >>> t[130] = 'koko' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'FrozenSortedDict' object does not support item assignment Key-Type Example: >>> # Almost no change! >>> t = FrozenSortedDict([(1, 'a'), (2, 'b')], key_type = int) >>> # Identical from here. """ try: items = items.items() except AttributeError: pass self._init_info = _CommonInitInfo(key_type, alg, key, compare, updator) metadata = _updator_metadata(0, self._init_info) _adopt_updator_methods(self, updator) FrozenDictTree.__init__( self, alg, items, key_type, 0, metadata, key, compare, 0)