Example #1
1
    def __new__(cls, *args):
        """Create a new immutable GroupableOrderedDict."""
        new = OrderedDict.__new__(cls)
        OrderedDict.__init__(new)

        if len(args) > 1:
            raise TypeError("expected at most 1 arguments, got {}".format(len(args)))

        ordering = []
        values = args[0]

        if values:
            if isinstance(values, GroupableOrderedDict):
                values = values.iteritems(with_order=False, repeated=True)
            elif isinstance(values, dict):
                if "__order__" in values:
                    order = values.pop("__order__")

                    tmp = []
                    c = Counter()
                    for key in order:
                        v = values[key]
                        if not isinstance(v, (tuple, list)):
                            if c[key] == 0:
                                tmp.append((key, v))
                            else:
                                raise Exception(
                                    "Order and values don't match" "on key {0} at position {1}".format(key, c[key])
                                )
                        else:
                            tmp.append((key, v[c[key]]))
                        c[key] += 1
                    values = tmp
                else:
                    values = iteritems(values)

            for key, value in values:
                if key not in new:
                    OrderedDict.__setitem__(new, key, [])
                v = []
                if isinstance(value, (tuple, list)):
                    for item in value:
                        v.append(item)
                        ordering.append(key)
                else:
                    v.append(value)
                    ordering.append(key)

                OrderedDict.__getitem__(new, key).extend(v)

        # Immutable...
        for key, value in dict.items(new):
            OrderedDict.__setitem__(new, key, tuple(value))

        OrderedDict.__setitem__(new, "__order__", tuple(ordering))
        return new
Example #2
1
class Metadata(MutableMapping):
    def __init__(self, seq=None):
        self.dct = OrderedDict(seq) if seq else OrderedDict()

    def __contains__(self, key):
        return self.dct.__contains__(key)

    def __getitem__(self, key):
        return self.dct.__getitem__(key)

    def __setitem__(self, key, value):
        self.dct.__setitem__(key, value)

    def __delitem__(self, key):
        return self.dct.__delitem__(key)

    def __iter__(self):
        return self.dct.__iter__()

    def __len__(self):
        return self.dct.__len__()

    def __repr__(self):
        return repr(self.dct)

    def __str__(self):
        return str(self.dct)
class InfoRow:
    def __init__(self):
        self.dict = OrderedDict()

    def __getitem__(self, item):
        if isinstance(item, int):
            return self.dict.items()[item][1]
        return self.dict.__getitem__(item.lower())

    def __contains__(self, item):
        return self.dict.__contains__(item.lower())

    def __setitem__(self, key, value):
        if isinstance(key, int):
            self.dict.items()[key][1] = value
        self.dict.__setitem__(key.lower(), value)

    def get(self, k, d=None):
        if isinstance(k, int):
            try:
                return self.dict.items()[k][1]
            except:
                return d
        return self.dict.get(k.lower(), d)

    def __str__(self, rowSeparator="\n", columnSeparator="\t"):
        return getStr(dict2Str(self.dict))

    def __unicode__(self):
        return str(self)

    def __repr__(self):
        return str(self)
class _NSContext(object):
    """
    A mapping from XML namespaces onto their prefixes in the document.
    """

    def __init__(self, parent=None):
        """
        Pull out the parent's namespaces, if there's no parent then default to
        XML.
        """
        self.parent = parent
        if parent is not None:
            self.nss = OrderedDict(parent.nss)
        else:
            self.nss = {"http://www.w3.org/XML/1998/namespace": "xml"}

    def get(self, k, d=None):
        """
        Get a prefix for a namespace.

        @param d: The default prefix value.
        """
        return self.nss.get(k, d)

    def __setitem__(self, k, v):
        """
        Proxy through to setting the prefix for the namespace.
        """
        self.nss.__setitem__(k, v)

    def __getitem__(self, k):
        """
        Proxy through to getting the prefix for the namespace.
        """
        return self.nss.__getitem__(k)
Example #5
1
class Pipeline:
    def __init__(self, name, **args):
        self.name = name
        self.dict = OrderedDict(args)

    def __getitem__(self, key):
        return self.dict.__getitem__(key)

    def __setitem__(self, key, val):
        return self.dict.__setitem__(key, val)

    def has_key(self, key):
        return self.dict.__contains__(key)

    def __repr__(self):
        return "Pipeline(%s,%r)" % (self.name, ", ".join("%s=%s" % (key, val) for (key, val) in self.dict.items()))

    def flush(self):
        self.dict.clear()

    def stall(self, needed):
        stall = 0
        for x in needed:
            stall = max(stall, self.dict.get(x, 0))
        return stall

    def conflicts(self, needed):
        conflict = OrderedDict()
        for x in needed:
            if x in self.dict:
                conflict[x] = self.dict[x]
        return conflict

    def retire(self, cycles=1):
        dict_retire(self.dict, cycles)
Example #6
1
    def __getitem__(self, item):
        if isinstance(item, slice):
            data = []

            start, stop = self.__sliceToIndex(item)
            for point in xrange(start, stop):
                data += [self.__getitem__(point)]

            return data
        if isinstance(item, int):
            if item < 0:
                return OrderedDict.__getitem__(self, self._time_of_pos[(OrderedDict.__len__(self)) + item])
            else:
                return OrderedDict.__getitem__(self, self._time_of_pos[item])
        else:
            return OrderedDict.__getitem__(self, item)
Example #7
1
class Buckets(object):
    """Proxy for OrderedDict"""

    def __init__(self, *args, **kwargs):
        self._od = OrderedDict(*args, **kwargs)

    def __getattr__(self, a):
        return getattr(self._od, a)

    def __setitem__(self, *args, **kwargs):
        return self._od.__setitem__(*args, **kwargs)

    def __getitem__(self, *args, **kwargs):
        return self._od.__getitem__(*args, **kwargs)

    def __delitem__(self, *args, **kwargs):
        return self._od.__delitem__(*args, **kwargs)

    def __eq__(self, other):
        if isinstance(other, Buckets):
            return self._od.__eq__(other._od)
        else:
            return self._od.__eq__(other)

    def copy(self, *args, **kwargs):
        new = Buckets()
        new._od = self._od.copy()
        return new
Example #8
1
    def handle_header(self, header):
        # FIXME: non-crlf-endings
        if not header.replace("\r\n", ""):
            self.headers_complete(True)
            return

        header = header.replace("\r\n", "")

        # multiline headers
        if header.startswith(("\t", " ")):
            k = list(self.headers.keys())[-1]
            from collections import OrderedDict

            raw_v = OrderedDict.__getitem__(self.headers, k)
            k, v = raw_v[-1]

            # section 4.2 says that we MAY reduce whitespace down to a single
            # character, so let's do it.
            v = "".join((v, header.lstrip()))

            raw_v[-1] = k, v
        else:
            k, v = header.split(":", 1)
            k = k.rstrip()
            v = v.lstrip()

            self.headers[k] = v
Example #9
1
 def addData(self, data):
     """
     Adds data to the set. Must match the variable order of the set and the
     number of variables in the set.
     """
     if len(data) != 0:
         pos = 1
         for var in OrderedDict.__iter__(self):
             (OrderedDict.__getitem__(self, var).addData([(column[0], column[pos]) for column in data]))
             pos += 1
Example #10
1
    def __getLine(self, pos=None, add_time=False):
        if add_time is False:
            line = ()
        else:
            line = (self._time.getTimeFromPos(pos),)

        for var in OrderedDict.__iter__(self):
            line += (OrderedDict.__getitem__(self, var)[pos],)

        return line
Example #11
1
 def __getitem__(self, key):
     if OrderedDict.__contains__(self, key):
         return OrderedDict.__getitem__(self, key)
     elif (not OrderedDict.__contains__(self, key)) and (self.parent is not None):
         dct = find_next_dict(self)
         if dct is None:
             raise KeyError("%r not in dictionary" % key)
         else:
             return dct[key]
     else:
         raise KeyError("%r not in dictionary" % key)
Example #12
1
class MetafeatureFunctions(object):
    def __init__(self):
        self.functions = OrderedDict()
        self.dependencies = OrderedDict()
        self.values = OrderedDict()

    def clear(self):
        self.values = OrderedDict()

    def __iter__(self):
        return self.functions.__iter__()

    def __getitem__(self, item):
        return self.functions.__getitem__(item)

    def __setitem__(self, key, value):
        return self.functions.__setitem__(key, value)

    def __delitem__(self, key):
        return self.functions.__delitem__(key)

    def __contains__(self, item):
        return self.functions.__contains__(item)

    def get_value(self, key):
        return self.values[key].value

    def set_value(self, key, item):
        self.values[key] = item

    def is_calculated(self, key):
        """Return if a helper function has already been executed.

        Necessary as get_value() can return None if the helper function hasn't
        been executed or if it returned None."""
        return key in self.values

    def get_dependency(self, name):
        """Return the dependency of metafeature "name".
        """
        return self.dependencies.get(name)

    def define(self, name, dependency=None):
        """Decorator for adding metafeature functions to a "dictionary" of
        metafeatures. This behaves like a function decorating a function,
        not a class decorating a function"""

        def wrapper(metafeature_class):
            instance = metafeature_class()
            self.__setitem__(name, instance)
            self.dependencies[name] = dependency
            return instance

        return wrapper
Example #13
0
 def __getitem__(self, key):
     try:
         return OrderedDict.__getitem__(self, key)
     except KeyError:
         key = key.split(", ")
         key2 = ", ".join(key[1:])
         return self[key[0]].__getitem__(key2)
Example #14
0
 def pop(self, key, default=None):
     try:
         item = OrderedDict.__getitem__(self, key)
         del self[key]
         return item[0]
     except KeyError:
         return default
Example #15
0
 def __getitem__(self, key):
     """
     :return: string representing the concatenation of the values for field 'key'.
     """
     value = OrderedDict.__getitem__(self, key)
     if not isinstance(value, (tuple, list)):
         value = [value]
     return Record.sep(key).join(filter(None, value))
Example #16
0
 def __getitem__(self, k):
     if not isinstance(k, slice):
         return OrderedDict.__getitem__(self, k)
     x = SlicableOrderedDict()
     for idx, key in enumerate(self.keys()):
         if k.start <= idx < k.stop:
             x[key] = self[key]
     return x
Example #17
0
 def set_ttl(self, key, seconds):
     is_have = OrderedDict.__getitem__(self, key)
     if is_have:
         expire_time = time.time() + seconds
         self.key_time_map[key] = {"time": time.time(), "max_age": 0, "expire_time": expire_time}
         key_ttl = expire_time - time.time()
         if key_ttl > 0:
             return key_ttl
     return None
Example #18
0
    def __getitem__(self, key):
        """D[key].

        It will return a list or an element depending on the quantity present.
        """
        item = OrderedDict.__getitem__(self, key)
        if len(item) == 1 and key != "__order__":
            return item[0]
        return item
Example #19
0
 def __getitem__(self, key):
     """returns just first value of list/tuple or rdict"""
     r = OrderedDict.__getitem__(self, key)
     if r and isinstance(r, list):
         return r[0]
     elif isinstance(r, (rdict, ufile)):
         return r
     else:
         return None
Example #20
0
 def __contains__(self, key):
     try:
         item = OrderedDict.__getitem__(self, key)
         if time.time() - item[1] < self.max_age:
             return True
         else:
             del self[key]
     except KeyError:
         pass
     return False
Example #21
0
        def __getitem__(self, key, *args, **kwargs):
            # Get the key and remove it from the cache, or raise KeyError
            value = OrderedDict.__getitem__(self, key)
            del self[key]

            # Insert the (key, value) pair on the front of the cache
            OrderedDict.__setitem__(self, key, value)

            # Return the value from the cache
            return value
Example #22
0
    def __getitem__(self, key):
        """Extends OrderedDict.__getitem__() to handle path lists as keys"""
        if isinstance(key, list):
            value = self
            keys = deepcopy(key)
            while len(keys) > 0:
                value = value[keys.pop(0)]
            return value

        else:
            return OrderedDict.__getitem__(self, key)
Example #23
0
 def __contains__(self, key):
     """ Return True if the dict has a key, else return False. """
     try:
         with self.lock:
             item = OrderedDict.__getitem__(self, key)
             if time.time() - item[1] < self.max_age:
                 return True
             else:
                 del self[key]
     except KeyError:
         pass
     return False
Example #24
0
 def __getitem__(self, key):
     """
	>>> q = pykov.Vector(C=.4, B=.6)
	>>> q['C']
	0.4
	>>> q['Z']
	0.0
	"""
     try:
         return OrderedDict.__getitem__(self, key)
     except KeyError:
         return 0.0
Example #25
0
    def pop(self, key, default=None):
        """ Get item from the dict and remove it.

        Return default if expired or does not exist. Never raise KeyError.
        """
        with self.lock:
            try:
                item = OrderedDict.__getitem__(self, key)
                del self[key]
                return item[0]
            except KeyError:
                return default
Example #26
0
File: visig.py Project: sangoma/NLA
    def __getitem__(self, key):
        # if it's already mapped get the value
        if isinstance(key, slice):
            print("you've passed a slice! with start", key.start, "and stop", key.stop)
            return OrderedIndexedDict(self.values())[key]

        elif key in self:
            return OrderedDict.__getitem__(self, key)

        elif isinstance(key, int):
            # TODO: is this the fastest way to implement this?
            return list(self.values())[key]
Example #27
0
 def __contains__(self, key):
     """ Return True if the dict has a key, else return False. """
     try:
         with self.lock:
             item = OrderedDict.__getitem__(self, key)
             expire_time = self.key_time_map[key].get("expire_time", None)
             if expire_time and expire_time > 0:
                 return True
             else:
                 del self[key]
     except KeyError:
         pass
     return False
Example #28
0
 def __getitem__(self, key, with_age=False, max_age=None):
     item = OrderedDict.__getitem__(self, key)
     item_age = time.time() - item[1]
     if not max_age:
         max_age = self.max_age
     if item_age < max_age:
         if with_age:
             return item[0], item_age
         else:
             return item[0]
     else:
         del self[key]
         raise KeyError(key)
Example #29
0
 def __getitem__(self, *args):
     """
	>>> T = pykov.Matrix({('A','B'): .3, ('A','A'): .7, ('B','A'): 1.})
	>>> T[('A','B')]
	0.3
	>>> T['A','B']
	0.3
	>>> 
	0.0
	"""
     try:
         return OrderedDict.__getitem__(self, args[0])
     except KeyError:
         return 0.0
Example #30
0
    def __getitem__(self, key):
        """
        Returns the item matching a key. The key matching has the same
        charasteristics as __contains__ method.

        Example::

            class A(object):
                pass


            class B(object):
                pass


            class A2(A):
                pass


            class_map = ClassMap({A: 1, B: 2})
            assert class_map[B] == 2
            assert class_map[A] == 1
            assert class_map[A2] == 1
            assert class_map[B()] == 2
            assert class_map[A()] == 1
            assert class_map[A2()] == 1
        """
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            if not isclass(key):
                key = type(key)
            for class_ in self:
                if issubclass(key, class_):
                    return OrderedDict.__getitem__(self, class_)
            raise