Example #1
0
    def __setitem__ (self, key, value):
        '''Set the value of an item in the dict.

        Implements copy-on-write until flushed.
        '''

        if self.__unsaved__ == None: self.__unsaved__ = dict(self)
        schema = self.__schema__
        if schema == any or any in schema or schema[dict] == any or any in self.__schema__[dict]:
            self.__unsaved__[key] = coconut.schema.Schema.import_element(value,any,self)
            return
        traverse = schema.get('traverse',True)
        if schema[dict] == any:
            if traverse:
                element = coconut.schema.Schema.import_element(value,any,self)
            else:
                element = value
        else:
            if not key in schema[dict]:
                raise coconut.error.ValidationKeyError (key)
            item_schema = schema[dict][key]
            if traverse:
                element = coconut.schema.Schema.import_element(value, item_schema, self)
            else: 
                element = value
        self.__unsaved__[key] = element
Example #2
0
 def remove (self, value):
     '''Remove an item from the list by value.'''
     if self.__unsaved__ == None:
         self.__flushed__ = list(self)
         self.__unsaved__ = list(self)
     if self.__schema__ == any:
         self.__unsaved__.remove(coconut.schema.Schema.import_element(value,any,self))
         return
     schema = self.__schema__
     traverse = schema.get('traverse',True)
     idx = len(self.__unsaved__)
     item_schema = coconut.schema.Schema.get_list_index_schema(idx, schema)
     if traverse:
         element = coconut.schema.Schema.import_element(value,item_schema,self)
     else:
         element = value
     self.__unsaved__.remove(element)
Example #3
0
 def append (self, value):
     '''Add an item to the end of a list.'''
     
     if self.__unsaved__ == None:
         self.__flushed__ = list(self)
         self.__unsaved__ = list(self)
     if self.__schema__ == any:
         self.__unsaved__.append(coconut.schema.Schema.import_element(value,any,self))
         return
     schema = self.__schema__
     traverse = schema.get('traverse',True)
     idx = len(self.__unsaved__)
     item_schema = coconut.schema.Schema.get_list_index_schema(idx, schema)
     if traverse:
         element = coconut.schema.Schema.import_element(value,item_schema,self)
     else:
         element = value
     self.__unsaved__.append(element)
Example #4
0
    def __setitem__ (self, idx, value):
        '''Set the value of an item in the list.

        Implements copy-on-write until flushed.
        '''

        if self.__unsaved__ == None:
            self.__flushed__ = list(self)
            self.__unsaved__ = list(self)
        if self.__schema__ == any:
            self.__unsaved__[key] = coconut.schema.Schema.import_element(value,any,self)
            return
        schema = self.__schema__
        traverse = schema.get('traverse',True)
        item_schema = coconut.schema.Schema.get_list_index_schema(idx,schema)
        if traverse:
            element = coconut.schema.Schema.import_element(value,item_schema,self)
        else:
            element = value
        self.__unsaved__[key] = element