예제 #1
0
 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
예제 #2
0
 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
예제 #3
0
 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
예제 #4
0
 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
예제 #5
0
    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)
예제 #6
0
    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)
예제 #7
0
 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)
예제 #8
0
    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
예제 #9
0
    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
예제 #10
0
 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)
예제 #11
0
    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
예제 #12
0
    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
예제 #13
0
 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)
예제 #14
0
 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)
예제 #15
0
 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])
예제 #16
0
 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])
예제 #17
0
 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])
예제 #18
0
 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])