Ejemplo n.º 1
0
 def __setitem__(self, index, value):
     if isinstance(index, slice):
         s = clean_slice(index, self.count, replace_none=True, allow_step=(1,))
         indices = iter(xrange(s.start, s.stop, s.step))
         values = iter(value)
         enum = izip(indices, values)
         extended = False
         for i, val in enum:
             found = self._update_item(i, val)
             if not found:
                 extended = True
                 self._create_item(i, val)
                 for i, val in enum:
                     self._create_item(i, val)
         try:
             # are there more indices than values?
             i = indices.next()
             if not extended:
                 del self[i:s.stop]
         except StopIteration:
             # no more indices, then there might be more values
             val_list = list(values)
             if val_list and not extended:
                 self._shift_gte(s.stop, len(val_list))
             for i, val in enumerate(val_list):
                 self._create_item(s.stop + i, val)
     else:           
         found = self._update_item(index, value)
         if not found:
             raise IndexError("assignment index out of range")
Ejemplo n.º 2
0
 def __delitem__(self, index):
     if isinstance(index, slice):
         s = clean_slice(index, self.count, allow_step=(1,))
         items = self._slice(s)
         count = items.count()
         items.delete()
         if s.stop is not None:
             self._shift_gte(s.stop, -count)
     else:
         try:
             self.pop(index)
         except self.model.DoesNotExist:
             raise IndexError("assignment index out of range")
Ejemplo n.º 3
0
 def _do_slice(self, s, target, lookup, filters=None):
     if not filters:
         filters = {}
     s = clean_slice(s, self.count, allow_step=(1, -1))
     if s.start and s.stop is not None:
         filters["%s__range" % lookup] = (s.start, s.stop - 1)
     elif s.start:
         filters["%s__gte" % lookup] = s.start
     elif s.stop is not None:
         filters["%s__lt" % lookup] = s.stop
     qs = target.filter(**filters)
     if s.step == -1:
         qs = qs.reverse()
     return qs