def __contains__(self, key): try: path, _ = self.prepare_key(key) leaf = self._search(iord(path), self._root) if hasattr(leaf, 'value'): return True except AttributeError: return False
def __delitem__(self, key): if not isinstance(key, slice): try: path, _ = self.prepare_key(key) leaf = self._delete(iord(path), self._root) del leaf.value, leaf.meta self._values -= 1 except AttributeError: raise KeyError(key) else: cut = self._build_slice(key) for node in self._iter(self._root, cut.start, cut.stop, cut.step): if not hasattr(node, 'value'): continue leaf = self._delete(iord(node.path), self._root) del leaf.value, leaf.meta self._values -= 1
def _build_slice(self, key): if key.start is not None: start = iord(self.prepare_key(key.start)[0]) else: start = tuple() if key.stop is not None: stop = iord(self.prepare_key(key.stop)[0]) else: stop = tuple() if key.step is None or key.step == 1: reverse = False elif key.step == -1: reverse = True else: raise ValueError("slice step must be 1, -1 or None") return slice(start, stop, reverse)
def __getitem__(self, key): if not isinstance(key, slice): try: path, _ = self.prepare_key(key) leaf = self._search(iord(path), self._root) return leaf.value except AttributeError: raise KeyError(key) else: cut = self._build_slice(key) return self._iter_values(self._root, cut.start, cut.stop, cut.step)
def add(self, key): """Add an element to a set. This has no effect if the element is already present. """ path, meta = self.prepare_key(key) leaf = self._insert(iord(path), self._root) if not hasattr(leaf, 'value'): self._values += 1 leaf.value = None leaf.meta = meta
def discard(self, key): """Remove an element from a set if it is a member. If the element is not a member, do nothing. """ try: path, _ = self.prepare_key(key) leaf = self._delete(iord(path), self._root) del leaf.value, leaf.meta self._values -= 1 except AttributeError: pass
def __setitem__(self, key, value): if not isinstance(key, slice): path, meta = self.prepare_key(key) leaf = self._insert(iord(path), self._root) if not hasattr(leaf, 'value'): self._values += 1 leaf.value = value leaf.meta = meta else: cut = self._build_slice(key) values = iter(value) for node in self._iter(self._root, cut.start, cut.stop, cut.step): if not hasattr(node, 'value'): continue try: node.value = next(values) except StopIteration: msg = "Fewer new elements to than slice length" raise ValueError(msg)
def test_unicode_path(self): t = trie.TrieBase() path, encoded = t.prepare_key(b'ab'.decode('UTF-8')) self.assertTrue(encoded) self.assertSequenceEqual(list(trie.iord(path)), [97, 98])
def test_byte_path(self): t = trie.TrieBase() path, encoded = t.prepare_key(b'ab') self.assertFalse(encoded) self.assertSequenceEqual(list(trie.iord(path)), [97, 98])