Skip to content

odict

Ordered dictionary.

Classes

OrderedDict(data=None)

Bases: dict

A dictionary that keeps its keys in the order in which they're inserted.

Copied from Django's SortedDict with some modifications.

Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def __init__(self, data=None):
    if data is None or isinstance(data, dict):
        data = data or []
        super(OrderedDict, self).__init__(data)
        self.keyOrder = list(data) if data else []
    else:
        super(OrderedDict, self).__init__()
        super_set = super(OrderedDict, self).__setitem__
        for key, value in data:
            # Take the ordering from first key
            if key not in self:
                self.keyOrder.append(key)
            # But override with last value in data (dict() does this)
            super_set(key, value)

Attributes

keyOrder = list(data) if data else [] instance-attribute
iteritems = _iteritems class-attribute instance-attribute
iterkeys = _iterkeys class-attribute instance-attribute
itervalues = _itervalues class-attribute instance-attribute

Functions

pop(k, *args)
Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def pop(self, k, *args):
    result = super(OrderedDict, self).pop(k, *args)
    try:
        self.keyOrder.remove(k)
    except ValueError:
        # Key wasn't in the dictionary in the first place. No problem.
        pass
    return result
popitem()
Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def popitem(self):
    result = super(OrderedDict, self).popitem()
    self.keyOrder.remove(result[0])
    return result
items()
Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def items(self):
    return [(k, self[k]) for k in self.keyOrder]
keys()
Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def keys(self):
    return self.keyOrder[:]
values()
Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def values(self):
    return [self[k] for k in self.keyOrder]
update(dict_)
Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def update(self, dict_):
    for k in dict_:
        self[k] = dict_[k]
setdefault(key, default)
Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def setdefault(self, key, default):
    if key not in self:
        self.keyOrder.append(key)
    return super(OrderedDict, self).setdefault(key, default)
value_for_index(index)

Returns the value of the item at the given zero-based index.

Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def value_for_index(self, index):
    """Returns the value of the item at the given zero-based index."""
    return self[self.keyOrder[index]]
insert(index, key, value)

Inserts the key, value pair before the item with the given index.

Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def insert(self, index, key, value):
    """Inserts the key, value pair before the item with the given index."""
    if key in self.keyOrder:
        n = self.keyOrder.index(key)
        del self.keyOrder[n]
        if n < index:
            index -= 1
    self.keyOrder.insert(index, key)
    super(OrderedDict, self).__setitem__(key, value)
copy()

Returns a copy of this object.

Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def copy(self):
    """Returns a copy of this object."""
    # This way of initializing the copy means it works for subclasses, too.
    return self.__class__(self)
clear()
Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def clear(self):
    super(OrderedDict, self).clear()
    self.keyOrder = []
index(key)

Return the index of a given key.

Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def index(self, key):
    """Return the index of a given key."""
    try:
        return self.keyOrder.index(key)
    except ValueError:
        raise ValueError("Element '%s' was not found in OrderedDict" % key)
index_for_location(location)

Return index or None for a given location.

Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def index_for_location(self, location):
    """Return index or None for a given location."""
    if location == '_begin':
        i = 0
    elif location == '_end':
        i = None
    elif location.startswith('<') or location.startswith('>'):
        i = self.index(location[1:])
        if location.startswith('>'):
            if i >= len(self):
                # last item
                i = None
            else:
                i += 1
    else:
        raise ValueError('Not a valid location: "%s". Location key '
                         'must start with a ">" or "<".' % location)
    return i
add(key, value, location)

Insert by key location.

Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def add(self, key, value, location):
    """Insert by key location."""
    i = self.index_for_location(location)
    if i is not None:
        self.insert(i, key, value)
    else:
        self.__setitem__(key, value)

Change location of an existing item.

Source code in pyrevitlib/pyrevit/coreutils/markdown/odict.py
def link(self, key, location):
    """Change location of an existing item."""
    n = self.keyOrder.index(key)
    del self.keyOrder[n]
    try:
        i = self.index_for_location(location)
        if i is not None:
            self.keyOrder.insert(i, key)
        else:
            self.keyOrder.append(key)
    except Exception as e:
        # restore to prevent data loss and reraise
        self.keyOrder.insert(n, key)
        raise e