Keep the unique order in sync.
429692
def save(self, *args, **kwargs):
        """"""
        objects = self.get_filtered_manager()
        old_pos = getattr(self, '_original_sort_order', None)
        new_pos = self.sort_order

        if old_pos is None and self._unique_togethers_changed():
            self.sort_order = None
            new_pos = None

        try:
            with transaction.atomic():
                self._save(objects, old_pos, new_pos)
        except IntegrityError:
            with transaction.atomic():
                old_pos = objects.filter(pk=self.pk).values_list(
                    'sort_order', flat=True)[0]
                self._save(objects, old_pos, new_pos)

        # Call the "real" save() method.
        super(Orderable, self).save(*args, **kwargs)
912335
private function ensureOrderColumnExists()
    {
        $attributes = $this
            ->connection
            ->createQueryBuilder()
            ->select('metamodel.tableName', 'attribute.colname')
            ->from('tl_metamodel_attribute', 'attribute')
            ->leftJoin('attribute', 'tl_metamodel', 'metamodel', 'metamodel.id=attribute.pid')
            ->where('attribute.type=:type')
            ->setParameter('type', 'file')
            ->andWhere('attribute.file_multiple=:multiple')
            ->setParameter('multiple', '1')
            ->execute();

        while ($row = $attributes->fetch(\PDO::FETCH_OBJ)) {
            if ($this->fieldExists($row->colname . '__sort', $row->tableName)) {
                continue;
            }
            $this
                ->connection
                ->exec(
                    \sprintf(
                        'ALTER TABLE %1$s ADD COLUMN %2$s__sort %3$s',
                        $row->tableName,
                        $row->colname,
                        'blob NULL'
                    )
                );
        }
    }
1127746
def _consolidate_inplace(self):
        """"""

        def f():
            self._data = self._data.consolidate()

        self._protect_consolidate(f)
1266263
def insert_many(self, it):
        """"""
        unique_indexes = self._uniqueIndexes  # [ind for ind in self._indexes.values() if ind.is_unique]
        NO_SUCH_ATTR = object()
        new_objs = list(it)
        if unique_indexes:
            for ind in unique_indexes:
                ind_attr = ind.attr
                new_keys = dict((getattr(obj, ind_attr, NO_SUCH_ATTR), obj) for obj in new_objs)
                if not ind.accept_none and (None in new_keys or NO_SUCH_ATTR in new_keys):
                    raise KeyError("unique key cannot be None or blank for index %s" % ind_attr, 
                                    [ob for ob in new_objs if getattr(ob, ind_attr, NO_SUCH_ATTR) is None])
                if len(new_keys) < len(new_objs):
                    raise KeyError("given sequence contains duplicate keys for index %s" % ind_attr)
                for key in new_keys:
                    if key in ind:
                        obj = new_keys[key]
                        raise KeyError("duplicate unique key value '%s' for index %s" % (getattr(obj, ind_attr), ind_attr), 
                                       new_keys[key])
                    
        for obj in new_objs:
            self.obs.append(obj)
            for attr, ind in self._indexes.items():
                obval = getattr(obj, attr)
                ind[obval] = obj

        return self
256439
def sync_in_records(self, force=False):
        """"""
        self.log('---- Sync Files ----')

        for f in self.build_source_files:
            f.record_to_objects()

        # Only the metadata needs to be driven to the objects, since the other files are used as code,
        # directly from the file record.
        self.build_source_files.file(File.BSFILE.META).record_to_objects()

        self.commit()
355304
def reorder(self, single_column=False):
        """"""
        if single_column:
            columns = self.sortOrder[:1]
        else:
            columns = self.sortOrder
        for ascending,column in columns[::-1]:
            # Python 2.2+ guarantees stable sort, so sort by each column in reverse 
            # order will order by the assigned columns 
            self.sorted.sort( key=column.get, reverse=(not ascending))
230226
private function commitPersisted()
    {
        $sortedDocuments = $this->sortObjects($this->scheduledForPersist);
        $client = $this->sm->getClient();

        foreach ($sortedDocuments as $entityName => $documents) {
            $classMetadata = $this->sm->getClassMetadata($entityName);
            $this->updatedIndexes[] = $classMetadata->index;
            $client->addDocuments($classMetadata, $documents);
        }
    }
1081989
def Flush(self):
    """"""
    if self.locked and self.CheckLease() == 0:
      self._RaiseLockError("Flush")

    self._WriteAttributes()
    self._SyncAttributes()

    if self.parent:
      self.parent.Flush()
511372
def extend_unique(list1, list2):
    """"""
    for item in list2:
        if item not in list1:
            list1.append(item)
1023589
def sync(self, data):
        """"""
        for k, v in data.get('billing', {}).items():
            setattr(self, k, v)
        self.card_number = data.get('credit_card', {}).get('card_number',
                                                           self.card_number)
        self.save(sync=False)
32475
private void moveRowsBasedOnPriority() {
        for (RowNumber myNumber : overs.keySet()) {
            Over over = overs.get(myNumber);

            int newIndex = rowOrder.indexOf(new RowNumber(over.getOver()));

            rowOrder.remove(myNumber);
            rowOrder.add(newIndex,
                         myNumber);
        }
    }
25631
def keep(self, keep_names):
        """"""
        
        current_names = self._data.columns
        drop_names = []
        for name in current_names:
            if name not in keep_names:
                drop_names.append(name)
        self.drop(drop_names)
618380
def sync(self):
        """"""
        self.driver.create_schema()
        self.driver.set_metadata({
            'current_version': Gauged.VERSION,
            'initial_version': Gauged.VERSION,
            'block_size': self.config.block_size,
            'resolution': self.config.resolution,
            'created_at': long(time() * 1000)
        }, replace=False)
126270
def preserve_order(self, value):
        

        if value is not None:
            assert type(value) is bool, "'{0}' attribute: '{1}' type is not 'bool'!".format("preserve_order", value)
        self.__preserve_order = value
365426
def __sync(self):
    """"""
    pad_length = _BLOCK_SIZE - self.__reader.tell() % _BLOCK_SIZE
    if pad_length and pad_length != _BLOCK_SIZE:
      data = self.__reader.read(pad_length)
      if len(data) != pad_length:
        raise EOFError('Read %d bytes instead of %d' %
                       (len(data), pad_length))
1122786
def apply_order(self):
    ''''''
    self._ensure_modification_is_safe()

    if len(self.query.orders) > 0:
      self._iterable = Order.sorted(self._iterable, self.query.orders)
1248096
def unique_list(input_, key=lambda x:x):
    """"""
    seen = set()
    output = []
    for x in input_:
        keyx = key(x)
        if keyx not in seen:
            seen.add(keyx)
            output.append(x)
    return output