(self, key, value=None)
Inserts the given key into the graph.

        :param key: a sequence of bytes objects, a bytes object, or a string.
        :param value: an optional value to encode in the graph along with the
            key. If the writer was not instantiated with a value type, passing
            a value here will raise an error.
Inserts the given key into the graph.
Inserts the given key into the graph .
def insert(self, key, value=None):
        """Inserts the given key into the graph.

        :param key: a sequence of bytes objects, a bytes object, or a string.
        :param value: an optional value to encode in the graph along with the
            key. If the writer was not instantiated with a value type, passing
            a value here will raise an error.
        """

        if not self._infield:
            raise Exception("Inserted %r before starting a field" % key)
        self._inserted = True
        key = to_labels(key)  # Python 3 sucks

        vtype = self.vtype
        lastkey = self.lastkey
        nodes = self.nodes
        if len(key) < 1:
            raise KeyError("Can't store a null key %r" % (key,))
        if lastkey and lastkey > key:
            raise KeyError("Keys out of order %r..%r" % (lastkey, key))

        # Find the common prefix shared by this key and the previous one
        prefixlen = 0
        for i in xrange(min(len(lastkey), len(key))):
            if lastkey[i] != key[i]:
                break
            prefixlen += 1
        # Compile the nodes after the prefix, since they're not shared
        self._freeze_tail(prefixlen + 1)

        # Create new nodes for the parts of this key after the shared prefix
        for char in key[prefixlen:]:
            node = UncompiledNode(self)
            # Create an arc to this node on the previous node
            nodes[-1].add_arc(char, node)
            nodes.append(node)
        # Mark the last node as an accept state
        lastnode = nodes[-1]
        lastnode.accept = True

        if vtype:
            if value is not None and not vtype.is_valid(value):
                raise ValueError("%r is not valid for %s" % (value, vtype))

            # Push value commonalities through the tree
            common = None
            for i in xrange(1, prefixlen + 1):
                node = nodes[i]
                parent = nodes[i - 1]
                lastvalue = parent.last_value(key[i - 1])
                if lastvalue is not None:
                    common = vtype.common(value, lastvalue)
                    suffix = vtype.subtract(lastvalue, common)
                    parent.set_last_value(key[i - 1], common)
                    node.prepend_value(suffix)
                else:
                    common = suffix = None
                value = vtype.subtract(value, common)

            if key == lastkey:
                # If this key is a duplicate, merge its value with the value of
                # the previous (same) key
                lastnode.value = self.merge(lastnode.value, value)
            else:
                nodes[prefixlen].set_last_value(key[prefixlen], value)
        elif value:
            raise Exception("Value %r but no value type" % value)

        self.lastkey = key
def insert ( self , key , value = None ) : if not self . _infield : raise Exception ( "Inserted %r before starting a field" % key ) self . _inserted = True key = to_labels ( key ) # Python 3 sucks vtype = self . vtype lastkey = self . lastkey nodes = self . nodes if len ( key ) < 1 : raise KeyError ( "Can't store a null key %r" % ( key , ) ) if lastkey and lastkey > key : raise KeyError ( "Keys out of order %r..%r" % ( lastkey , key ) ) # Find the common prefix shared by this key and the previous one prefixlen = 0 for i in xrange ( min ( len ( lastkey ) , len ( key ) ) ) : if lastkey [ i ] != key [ i ] : break prefixlen += 1 # Compile the nodes after the prefix, since they're not shared self . _freeze_tail ( prefixlen + 1 ) # Create new nodes for the parts of this key after the shared prefix for char in key [ prefixlen : ] : node = UncompiledNode ( self ) # Create an arc to this node on the previous node nodes [ - 1 ] . add_arc ( char , node ) nodes . append ( node ) # Mark the last node as an accept state lastnode = nodes [ - 1 ] lastnode . accept = True if vtype : if value is not None and not vtype . is_valid ( value ) : raise ValueError ( "%r is not valid for %s" % ( value , vtype ) ) # Push value commonalities through the tree common = None for i in xrange ( 1 , prefixlen + 1 ) : node = nodes [ i ] parent = nodes [ i - 1 ] lastvalue = parent . last_value ( key [ i - 1 ] ) if lastvalue is not None : common = vtype . common ( value , lastvalue ) suffix = vtype . subtract ( lastvalue , common ) parent . set_last_value ( key [ i - 1 ] , common ) node . prepend_value ( suffix ) else : common = suffix = None value = vtype . subtract ( value , common ) if key == lastkey : # If this key is a duplicate, merge its value with the value of # the previous (same) key lastnode . value = self . merge ( lastnode . value , value ) else : nodes [ prefixlen ] . set_last_value ( key [ prefixlen ] , value ) elif value : raise Exception ( "Value %r but no value type" % value ) self . lastkey = key