Exemplo n.º 1
0
    def __setitem__(self, key, value):
        """Hook that intercepts data insertion and updates automatically
        accounting fields.
        """
        encoding = safe_encoding(self.mst)
        try:
            # try to convert key into int if possible
            key = int(key)
        except ValueError:
            pass

        self.last_insert_key.append(key)

        if type(value) in (MasterField, MasterContainerField):
            # adjust encoding in fields
            value.encoding = encoding
            dict.__setitem__(self, key, value)
        elif type(value) in (list, tuple):
            # Build an appropriate MasterContainerField
            dict.__setitem__(self, key, MasterContainerField(key, value,
                                                             config=self.config))
        elif type(value) in (str, unicode):
            # received a simple string
            # delegate the operation to standard dict implementation
            # but create a MasterField instance wrapping the value.
            dict.__setitem__(self, key, MasterField(key, value, config=self.config))
        else:
            raise ValueError("Invalid type %s for field %s"%(type(value), key))
Exemplo n.º 2
0
    def __setitem__(self, key, value):
        """Hook that intercepts data insertion and updates automatically
        accounting fields.
        """
        encoding = safe_encoding(self.mst)
        try:
            # try to convert key into int if possible
            key = int(key)
        except ValueError:
            pass

        self.last_insert_key.append(key)

        if type(value) in (MasterField, MasterContainerField):
            # adjust encoding in fields
            value.encoding = encoding
            dict.__setitem__(self, key, value)
        elif type(value) in (list, tuple):
            # Build an appropriate MasterContainerField
            dict.__setitem__(self, key, MasterContainerField(key, value, config=self.config))
        elif type(value) in (str, unicode):
            # received a simple string
            # delegate the operation to standard dict implementation
            # but create a MasterField instance wrapping the value.
            dict.__setitem__(self, key, MasterField(key, value, config=self.config))
        else:
            raise ValueError("Invalid type %s for field %s" % (type(value), key))
Exemplo n.º 3
0
 def assemble_record(pairs):
     encoding = safe_encoding(self.mst)
     for key, values in groupby(pairs, lambda x: getslice(x, 0, 1)):
         key = key[0]
         fields = [MasterField(tag, data, config=self.config) for tag, data in values]
         if len(fields) == 1:
             # single values are not wrapped in lists
             new_field = fields[0]
         else:
             new_field = MasterContainerField(key, sequence=fields, config=self.config)
         dict.__setitem__(self, key, new_field)
Exemplo n.º 4
0
 def assemble_record(pairs):
     encoding = safe_encoding(self.mst)
     for key, values in groupby(pairs, lambda x: getslice(x, 0, 1)):
         key = key[0]
         fields = [MasterField(tag, data, config=self.config) \
                   for tag,data in  values]
         if len(fields)==1:
             # single values are not wrapped in lists
             new_field = fields[0]
         else:
             new_field = MasterContainerField(key, sequence=fields,
                                              config=self.config)
         dict.__setitem__(self, key, new_field)