Example #1
1
class QuestionTable(object):
    def __init__(self):
        self._table = OrderedDict()

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

    def questions(self):
        return [q for qList in self._table.values() for q in qList]

    def identifiers(self):
        return self._table.keys()

    def add(self, question):
        questionList = self._table.get(question.identifier, QuestionList())
        questionList.append(question)
        self._table[question.identifier] = questionList

    def get(self, identifier):
        questions = self._table.get(identifier, None)
        if questions:
            return questions.getVisibleQuestion()

    def getType(self, identifier):
        questions = self._table.get(identifier, None)
        if questions:
            return questions[0].type

    def getQuestionList(self, identifier):
        return self._table.get(identifier, None)
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)
Example #3
1
class flagEnum(object):
    def __init__(self, *sequential):
        val = 1
        self.__enums = OrderedDict()
        for el in sequential:
            self.__enums[el] = val
            setattr(self, el, val)
            val = val << 1

    def __getitem__(self, key):
        return self.__enums[key]

    def items(self):
        return self.__enums.items()

    def keys(self):
        return self.__enums.keys()

    def values(self):
        return self.__enums.values()

    def mask(self, keys):
        masks = []
        for key in keys:
            masks.append(self[key])
        return reduce(operator.or_, masks) if masks else 0

    def __iter__(self):
        return self.__enums.__iter__()
Example #4
1
class OrderedDictEx(MutableMapping):
    def __init__(self, fifo=True, maxSize=None, readImpactsOrder=False):
        super(OrderedDictEx, self).__init__()

        self._dic = OrderedDict()
        self.fifo = fifo
        self.max_size = maxSize
        self.read_impacts_order = readImpactsOrder

        self._recursing = False

        self._lock = RLock()
        self.recurse_count = 0

    def __setitem__(self, key, value):
        with self._lock:
            # Store items in order that key was last added.
            # This will ensure update has same impact on position
            # as setting the item.
            skipSizeCheck = False  # optimization
            if key in self._dic:
                del self._dic[key]
                skipSizeCheck = True
            self._dic[key] = value

            if self.max_size is not None and not skipSizeCheck:
                while len(self._dic) > self.max_size:
                    self.removeOrderedItem()

    def __str__(self):
        return str(self._dic)

    def __unicode__(self):
        return unicode(self._dic)

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

    def __len__(self):
        return len(self._dic)

    def __getitem__(self, key):
        with self._lock:
            val = self._dic[key]

            if self.read_impacts_order:
                self.__setitem__(key, val)

            return val

    def __delitem__(self, key):
        del self._dic[key]

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

    def removeOrderedItem(self):
        return self._dic.popitem(not self.fifo)
Example #5
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 #6
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 #7
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 #8
1
class IndexedDict(Mapping):
    """Wrapper around OrderedDict that allows access via item position or key"""

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

    def __getitem__(self, k):
        try:
            return list(self._od.values())[k]
        except TypeError:
            return self._od[k]

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

    def __iter__(self):
        return self._od.__iter__()
Example #9
1
class TargetVars(Mapping):
    """Immutable ordered mapping from target variables to their values."""

    EMPTY = EmptyTargetVar()

    def __init__(self, target_vars=tuple(), is_empty=True):
        if is_empty:
            target_vars = [(v, self.EMPTY) for v in target_vars]

        self._od = OrderedDict(target_vars)

    # getitem, len, iter wrap OrderedDict behavior
    def __getitem__(self, k):
        return self._od.__getitem__(k)

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

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

    def update(self, *args, **kwargs):
        cpy = self.copy()
        cpy._od.update(*args, **kwargs)
        return cpy

    def copy(self):
        return self.__class__(self._od)

    def __str__(self):
        """Format target vars for printing"""
        if len(self) > 1:
            return "({})".format(", ".join(self._od.keys()))
        else:
            return "".join(self._od.keys())

    def defined_items(self):
        """Return copy of instance, omitting entries that are EMPTY"""
        return self.__class__([(k, v) for k, v in self.items() if v is not self.EMPTY], is_empty=False)
Example #10
1
class OrderedDefaultDict(object):
    """
    Implements (most of) a default dict with ordering.
    """

    def __init__(self, factory):
        self._factory = factory
        self._dict = OrderedDict()

    def __setitem__(self, key, item):
        self._dict.__setitem__(key, item)

    def __getitem__(self, key):
        if key not in self._dict:
            self._dict[key] = self._factory()
        return self._dict.__getitem__(key)

    def __repr__(self):
        return self._dict.__repr__()

    def __iter__(self):
        return self._dict.__iter__()
Example #11
1
class BaseCache(object):
    """
    BaseCache is a class that saves and operates on an OrderedDict. It has a
    certain capacity, stored in the attribute `maxsize`. Whether this
    capacity is reached, can be checked by using the boolean property
    `is_full`. To implement a custom cache, inherit from this class and
    override the methods ``__getitem__`` and ``__setitem__``.
    Call the method `sunpy.database.caching.BaseCache.callback` as soon
    as an item from the cache is removed.
    """

    def __init__(self, maxsize=float("inf")):
        self.maxsize = maxsize
        self._dict = OrderedDict()

    def get(self, key, default=None):  # pragma: no cover
        """Return the corresponding value to `key` if `key` is in the cache,
        `default` otherwise. This method has no side-effects, multiple calls
        with the same cache and the same passed key must always return the same
        value.

        """
        try:
            return self._dict[key]
        except KeyError:
            return default

    @abstractmethod
    def __getitem__(self, key):
        """abstract method: this method must be overwritten by inheriting
        subclasses. It defines what happens if an item from the cache is
        attempted to be accessed.

        """
        return  # pragma: no cover

    @abstractmethod
    def __setitem__(self, key, value):
        """abstract method: this method must be overwritten by inheriting
        subclasses. It defines what happens if a new value should be assigned
        to the given key. If the given key does already exist in the cache or
        not must be checked by the person who implements this method.
        """

    @abstractproperty
    def to_be_removed(self):
        """The item that will be removed on the next
        :meth:`sunpy.database.caching.BaseCache.remove` call.

        """

    @abstractmethod
    def remove(self):
        """Call this method to manually remove one item from the cache. Which
        item is removed, depends on the implementation of the cache. After the
        item has been removed, the callback method is called.

        """

    def callback(self, key, value):
        """This method should be called (by convention) if an item is removed
        from the cache because it is full. The passed key and value are the
        ones that are removed. By default this method does nothing, but it
        can be customized in a custom cache that inherits from this base class.

        """

    @property
    def is_full(self):
        """True if the number of items in the cache equals :attr:`maxsize`,
        False otherwise.

        """
        return len(self._dict) == self.maxsize

    def __delitem__(self, key):
        self._dict.__delitem__(key)

    def __contains__(self, key):
        return key in self._dict.keys()

    def __len__(self):
        return len(self._dict)

    def __iter__(self):
        for key in self._dict.__iter__():
            yield key

    def __reversed__(self):  # pragma: no cover
        for key in self._dict.__reversed__():
            yield key

    def clear(self):  # pragma: no cover
        return self._dict.clear()

    def keys(self):  # pragma: no cover
        return list(self._dict.keys())

    def values(self):  # pragma: no cover
        return list(self._dict.values())

    def items(self):  # pragma: no cover
        return list(self._dict.items())

    def iterkeys(self):  # pragma: no cover
        return iter(self._dict.keys())

    def itervalues(self):  # pragma: no cover
        for value in self._dict.values():
            yield value

    def iteritems(self):  # pragma: no cover
        for key, value in six.iteritems(self._dict):
            yield key, value

    def update(self, *args, **kwds):  # pragma: no cover
        self._dict.update(*args, **kwds)

    def pop(self, key, default=MutableMapping._MutableMapping__marker):  # pragma: no cover
        return self._dict.pop(key, default)

    def setdefault(self, key, default=None):  # pragma: no cover
        return self._dict.setdefault(key, default)

    def popitem(self, last=True):  # pragma: no cover
        return self._dict.popitem(last)

    def __reduce__(self):  # pragma: no cover
        return self._dict.__reduce__()

    def copy(self):  # pragma: no cover
        return self._dict.copy()

    def __eq__(self, other):  # pragma: no cover
        return self._dict.__eq__(other)

    def __ne__(self, other):  # pragma: no cover
        return self._dict.__ne__(other)

    def viewkeys(self):  # pragma: no cover
        return self._dict.keys()

    def viewvalues(self):  # pragma: no cover
        return self._dict.values()

    def viewitems(self):  # pragma: no cover
        return self._dict.items()

    @classmethod
    def fromkeys(cls, iterable, value=None):  # pragma: no cover
        return OrderedDict.fromkeys(iterable, value)

    def __repr__(self):  # pragma: no cover
        return "{0}({1!r})".format(self.__class__.__name__, dict(self._dict))
Example #12
1
class ChainSet(object):
    """
    Base class for various methods to rename chains

    Contains _chains, which maps from the renamed chain to a tuple with the
    original (object,state,chain). All dict-like accessors work on ChainSets,
    e.g.
        chain_set["A"] -> ("obj",1,"A")

    """

    def __init__(self):
        # Use an OrderedDict in Python >= 1.7 for better printing
        if _orderedDict:
            self._chains = OrderedDict()
        else:
            self._chains = dict()

    def map_chain(self, obj, state, origChain):
        """
        map_chain(string obj,int state, string chain]]) -> string

        Maps a chain letter to a unique chainID. Results are unique within each
        instance, and can be used as keys on this chain set.
        """
        raise NotImplementedError("Base class")

    # delegate most methods to _chains
    def __getattr__(self, at):
        if at in "pop popitem update setdefault".split():
            raise AttributeError("type object '%s' has no attribute '%s'" % (type(self), at))
        return getattr(self._chains, at)

    def __cmp__(self, other):
        return self._chains.__cmp__(other)

    def __eq__(self, other):
        return self._chains.__eq__(other)

    def __ge__(self, other):
        return self._chains.__ge__(other)

    def __gt__(self, other):
        return self._chains.__gt__(other)

    def __le__(self, other):
        return self._chains.__le__(other)

    def __lt__(self, other):
        return self._chains.__lt__(other)

    def __ne__(self, other):
        return self._chains.__ne__(other)

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

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

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

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

    def __str__(self):
        return str(self._chains)

    @staticmethod
    def _int_to_chain(i, base=_default_base):
        """
        _int_to_chain(int,int) -> str

        Converts a positive integer to a chain ID. Chain IDs include uppercase
        characters, numbers, and optionally lowercase letters.

        i = a positive integer to convert
        base = the alphabet size to include. Typically 36 or 62.
        """
        if i < 0:
            raise ValueError("positive integers only")
        if base < 0 or 62 < base:
            raise ValueError("Invalid base")

        quot = int(i) / base
        rem = i % base
        if rem < 26:
            letter = chr(ord("A") + rem)
        elif rem < 36:
            letter = str(rem - 26)
        else:
            letter = chr(ord("a") + rem - 36)
        if quot == 0:
            return letter
        else:
            return ChainSet._int_to_chain(quot - 1, base) + letter
Example #13
1
class DotMap(OrderedDict):
    def __init__(self, *args, **kwargs):
        self._map = OrderedDict()
        if args:
            d = args[0]
            if type(d) is dict:
                for k, v in self.__call_items(d):
                    if type(v) is dict:
                        v = DotMap(v)
                    self._map[k] = v
        if kwargs:
            for k, v in self.__call_items(kwargs):
                self._map[k] = v

    def __call_items(self, obj):
        if hasattr(obj, "iteritems") and ismethod(getattr(obj, "iteritems")):
            return obj.iteritems()
        else:
            return obj.items()

    def items(self):
        return self.iteritems()

    def iteritems(self):
        return self.__call_items(self._map)

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

    def next(self):
        return self._map.next()

    def __setitem__(self, k, v):
        self._map[k] = v

    def __getitem__(self, k):
        if k not in self._map:
            # automatically extend to new DotMap
            self[k] = DotMap()
        return self._map[k]

    def __setattr__(self, k, v):
        if k == "_map":
            super(DotMap, self).__setattr__(k, v)
        else:
            self[k] = v

    def __getattr__(self, k):
        if k == "_map":
            super(DotMap, self).__getattr__(k)
        else:
            return self[k]

    def __delattr__(self, key):
        return self._map.__delitem__(key)

    def __contains__(self, k):
        return self._map.__contains__(k)

    def __str__(self):
        items = []
        for k, v in self.__call_items(self._map):
            items.append("{0}={1}".format(k, repr(v)))
        out = "DotMap({0})".format(", ".join(items))
        return out

    def __repr__(self):
        return str(self)

    def toDict(self):
        d = {}
        for k, v in self.items():
            if type(v) is DotMap:
                v = v.toDict()
            d[k] = v
        return d

    def pprint(self):
        pprint(self.toDict())

        # proper dict subclassing

    def values(self):
        return self._map.values()

    @classmethod
    def parseOther(self, other):
        if type(other) is DotMap:
            return other._map
        else:
            return other

    def __cmp__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__cmp__(other)

    def __eq__(self, other):
        other = DotMap.parseOther(other)
        if not isinstance(other, dict):
            return False
        return self._map.__eq__(other)

    def __ge__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__ge__(other)

    def __gt__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__gt__(other)

    def __le__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__le__(other)

    def __lt__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__lt__(other)

    def __ne__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__ne__(other)

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

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

    def clear(self):
        self._map.clear()

    def copy(self):
        return self

    def get(self, key, default=None):
        return self._map.get(key, default)

    def has_key(self, key):
        return key in self._map

    def iterkeys(self):
        return self._map.iterkeys()

    def itervalues(self):
        return self._map.itervalues()

    def keys(self):
        return self._map.keys()

    def pop(self, key, default=None):
        return self._map.pop(key, default)

    def popitem(self):
        return self._map.popitem()

    def setdefault(self, key, default=None):
        self._map.setdefault(key, default)

    def update(self, *args, **kwargs):
        if len(args) != 0:
            self._map.update(*args)
        self._map.update(kwargs)

    def viewitems(self):
        return self._map.viewitems()

    def viewkeys(self):
        return self._map.viewkeys()

    def viewvalues(self):
        return self._map.viewvalues()

    @classmethod
    def fromkeys(cls, seq, value=None):
        d = DotMap()
        d._map = OrderedDict.fromkeys(seq, value)
        return d
Example #14
1
class DotMap(MutableMapping, OrderedDict):
    def __init__(self, *args, **kwargs):
        self._map = OrderedDict()
        self._dynamic = True
        if kwargs:
            if "_dynamic" in kwargs:
                self._dynamic = kwargs["_dynamic"]
        if args:
            d = args[0]
            if isinstance(d, dict):
                for k, v in self.__call_items(d):
                    if type(v) is dict:
                        v = DotMap(v, _dynamic=self._dynamic)
                    if type(v) is list:
                        l = []
                        for i in v:
                            n = i
                            if type(i) is dict:
                                n = DotMap(i, _dynamic=self._dynamic)
                            l.append(n)
                        v = l
                    self._map[k] = v
        if kwargs:
            for k, v in self.__call_items(kwargs):
                if k is not "_dynamic":
                    self._map[k] = v

    def __call_items(self, obj):
        if hasattr(obj, "iteritems") and ismethod(getattr(obj, "iteritems")):
            return obj.iteritems()
        else:
            return obj.items()

    def items(self):
        return self.iteritems()

    def iteritems(self):
        return self.__call_items(self._map)

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

    def next(self):
        return self._map.next()

    def __setitem__(self, k, v):
        self._map[k] = v

    def __getitem__(self, k):
        if k not in self._map and self._dynamic and k != "_ipython_canary_method_should_not_exist_":
            # automatically extend to new DotMap
            self[k] = DotMap()
        return self._map[k]

    def __setattr__(self, k, v):
        if k in {"_map", "_dynamic", "_ipython_canary_method_should_not_exist_"}:
            super(DotMap, self).__setattr__(k, v)
        else:
            self[k] = v

    def __getattr__(self, k):
        if k == {"_map", "_dynamic", "_ipython_canary_method_should_not_exist_"}:
            super(DotMap, self).__getattr__(k)
        else:
            return self[k]

    def __delattr__(self, key):
        return self._map.__delitem__(key)

    def __contains__(self, k):
        return self._map.__contains__(k)

    def __str__(self):
        items = []
        for k, v in self.__call_items(self._map):
            # bizarre recursive assignment situation (why someone would do this is beyond me)
            if id(v) == id(self):
                items.append("{0}=DotMap(...)".format(k))
            else:
                items.append("{0}={1}".format(k, repr(v)))
        out = "DotMap({0})".format(", ".join(items))
        return out

    def __repr__(self):
        return str(self)

    def toDict(self):
        d = {}
        for k, v in self.items():
            if type(v) is DotMap:
                # bizarre recursive assignment support
                if id(v) == id(self):
                    v = d
                else:
                    v = v.toDict()
            elif type(v) is list:
                l = []
                for i in v:
                    n = i
                    if type(i) is DotMap:
                        n = i.toDict()
                    l.append(n)
                v = l
            d[k] = v
        return d

    def pprint(self):
        pprint(self.toDict())

    def empty(self):
        return not any(self)

        # proper dict subclassing

    def values(self):
        return self._map.values()

        # ipython support

    def __dir__(self):
        return self.keys()

    @classmethod
    def parseOther(self, other):
        if type(other) is DotMap:
            return other._map
        else:
            return other

    def __cmp__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__cmp__(other)

    def __eq__(self, other):
        other = DotMap.parseOther(other)
        if not isinstance(other, dict):
            return False
        return self._map.__eq__(other)

    def __ge__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__ge__(other)

    def __gt__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__gt__(other)

    def __le__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__le__(other)

    def __lt__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__lt__(other)

    def __ne__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__ne__(other)

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

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

    def clear(self):
        self._map.clear()

    def copy(self):
        return DotMap(self.toDict())

    def __copy__(self):
        return self.copy()

    def __deepcopy__(self, memo=None):
        return self.copy()

    def get(self, key, default=None):
        return self._map.get(key, default)

    def has_key(self, key):
        return key in self._map

    def iterkeys(self):
        return self._map.iterkeys()

    def itervalues(self):
        return self._map.itervalues()

    def keys(self):
        return self._map.keys()

    def pop(self, key, default=None):
        return self._map.pop(key, default)

    def popitem(self):
        return self._map.popitem()

    def setdefault(self, key, default=None):
        self._map.setdefault(key, default)

    def update(self, *args, **kwargs):
        if len(args) != 0:
            self._map.update(*args)
        self._map.update(kwargs)

    def viewitems(self):
        return self._map.viewitems()

    def viewkeys(self):
        return self._map.viewkeys()

    def viewvalues(self):
        return self._map.viewvalues()

    @classmethod
    def fromkeys(cls, seq, value=None):
        d = DotMap()
        d._map = OrderedDict.fromkeys(seq, value)
        return d

    def __getstate__(self):
        return self.__dict__

    def __setstate__(self, d):
        self.__dict__.update(d)
Example #15
1
class ListYang(Yang):  # FIXME: to inherit from OrderedDict()
    """
    Class to express list as dictionary 
    """

    def __init__(self, tag, parent=None, type=None):
        super(ListYang, self).__init__(tag, parent)
        self._data = OrderedDict()
        self._type = type

    def get_type(self):
        """
        Returns class which references elements of _data OrderedDict
        :param: -
        :return: Yang subclass
        """
        return self._type

    def set_type(self, type):
        """
        Sets class which references elements of _data OrderedDict
        :param: Yang subclass
        :return: -
        """
        self._type = type

    def keys(self):
        """
        Returns indices of ListYang dictionary 
        :param: -
        :return: list
        """
        return self._data.keys()

    def values(self):
        """
        Returns values of ListYang dictionary
        :param: -
        :return: list
        """
        return self._data.values()

    def iterkeys(self):
        """
        Returns iterator of keys of ListYang dictionary 
        :param: -
        :return: iterator
        """
        return self._data.iterkeys()

    def itervalues(self):
        """
        Returns iterator of values of ListYang dictionary 
        :param: -
        :return: list
        """
        return self._data.itervalues()

    def items(self):
        """
        Returns items of ListYang dictionary 
        :param: -
        :return: list
        """
        return self._data.items()

    def iteritems(self):
        """
        Returns iterator of items of ListYang dictionary 
        :param: -
        :return: list
        """
        return self._data.iteritems()

    def has_key(self, key):  # PEP8 wants it with 'in' instead of 'has_key()'
        """
        Returns if key is in ListYang dictionary 
        :param key: string
        :return: boolean
        """
        return key in self._data.keys()

    def has_value(self, value):
        """
        Returns if value is in ListYang dictionary values
        :param value: string or instance
        :return: boolean
        """
        return value in self._data.values()

    def length(self):
        """
        Returns length of ListYang dictionary
        :param: -
        :return: int
        """
        return len(self._data)

    def is_initialized(self):
        """
        Returns if ListYang dictionary contains elements
        :param: -
        :return: boolean
        """
        if len(self._data) > 0:
            return True
        return False

    def add(self, item):
        """
        add single or a list of items
        :param item: a single ListedYang or a list of ListedYang derivates
        :return: item
        """
        if type(item) is list or type(item) is tuple:
            for i in item:
                if isinstance(i, ListedYang):
                    self.add(i)
                else:
                    raise TypeError("Item must be ListedYang or a list of ListedYang!")
        elif isinstance(item, ListedYang):
            item.set_parent(self)
            self[item.keys()] = item
        else:
            raise TypeError("Item must be ListedYang or a list of ListedYang!")
        return item

    def remove(self, item):
        """
        remove a single element from the list based on a key or a ListedYang
        :param item: key (single or composit) or a ListedYang
        :return: item
        """
        if isinstance(item, ListedYang):
            item = item.keys()
        return self._data.pop(item)

    def _et(self, node, inherited=False, ordered=True):
        """
        Overides Yang method to each ListYang component be defined as SubElement of ElementTree
        :param node: ElementTree
        :return: ElementTree
        """
        if ordered:
            ordered_keys = sorted(self.keys())
            for k in ordered_keys:
                self._data[k]._et(node, ordered)
        else:
            for v in self.values():
                v._et(node, ordered)
        return node
        # for v in self.values():
        #     v._et(node)
        # return node

    def __iter__(self):  # ???
        """
        Returns iterator of ListYang dict
        :param: -
        :return: iterator
        """
        return self._data.__iter__()

    def next(self):
        """
        Go to next element of ListYang dictionary
        :param: -
        :return: -
        """
        self._data.next()

    def __getitem__(self, key):
        """
        Returns ListYang value if key in dictionary
        :param key: string
        :return: instance
        """
        if type(key) is list:
            key = tuple(key)
        if key in self._data.keys():
            return self._data[key]
        else:
            raise KeyError("key not existing")

    def __setitem__(self, key, value):
        """
        Fill ListYang dict with key associated to value
        :param key: string
        :param value: string or instance
        :return: -
        """
        self._data[key] = value
        value.set_parent(self)

    def clear_data(self):
        """
        Clear ListYang dict
        :param: -
        :return: -
        """
        self._data = dict()

    def reduce(self, reference):
        """
        Check if all keys of reference are going to be reduced and erase their values if yes
        :param reference: ListYang
        :return: boolean
        """
        _reduce = True
        for key in self.keys():
            if key in reference.keys():
                if self[key].reduce(reference[key]):
                    self[key].delete()
                else:
                    _reduce = False
            else:
                _reduce = False
        return _reduce

    def __merge__(self, source, execute=False):

        for item in source.keys():
            if item not in self.keys():
                self.add(copy.deepcopy(source[item]))  # it should be a full_copy()
            else:
                if isinstance(self[item], Yang) and type(self[item]) == type(source[item]):
                    # self[item].set_operation(target[item].get_operation())
                    self[item].__merge__(source[item], execute)

    def __eq__(self, other):
        """
        Check if dict of other ListYang is equal 
        :param other: ListYang
        :return: boolean
        """
        if self._data == other._data:
            return True
        return False

    def contains_operation(self, operation):
        """
        Check if any of items have operation set
        :param operation: string
        :return: boolean
        """
        for key in self._data.keys():
            if self._data[key].contains_operation(operation):
                return True
        return False

    def set_operation(self, operation="delete"):
        """
        Set operation for all of items in ListYang dict`
        :param operation: string
        :return: -
        """
        super(ListYang, self).set_operation(operation)
        for key in self._data.keys():
            self._data[key].set_operation(operation)

    def bind(self, relative=False):
        for v in self.values():
            v.bind(relative)
Example #16
0
class FileConfig(MutableMapping):
    """
    Maps key-value pairs to a backing config file.
    You can manually edit the file by modifying self.content.
    """

    def __init__(self, filename, params=[]):
        self.filename = filename
        self.params = OrderedDict()
        for param in params:
            self.add_param(*param)

    def read(self):
        try:
            with open(self.filename, "r") as file:
                self.content = unicode(file.read())
        except IOError as e:
            log.error(e)
            self.content = u""
            # Initialize all params from default values.
            for key in self.params:
                self[key] = self[key]

    def commit(self):
        if not os.path.isfile(self.filename):
            dir = os.path.dirname(self.filename)
            try:
                os.makedirs(dir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise e
        with open(self.filename, "w+") as file:
            # Fix all linebreaks
            file.write(os.linesep.join(self.content.splitlines()))

    def add_param(self, key, handler):
        self.params[key] = handler

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

    def __len__(self):
        return len(self.params)

    def __getitem__(self, key):
        return self.params[key].read(self.content)

    def __setitem__(self, key, value):
        self.content = self.params[key].write(self.content, value)

    def keys(self):
        return self.params.keys()

    def __delitem__(self, key):
        del self.params[key]
Example #17
0
    class View(object):
        """ Presents a view of the set of `Option` arguments to a search command

        TODO: Description

        """

        def __init__(self, command):
            self._items = OrderedDict(
                [
                    (option.name, Option.Item(command, option))
                    for member_name, option in type(command).option_definitions
                ]
            )
            return

        def __contains__(self, name):
            return name in self._items

        def __getitem__(self, name):
            return self._items[name]

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

        def __len__(self):
            return len(self._items)

        def __repr__(self):
            text = "".join(["Option.View(", ",".join([repr(item) for item in self.itervalues()]), ")"])
            return text

        def __str__(self):
            text = " ".join([str(item) for item in self.itervalues() if item.is_set])
            return text

        # region Methods

        def get_missing(self):
            missing = [item.name for item in self._items.itervalues() if item.is_required and not item.is_set]
            return missing if len(missing) > 0 else None

        def iteritems(self):
            return self._items.iteritems()

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

        def itervalues(self):
            return self._items.itervalues()

        def reset(self):
            for value in self.itervalues():
                value.reset()
            return
Example #18
0
class ResultStore(object):
    """
    Result store interface
    """

    def __init__(self):
        """
        Initialize a new :py:class:`ResultStore`
        """
        self.db = OrderedDict()

    def __contains__(self, k):
        """
        Check whether the given key is in the :py:class:`ResultStore`

        Args:
            k (str): key
        Returns:
            bool: whether or not k is in ``self.db``
        """
        return k in self.db

    def __iter__(self):
        """
        Get an iterable over the keys in ``self.db``

        Returns:
            iterable: an iterable over the keys in ``self.db``
        """
        return self.db.__iter__()

    def __setitem__(self, k, v):
        """
        Set a (key, value) pair in ``self.db``

        Args:
            k (str): key
            v (str): value
        """
        return self.db.__setitem__(k, v)

    def itervalues(self):
        """
        Get an iterable over the values in ``self.db``

        Returns:
            iterable: an iterable over the values in ``self.db``
        """
        return self.db.itervalues()
Example #19
0
class Answers(object):
    def __init__(self, raw_answers, aid_to_answer_common):
        self.aid_to_answer = OrderedDict()

        for raw_answer in raw_answers:
            aid = raw_answer["id"]
            self[aid] = Answer(raw_answer)
            aid_to_answer_common.update(self.aid_to_answer)

    def __setitem__(self, key, value):
        self.aid_to_answer[key] = value

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

    def get_all(self):
        return self.aid_to_answer.values()
Example #20
0
class enum(object):
    def __init__(self, *sequential, **named):
        self.__enums = OrderedDict(zip(sequential, range(len(sequential))), **named)
        for key, val in self.__enums.items():
            setattr(self, key, val)

    def __getitem__(self, key):
        return self.__enums[key]

    def items(self):
        return self.__enums.items()

    def keys(self):
        return self.__enums.keys()

    def values(self):
        return self.__enums.values()

    def __iter__(self):
        return self.__enums.__iter__()
Example #21
0
    def relabel(self, keys):
        """ Relabel a collection of keys (a dictionary of old -> new. 
        This does not allow moving things between different directories.
        This code seems useless. The purpose of relabeling is to move around
        files across directories. """

        assert all(self.key_exists(k) and not self.key_exists(v) for k, v in keys.iteritems())
        for old_key in keys.keys():
            new_key = keys[old_key]

            old_parent, old_file = self.__get_parent_dir_and_file(old_key)
            new_parent, new_file = self.__get_parent_dir_and_file(new_key)
            if old_parent != new_parent:
                raise KeyError("key parent directory mismatch:", old_key, " and ", new_key)

            old_index, old_key_file = self.__find_enclosing_index(old_key, False)
            l = old_index._OrderedDict__map[old_file]
            del old_index._OrderedDict__map[old_file]
            old_index._OrderedDict__map[new_file] = l
            l[2] = new_file
            dict.__setitem__(old_index, new_file, dict.pop(old_index, old_file))
        self._list = [k for k in OrderedDict.__iter__(self)]
        self._full_key_list = None
Example #22
0
class SRStats:
    """ This class defines a SeqRecord stats object, that holds statistical 
        information for sequence records related to motifs, wiggle tracks, 
        etc.
    """

    # ===========================================================================
    # GENERAL STATS
    # ===========================================================================

    CALL_STATS = OrderedDict()

    # sr here is the seqrecord
    general = OrderedDict(
        [
            ("chr", lambda sr: sr.annotations["chr"]),
            ("syn_range", lambda sr: "{}:{}".format(sr.annotations["synth_start"], sr.annotations["synth_end"])),
            ("id", lambda sr: sr.id),
            ("up", lambda sr: sr.annotations["synth_us"]),
            ("exon_len", lambda sr: sr.annotations["len"]),
            ("down", lambda sr: sr.annotations["synth_ds"]),
            ("ss_num", lambda sr: len(sr.correct_motifs)),
            ("ss_score", lambda sr: score.score_seq_record(sr)),
        ]
    )

    CALL_STATS["general"] = ([""], general, lambda sr, x: (sr,))

    # ===========================================================================
    # MOTIF STATS
    # ===========================================================================
    # x here is a feature list iterator, sr is seq_record
    motif_attribs = OrderedDict(
        [
            ("sum", lambda sr, x: sum((f.extract_score() for f in sr.get_features(x.type)))),
            ("count", lambda sr, x: sum((1 for f in sr.get_features(x.type)))),
            ("comp", lambda sr, x: sum((1 for f in sr.get_features(x.type, meta=True)))),
            (
                "ratio",
                lambda sr, x: sum((1 for f in sr.get_features(x.type)))
                / (sum((1 for f in sr.get_features(x.type, meta=True))) or float("-inf")),
            ),
            ("max", lambda sr, x: util.max_none((f.extract_score() for f in sr.get_features(x.type, meta=True)))),
        ]
    )

    CALL_STATS["motif"] = (
        sorted(motif.motif_types.values()),  # x values
        motif_attribs,  # stats to get (dict above)
        lambda sr, x: (sr, x),
    )  # how to get

    # ===========================================================================
    # AGGREGATE STATS
    # ===========================================================================
    # ag is aggregate group, sr is seq record
    agg_attribs = OrderedDict(
        {"sum": lambda sr, ag: sum((f.extract_score() for f in sr.get_features(ag, aggregate=True)))}
    )

    CALL_STATS["agg"] = (
        motif.agg_types,  # x values
        agg_attribs,  # stats to get (dict above)
        lambda sr, ag: (sr, ag),
    )  # how to get

    # ===========================================================================
    # WIG STATS
    # ===========================================================================
    # x here is a list of wig arrays for features, sr is seq features
    # TODO: MAKE THIS FOR ALL POTENTIAL TRACKS, NOT JUST MAMCONSERV
    wig_attribs = OrderedDict(
        [
            ("cnsv_avg", lambda sr, x: np.mean([fl for fl in chain(*x)])),
            ("cnsv_f_max", lambda sr, x: util.max_none(np.mean(f) for f in x)),
        ]
    )

    CALL_STATS["wig"] = (
        list(chain(sorted(motif.motif_types.values()), ("exon", "intron"))),
        wig_attribs,
        lambda sr, x: (sr, sr.wigs_for_ftype(x, "MamConserv")),
    )

    # ===========================================================================
    # SNP STATS
    # ===========================================================================

    # x here is a list of snps (variation features), sr is seq_record
    snp_attribs = OrderedDict(
        [
            ("count", lambda sr, x: sum((1 for f in x))),
            ("syn", lambda sr, x: sum(("synonymous_codon" in snp.qualifiers["note"] for snp in x))),
            ("nonsyn", lambda sr, x: sum((has_effect(nonsyn_var, snp) for snp in x))),
            ("intron", lambda sr, x: sum(("intron_variant" in snp.qualifiers["note"] for snp in x))),
            ("splice", lambda sr, x: sum((has_effect(splice_var, snp) for snp in x))),
            ("bad", lambda sr, x: sum((has_effect(bad_effects, snp) for snp in x))),
        ]
    )

    CALL_STATS["snp"] = (
        ["snp"],  # x values (here only one)
        snp_attribs,  # stats to get (dict above)
        lambda sr, x: (sr, sr.get_features("variation")),
    )

    def __init__(self, sr):

        self._stats_odict = OrderedDict()

        sr.stats = self

        for stat_group in SRStats.CALL_STATS:
            stat_actions = SRStats.CALL_STATS[stat_group]
            oloop_list, fxn_odict, get_st_fxn = stat_actions

            self._stats_odict[stat_group] = OrderedDict()

            for i in oloop_list:
                self._stats_odict[stat_group][str(i)] = OrderedDict()

                for st in fxn_odict:
                    lambda_args = get_st_fxn(sr, i)
                    self._stats_odict[stat_group][str(i)][str(st)] = fxn_odict[st](*lambda_args)

    def __getitem__(self, *args):
        return self._stats_odict.__getitem__(*args)

    def __setitem__(self, *args):
        return self._stats_odict.__setitem__(*args)

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

    def __str__(self):

        out_str = ""

        for sc in self:
            for st in self[sc]:
                for stat in self[sc][st]:
                    out_str += "{}\t".format((self[sc][st][stat]))

        return out_str

    def header(self):

        out_str = ""

        for sc in self:
            for st in self[sc]:
                for stat in self[sc][st]:
                    out_str += "_".join([sc, st, stat]) + "\t"

        return out_str

    def __repr__(self):

        out_str = ""

        for sc in self:
            out_str += "{}=============\n".format(sc)
            for st in self[sc]:
                out_str += "\t{}:\n".format(st)
                for stat in self[sc][st]:
                    out_str += "\t\t{}:\t{}\n".format(stat, self[sc][st][stat])

        return out_str
Example #23
0
class VecWrapper(object):
    """
    A dict-like container of a collection of variables.

    Args
    ----
    sysdata : _SysData
        A data object for system level data

    probdata : _ProbData
        A data object for Problem level data that we need in order to store
        flags that span multiple layers in the hierarchy.

    comm : an MPI communicator (real or fake)
        a communicator that can be used for distributed operations
        when running under MPI.  If not running under MPI, it is
        ignored

    Attributes
    ----------
    idx_arr_type : dtype, optional
        A dtype indicating how index arrays are to be represented.
        The value 'i' indicates an numpy integer array, other
        implementations, e.g., petsc, will define this differently.
    """

    idx_arr_type = "i"

    def __init__(self, sysdata, probdata, comm=None):
        self.comm = comm
        self.vec = None
        self._dat = OrderedDict()

        # Automatic unit conversion in target vectors
        self.deriv_units = False

        # Scaling support in source vectors
        self.vectype = None

        # Supports complex step
        self.alloc_complex = False

        self._sysdata = sysdata
        self._probdata = probdata

        self.scale_cache = None
        self.units_cache = None

    def _flat(self, name):
        """
        Return a flat version of the named variable, including any necessary conversions.
        """
        return self._dat[name].flat()

    def metadata(self, name):
        """
        Returns the metadata for the named variable.

        Args
        ----
        name : str
            Name of variable to get the metadata for.

        Returns
        -------
        dict
            The metadata dict for the named variable.

        Raises
        -------
        KeyError
            If the named variable is not in this vector.
        """
        try:
            return self._dat[name].meta
        except KeyError as error:
            raise KeyError("Variable '%s' does not exist" % name)

    def __getitem__(self, name):
        """
        Retrieve unflattened value of named var.

        Args
        ----
        name : str
            Name of variable to get the value for.

        Returns
        -------
            The unflattened value of the named variable.
        """
        return self._dat[name].get()

    def __setitem__(self, name, value):
        """
        Set the value of the named variable.

        Args
        ----
        name : str
            Name of variable to get the value for.

        value :
            The unflattened value of the named variable.
        """
        self._dat[name].set(value)

    def __len__(self):
        """
        Returns
        -------
            The number of keys (variables) in this vector.
        """
        return len(self._dat)

    def __contains__(self, key):
        """
        Returns
        -------
            A boolean indicating if the given key (variable name) is in this vector.
        """

        return key in self._dat

    def __iter__(self):
        """
        Returns
        -------
            A dictionary iterator over the items in _dat.
        """
        return self._dat.__iter__()

    def vec_val_iter(self):
        """
        Returns
        -------
            An iterator over names and values of all variables found in the
            flattened vector, i.e., no pass_by_obj variables.
        """
        return ((n, acc.val) for n, acc in iteritems(self._dat) if not acc.pbo)

    def keys(self):
        """
        Returns
        -------
        list or KeyView (python 3)
            the keys (variable names) in this vector.
        """
        return self._dat.keys()

    def iterkeys(self):
        """
        Returns
        -------
        iter of str
            the keys (variable names) in this vector.
        """
        return iterkeys(self._dat)

    def items(self):
        """
        Returns
        -------
        list of (str, dict)
            List of tuples containing the name and metadata dict for each
            variable.
        """
        return [(name, acc.meta) for name, acc in iteritems(self._dat)]

    def iteritems(self):
        """
        Returns
        -------
        iterator
            Iterator returning the name and metadata dict for each variable.
        """
        return ((name, acc.meta) for name, acc in iteritems(self._dat))

    def values(self):
        """
        Returns
        -------
        list of dict
            List containing metadata dict for each variable.
        """
        return [acc.meta for acc in itervalues(self._dat)]

    def itervalues(self):
        """
        Returns
        -------
        iter of dict
            Iterator yielding metadata dict for each variable.
        """
        return (acc.meta for acc in itervalues(self._dat))

    def _get_local_idxs(self, name, idx_dict, get_slice=False):
        """
        Returns all of the indices for the named variable in this vector.

        Args
        ----
        name : str
            Name of variable to get the indices for.

        get_slice : bool, optional
            If True, return the idxs as a slice object, if possible.

        Returns
        -------
        size
            The size of the named variable.

        ndarray
            Index array containing all local indices for the named variable.
        """
        try:
            slc = self._dat[name].slice
            if slc is None:
                return self.make_idx_array(0, 0)
        except KeyError:
            # this happens if 'name' doesn't exist in this process
            return self.make_idx_array(0, 0)

        start, end = slc

        if name in idx_dict:
            # TODO: possible slice conversion
            idxs = self.to_idx_array(idx_dict[name]) + start
            if idxs.size > (end - start) or max(idxs) >= end:
                raise RuntimeError("Indices of interest specified for '%s'" "are too large" % name)
            return idxs
        else:
            if get_slice:
                return slice(start, end)
            return self.make_idx_array(start, end)

    def norm(self):
        """
        Calculates the norm of this vector.

        Returns
        -------
        float
            Norm of our internal vector.
        """
        return norm(self.vec)

    def get_view(self, system, comm, varmap):
        """
        Return a new `VecWrapper` that is a view into this one.

        Args
        ----
        system : `System`
            System for which the view is being created.

        comm : an MPI communicator (real or fake)
            A communicator that is used in the creation of the view.

        varmap : dict
            Mapping of variable names in the old `VecWrapper` to the names
            they will have in the new `VecWrapper`.

        Returns
        -------
        `VecWrapper`
            A new `VecWrapper` that is a view into this one.
        """
        view = self.__class__(system._sysdata, system._probdata, comm)
        view.alloc_complex = self.alloc_complex
        view.vectype = self.vectype
        view_size = 0

        start = -1

        alloc_complex = self.alloc_complex

        # varmap is ordered, in the same order as _dat
        for name, pname in iteritems(varmap):
            if name in self._dat:
                acc = self._dat[name]
                if acc.pbo or acc.remote:
                    view._dat[pname] = Accessor(view, None, acc.val, acc.meta, self._probdata, alloc_complex)
                else:
                    pstart, pend = acc.slice
                    if start == -1:
                        start = pstart
                        end = pend
                    else:
                        assert pstart == end, "%s not contiguous in block containing %s" % (name, varmap.keys())
                    end = pend
                    meta = acc.meta

                    if alloc_complex:
                        imag_val = acc.imag_val
                    else:
                        imag_val = None

                    view._dat[pname] = Accessor(
                        view,
                        (view_size, view_size + meta["size"]),
                        acc.val,
                        meta,
                        self._probdata,
                        alloc_complex,
                        imag_val=imag_val,
                    )
                    view_size += meta["size"]

        if start == -1:  # no items found
            view.vec = self.vec[0:0]
            if alloc_complex:
                view.imag_vec = self.imag_vec[0:0]

        else:
            view.vec = self.vec[start:end]
            if alloc_complex:
                view.imag_vec = self.imag_vec[start:end]

        return view

    def make_idx_array(self, start, end):
        """
        Return an index vector of the right int type for
        the current implementation.

        Args
        ----
        start : int
            The starting index.

        end : int
            The ending index.

        Returns
        -------
        ndarray of idx_arr_type
            index array containing all indices from start up to but
            not including end
        """
        return numpy.arange(start, end, dtype=self.idx_arr_type)

    def to_idx_array(self, indices):
        """
        Given some iterator of indices, return an index array of the
        right int type for the current implementation.

        Args
        ----
        indices : iterator of ints
            An iterator of indices.

        Returns
        -------
        ndarray of idx_arr_type
            Index array containing all of the given indices.

        """
        return numpy.array(indices, dtype=self.idx_arr_type)

    def merge_idxs(self, idxs):
        """
        Return source and target index arrays, built up from
        smaller index arrays.

        Args
        ----
        idxs : array
            Indices.

        Returns
        -------
        ndarray of idx_arr_type
            Index array containing all of the merged indices.

        """
        if len(idxs) == 0:
            return self.make_idx_array(0, 0)

        return numpy.concatenate(idxs)

    def dump(self, out_stream=sys.stdout):  # pragma: no cover
        """
        Args
        ----
        out_stream : file_like
            Where to send human readable output. Default is sys.stdout. Set to
            None to return a str.
        """

        if out_stream is None:
            out_stream = cStringIO()
            return_str = True
        else:
            return_str = False

        lens = [len(n) for n in self.keys()]
        nwid = max(lens) if lens else 10
        vlens = [len(repr(self[v])) for v in self.keys()]
        vwid = max(vlens) if vlens else 1
        for acc in itervalues(self._dat):
            if acc.pbo:
                defwid = 8
                break
        else:
            defwid = 1

        slens = [
            len("[{0[0]}:{0[1]}]".format(self._dat[v].slice)) for v in self.keys() if self._dat[v].slice is not None
        ] + [defwid]
        swid = max(slens)

        for v, acc in iteritems(self._dat):
            if acc.pbo or acc.remote:
                continue
            if self._dat[v].slice is not None:
                uslice = "[{0[0]}:{0[1]}]".format(self._dat[v].slice)
            else:
                uslice = ""
            template = "{0:<{nwid}} {1:<{swid}} {2:>{vwid}}\n"
            out_stream.write(template.format(v, uslice, repr(self[v]), nwid=nwid, swid=swid, vwid=vwid))

        for v, acc in iteritems(self._dat):
            if acc.pbo and not acc.remote:
                template = "{0:<{nwid}} {1:<{swid}} {2}\n"
                out_stream.write(template.format(v, "(by obj)", repr(self[v]), nwid=nwid, swid=swid))
        if return_str:
            return out_stream.getvalue()
Example #24
0
class Board:
    """
    Attributes:
        cells: a list of all the living pieces
    """

    def __init__(self):
        self.cells = OrderedDict()

    def get_cells(self):
        """Return a list of tuples containing the coordinates of all cells"""
        return self.cells.keys()

    def evolve(self):
        """Advance the board one generation"""
        next_state = OrderedDict()
        new_cells = []
        dead_cells = []

        for loc in self.cells:
            cell = self.cells[loc]
            neighbors = cell.count_neighbors()

            if neighbors in (2, 3):
                next_state[loc] = cell
            else:
                dead_cells.append(loc)

            open_cells = cell.get_open_neighbors()

            for space in open_cells:
                if space in self.cells:
                    continue
                neighbors = self.count_neighbors(space)
                if self.count_neighbors(space) == 3 and space not in new_cells:
                    new_cells.append(space)

        for x, y in dead_cells:
            self.die(x, y)

        self.cells = next_state

        for x, y in new_cells:
            self.birth(x, y)

    def count_neighbors(self, loc):
        """Count how many neighbors an open space has"""
        x = loc[0]
        y = loc[1]
        neighbors = []
        for xtrans in (-1, 0, 1):
            for ytrans in (-1, 0, 1):
                if xtrans != 0 or ytrans != 0:
                    neighbors.append((x + xtrans, y + ytrans))
        count = 0
        for space in neighbors:
            if space in self.cells:
                count += 1
        return count

    def die(self, x, y):
        self.cells[(x, y)].die()
        del self.cells[(x, y)]

    def birth(self, x, y):
        """Add a cell to the board"""
        if (x, y) in self.cells:
            raise ValueError("cell ({}, {}) is already mapped".format(x, y))

        cell = Cell(x, y)
        self.cells[(x, y)] = cell

        for xtrans in range(-1, 2):
            for ytrans in range(-1, 2):
                if xtrans == 0 and ytrans == 0:
                    continue
                loc = (x + xtrans, y + ytrans)
                if loc in self.cells:
                    neighbor = self.cells[loc]
                    cell.connect(neighbor)

    def cell_count():
        """Return the number of living cells"""
        return len(self.cells)

    def __iter__(self):
        """Allow someone to iterate over all the cells"""
        return self.cells.__iter__()

    def __str__(self, minX=-50, maxX=50, minY=-25, maxY=25):
        """Print a quick text representation of the board"""
        avgX = 0
        avgY = 0
        for loc in self.cells:
            avgX += loc[0]
            avgY += loc[1]
        avgX = avgX / len(self.cells)
        avgY = avgY / len(self.cells)

        board = ""
        row = " "
        for x in range(minX, maxX + 1):
            row += "_"
        board += row + " \n"
        for y in range(avgY + maxY, avgY + minY, -1):
            row = "|"
            for x in range(avgX + minX, avgX + maxX + 1):
                if (x, y) in self.cells:
                    row += "#"
                else:
                    row += " "
            board += row + "|\n"
        row = "|"
        for x in range(minX, maxX + 1):
            row += "_"
        board += row + "|"
        return board
Example #25
0
class Changelog:
    def __init__(self, file_location=None, content=None):
        self.data = OrderedDict()
        if content is not None:
            self._parse(content)
        elif file_location is not None:
            with open(file_location, "r") as f:
                self._parse(f.read())

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

    def iteritems(self):
        return self.data.iteritems()

    def get(self, version):
        return self.data.get(version)

    def get_changes(self, start_version, end_version=None):
        versions = self.data.keys()

        end_version_index = 0
        if end_version is not None:
            try:
                end_version_index = versions.index(str(end_version))
            except ValueError:
                raise ValueError("Unknown version %s" % str(end_version))
        try:
            start_version_index = versions.index(str(start_version))
        except ValueError:
            raise ValueError("Unknown version %s" % str(start_version))

        newer_releases = versions[end_version_index:start_version_index]
        changes = []
        for release in newer_releases:
            changes.extend(self.data[release])
        return changes

    def latest(self):
        if self.data.items():
            return self.data.items()[0]
        return None

    def _parse(self, content):
        tree = publish_doctree(content)

        def isValidVersionSection(x):
            if x.tagname == "section":
                try:
                    StrictVersion(x["names"][0].split()[0])
                except (ValueError, IndexError):
                    pass
                else:
                    return True
            return False

        def isListItem(x):
            return x.tagname == "list_item"

        foundSections = tree.traverse(condition=isValidVersionSection)
        for section in foundSections:
            version = section["names"][0].split()[0]
            list_items = section.traverse(condition=isListItem)
            entries = [a.rawsource for a in list_items]
            self.data[version] = entries
Example #26
0
class VecWrapper(object):
    """
    A dict-like container of a collection of variables.

    Args
    ----
    pathname : str, optional
        the pathname of the containing `System`

    comm : an MPI communicator (real or fake)
        a communicator that can be used for distributed operations
        when running under MPI.  If not running under MPI, it is
        ignored

    Attributes
    ----------
    idx_arr_type : dtype, optional
        A dtype indicating how index arrays are to be represented.
        The value 'i' indicates an numpy integer array, other
        implementations, e.g., petsc, will define this differently.
    """

    idx_arr_type = "i"

    def __init__(self, pathname="", comm=None):
        self.pathname = pathname
        self.comm = comm
        self.vec = None
        self._vardict = OrderedDict()
        self._slices = {}

        # add a flat attribute that will have access method consistent
        # with non-flat access  (__getitem__)
        self.flat = _flat_dict(self._vardict)

        # Automatic unit conversion in target vectors
        self.deriv_units = False
        self.adj_accumulate_mode = False

    def metadata(self, name):
        """
        Returns the metadata for the named variable.

        Args
        ----
        name : str
            Name of variable to get the metadata for.

        Returns
        -------
        dict
            The metadata dict for the named variable.

        Raises
        -------
        KeyError
            If the named variable is not in this vector.
        """
        try:
            return self._vardict[name]
        except KeyError as error:
            msg = "Variable '{name}' does not exist".format(name=name)
            raise KeyError(msg)

    def _setup_prom_map(self):
        """
        Sets up the internal dict that maps absolute name to promoted name.
        """
        self._to_prom_name = {}
        for prom_name, meta in iteritems(self):
            self._to_prom_name[meta["pathname"]] = prom_name

    def __getitem__(self, name):
        """
        Retrieve unflattened value of named var.

        Args
        ----
        name : str
            Name of variable to get the value for.

        Returns
        -------
            The unflattened value of the named variable.
        """
        meta = self.metadata(name)

        if "pass_by_obj" in meta and meta["pass_by_obj"]:
            return meta["val"].val

        # For dparam vector, getitem is disabled in adjoint mode.
        if self.adj_accumulate_mode:
            return numpy.zeros((meta["shape"]))
        elif "unit_conv" in meta:
            # Convert units
            scale, offset = meta["unit_conv"]

            # Gradient is just the scale
            if self.deriv_units:
                offset = 0.0

            shape = meta["shape"]
            # if shape is 1, it's a float
            if shape == 1:
                return scale * (meta["val"][0] + offset)
            else:
                return scale * (meta["val"].reshape(shape) + offset)
        else:
            shape = meta["shape"]
            # if shape is 1, it's a float
            if shape == 1:
                return meta["val"][0]
            else:
                return meta["val"].reshape(shape)

    def __setitem__(self, name, value):
        """
        Set the value of the named variable.

        Args
        ----
        name : str
            Name of variable to get the value for.

        value :
            The unflattened value of the named variable.
        """
        meta = self.metadata(name)

        if "pass_by_obj" in meta and meta["pass_by_obj"]:
            meta["val"].val = value
            return

        unitconv = meta.get("unit_conv")

        # For dparam vector in adjoint mode, assignement behaves as +=.
        if self.adj_accumulate_mode is True:
            if self.deriv_units and unitconv:
                scale, offset = unitconv

                if isinstance(value, numpy.ndarray):
                    meta["val"][:] += scale * value.flat[:]
                else:
                    meta["val"][0] += scale * value
            else:
                if isinstance(value, numpy.ndarray):
                    meta["val"][:] += value.flat[:]
                else:
                    meta["val"][0] += value

        # Convert Units
        else:
            if self.deriv_units and unitconv:
                scale, offset = unitconv

                if isinstance(value, numpy.ndarray):
                    meta["val"][:] = scale * value.flat[:]
                else:
                    meta["val"][0] = scale * value
            else:
                if isinstance(value, numpy.ndarray):
                    meta["val"][:] = value.flat[:]
                else:
                    meta["val"][0] = value

    def __len__(self):
        """
        Returns
        -------
            The number of keys (variables) in this vector.
        """
        return len(self._vardict)

    def __contains__(self, key):
        """
        Returns
        -------
            A boolean indicating if the given key (variable name) is in this vector.
        """

        return key in self._vardict

    def __iter__(self):
        """
        Returns
        -------
            A dictionary iterator over the items in _vardict.
        """
        return self._vardict.__iter__()

    def keys(self):
        """
        Returns
        -------
        list or KeyView (python 3)
            the keys (variable names) in this vector.
        """
        return self._vardict.keys()

    def iterkeys(self):
        """
        Returns
        -------
        iter of str
            the keys (variable names) in this vector.
        """
        return iterkeys(self._vardict)

    def items(self):
        """
        Returns
        -------
        list of (str, dict)
            List of tuples containing the name and metadata dict for each
            variable.
        """
        return self._vardict.items()

    def iteritems(self):
        """
        Returns
        -------
        iterator
            Iterator returning the name and metadata dict for each variable.
        """
        return iteritems(self._vardict)

    def values(self):
        """
        Returns
        -------
        list of dict
            List containing metadata dict for each variable.
        """
        return self._vardict.values()

    def itervalues(self):
        """
        Returns
        -------
        iter of dict
            Iterator yielding metadata dict for each variable.
        """
        return self._vardict.values()

    def get_local_idxs(self, name, idx_dict):
        """
        Returns all of the indices for the named variable in this vector.

        Args
        ----
        name : str
            Name of variable to get the indices for.

        Returns
        -------
        size
            The size of the named variable.

        ndarray
            Index array containing all local indices for the named variable.
        """
        # TODO: add support for returning slice objects

        meta = self._vardict[name]
        if meta.get("pass_by_obj"):
            raise RuntimeError("No vector indices can be provided " "for 'pass by object' variable '%s'" % name)

        if name not in self._slices:
            return meta["size"], self.make_idx_array(0, 0)

        start, end = self._slices[name]
        if name in idx_dict:
            idxs = self.to_idx_array(idx_dict[name]) + start
            if idxs.size > (end - start) or max(idxs) >= end:
                raise RuntimeError("Indices of interest specified for '%s'" "are too large" % name)
            return idxs.size, idxs
        else:
            return meta["size"], self.make_idx_array(start, end)

    def norm(self):
        """
        Calculates the norm of this vector.

        Returns
        -------
        float
            Norm of our internal vector.
        """
        return norm(self.vec)

    def get_view(self, sys_pathname, comm, varmap):
        """
        Return a new `VecWrapper` that is a view into this one.

        Args
        ----
        sys_pathname : str
            pathname of the system for which the view is being created.

        comm : an MPI communicator (real or fake)
            A communicator that is used in the creation of the view.

        varmap : dict
            Mapping of variable names in the old `VecWrapper` to the names
            they will have in the new `VecWrapper`.

        Returns
        -------
        `VecWrapper`
            A new `VecWrapper` that is a view into this one.
        """
        view = self.__class__(sys_pathname, comm)
        view_size = 0

        vardict = self._vardict
        start = -1

        # varmap is ordered, in the same order as vardict
        for name, pname in iteritems(varmap):
            if name in vardict:
                meta = vardict[name]
                view._vardict[pname] = meta
                if not meta.get("pass_by_obj") and not meta.get("remote"):
                    pstart, pend = self._slices[name]
                    if start == -1:
                        start = pstart
                        end = pend
                    else:
                        assert pstart == end, "%s not contiguous in block containing %s" % (name, varmap.keys())
                    end = pend
                    view._slices[pname] = (view_size, view_size + meta["size"])
                    view_size += meta["size"]

        if start == -1:  # no items found
            view.vec = self.vec[0:0]
        else:
            view.vec = self.vec[start:end]

        view._setup_prom_map()

        return view

    def make_idx_array(self, start, end):
        """
        Return an index vector of the right int type for
        the current implementation.

        Args
        ----
        start : int
            The starting index.

        end : int
            The ending index.

        Returns
        -------
        ndarray of idx_arr_type
            index array containing all indices from start up to but
            not including end
        """
        return numpy.arange(start, end, dtype=self.idx_arr_type)

    def to_idx_array(self, indices):
        """
        Given some iterator of indices, return an index array of the
        right int type for the current implementation.

        Args
        ----
        indices : iterator of ints
            An iterator of indices.

        Returns
        -------
        ndarray of idx_arr_type
            Index array containing all of the given indices.

        """
        return numpy.array(indices, dtype=self.idx_arr_type)

    def merge_idxs(self, src_idxs, tgt_idxs):
        """
        Return source and target index arrays, built up from
        smaller index arrays and combined in order of ascending source
        index (to allow us to convert src indices to a slice in some cases).

        Args
        ----
        src_idxs : array
            Source indices.

        tgt_idxs : array
            Target indices.

        Returns
        -------
        ndarray of idx_arr_type
            Index array containing all of the merged indices.

        """
        assert len(src_idxs) == len(tgt_idxs)

        # filter out any zero length idx array entries
        src_idxs = [i for i in src_idxs if len(i)]
        tgt_idxs = [i for i in tgt_idxs if len(i)]

        if len(src_idxs) == 0:
            return self.make_idx_array(0, 0), self.make_idx_array(0, 0)

        src_tups = list(enumerate(src_idxs))

        src_sorted = sorted(src_tups, key=lambda x: x[1].min())

        new_src = [idxs for i, idxs in src_sorted]
        new_tgt = [tgt_idxs[i] for i, _ in src_sorted]

        return idx_merge(new_src), idx_merge(new_tgt)

    def get_promoted_varname(self, abs_name):
        """
        Returns the relative pathname for the given absolute variable
        pathname.

        Args
        ----
        abs_name : str
            Absolute pathname of a variable.

        Returns
        -------
        rel_name : str
            Relative name mapped to the given absolute pathname.
        """
        try:
            return self._to_prom_name[abs_name]
        except KeyError:
            raise KeyError("Relative name not found for variable '%s'" % abs_name)

    def get_states(self):
        """
        Returns
        -------
        list
            A list of names of state variables.
        """
        return [n for n, meta in iteritems(self._vardict) if meta.get("state")]

    def get_vecvars(self):
        """
        Returns
        -------
            A list of names of variables found in our 'vec' array.
        """
        return [(n, meta) for n, meta in iteritems(self._vardict) if not meta.get("pass_by_obj")]

    def get_byobjs(self):
        """
        Returns
        -------
        list
            A list of names of variables that are passed by object rather than
            through scattering entries from one array to another.
        """
        return [(n, meta) for n, meta in iteritems(self._vardict) if meta.get("pass_by_obj")]

    def _scoped_abs_name(self, name):
        """
        Args
        ----
        name : str
            The absolute pathname of a variable.

        Returns
        -------
        str
            The given name as seen from the 'scope' of the `System` that
            contains this `VecWrapper`.
        """
        if self.pathname:
            start = len(self.pathname) + 1
        else:
            start = 0
        return name[start:]

    def dump(self, out_stream=sys.stdout):
        """
        Args
        ----
        out_stream : file_like
            Where to send human readable output. Default is sys.stdout. Set to
            None to return a str.
        """

        if out_stream is None:
            out_stream = cStringIO()
            return_str = True
        else:
            return_str = False

        lens = [len(n) for n in self.keys()]
        nwid = max(lens) if lens else 10
        vlens = [len(repr(self[v])) for v in self.keys()]
        vwid = max(vlens) if vlens else 1
        if len(self.get_vecvars()) != len(self):  # we have some pass by obj
            defwid = 8
        else:
            defwid = 1
        slens = [len("[{0[0]}:{0[1]}]".format(self._slices[v])) for v in self.keys() if v in self._slices] + [defwid]
        swid = max(slens)

        for v, meta in iteritems(self):
            if meta.get("pass_by_obj") or meta.get("remote"):
                continue
            if v in self._slices:
                uslice = "[{0[0]}:{0[1]}]".format(self._slices[v])
            else:
                uslice = ""
            template = "{0:<{nwid}} {1:<{swid}} {2:>{vwid}}\n"
            out_stream.write(template.format(v, uslice, repr(self[v]), nwid=nwid, swid=swid, vwid=vwid))

        for v, meta in iteritems(self):
            if meta.get("pass_by_obj") and not meta.get("remote"):
                template = "{0:<{nwid}} {1:<{swid}} {2}\n"
                out_stream.write(template.format(v, "(by obj)", repr(self[v]), nwid=nwid, swid=swid))
        if return_str:
            return out_stream.getvalue()
Example #27
0
class VecWrapper(object):
    """
    A dict-like container of a collection of variables.

    Args
    ----
    pathname : str, optional
        the pathname of the containing `System`

    comm : an MPI communicator (real or fake)
        a communicator that can be used for distributed operations
        when running under MPI.  If not running under MPI, it is
        ignored

    Attributes
    ----------
    idx_arr_type : dtype, optional
        A dtype indicating how index arrays are to be represented.
        The value 'i' indicates an numpy integer array, other
        implementations, e.g., petsc, will define this differently.
    """

    idx_arr_type = "i"

    def __init__(self, sysdata, comm=None):
        self.comm = comm
        self.vec = None
        self._vardict = OrderedDict()
        self._slices = {}
        self.flat = None

        # Automatic unit conversion in target vectors
        self.deriv_units = False

        self._sysdata = sysdata

    def _flat(self, name):
        """
        Return a flat version of the named variable, including any necessary conversions.
        """
        acc = self._access[name]
        return acc.flat(acc.meta)

    def metadata(self, name):
        """
        Returns the metadata for the named variable.

        Args
        ----
        name : str
            Name of variable to get the metadata for.

        Returns
        -------
        dict
            The metadata dict for the named variable.

        Raises
        -------
        KeyError
            If the named variable is not in this vector.
        """
        try:
            return self._vardict[name]
        except KeyError as error:
            msg = "Variable '{name}' does not exist".format(name=name)
            raise KeyError(msg)

    def _setup_prom_map(self):
        """
        Sets up the internal dict that maps absolute name to promoted name.
        """
        to_prom_name = self._sysdata._to_prom_name
        to_top = self._sysdata._to_top_prom_name

        for prom_name, meta in iteritems(self):
            to_prom_name[meta["pathname"]] = prom_name
            to_top[prom_name] = meta["top_promoted_name"]

    def __getitem__(self, name):
        """
        Retrieve unflattened value of named var.

        Args
        ----
        name : str
            Name of variable to get the value for.

        Returns
        -------
            The unflattened value of the named variable.
        """
        acc = self._access[name]
        return acc.get(acc.meta)

    def __setitem__(self, name, value):
        """
        Set the value of the named variable.

        Args
        ----
        name : str
            Name of variable to get the value for.

        value :
            The unflattened value of the named variable.
        """
        acc = self._access[name]
        acc.set(acc.meta, value)

    def __len__(self):
        """
        Returns
        -------
            The number of keys (variables) in this vector.
        """
        return len(self._vardict)

    def __contains__(self, key):
        """
        Returns
        -------
            A boolean indicating if the given key (variable name) is in this vector.
        """

        return key in self._vardict

    def __iter__(self):
        """
        Returns
        -------
            A dictionary iterator over the items in _vardict.
        """
        return self._vardict.__iter__()

    def keys(self):
        """
        Returns
        -------
        list or KeyView (python 3)
            the keys (variable names) in this vector.
        """
        return self._vardict.keys()

    def iterkeys(self):
        """
        Returns
        -------
        iter of str
            the keys (variable names) in this vector.
        """
        return iterkeys(self._vardict)

    def items(self):
        """
        Returns
        -------
        list of (str, dict)
            List of tuples containing the name and metadata dict for each
            variable.
        """
        return self._vardict.items()

    def iteritems(self):
        """
        Returns
        -------
        iterator
            Iterator returning the name and metadata dict for each variable.
        """
        return iteritems(self._vardict)

    def values(self):
        """
        Returns
        -------
        list of dict
            List containing metadata dict for each variable.
        """
        return self._vardict.values()

    def itervalues(self):
        """
        Returns
        -------
        iter of dict
            Iterator yielding metadata dict for each variable.
        """
        return self._vardict.values()

    def _get_local_idxs(self, name, idx_dict, get_slice=False):
        """
        Returns all of the indices for the named variable in this vector.

        Args
        ----
        name : str
            Name of variable to get the indices for.

        get_slice : bool, optional
            If True, return the idxs as a slice object, if possible.

        Returns
        -------
        size
            The size of the named variable.

        ndarray
            Index array containing all local indices for the named variable.
        """
        try:
            start, end = self._slices[name]
        except KeyError:
            # this happens if 'name' doesn't exist in this process
            return self.make_idx_array(0, 0)

        if name in idx_dict:
            # TODO: possible slice conversion
            idxs = self.to_idx_array(idx_dict[name]) + start
            if idxs.size > (end - start) or max(idxs) >= end:
                raise RuntimeError("Indices of interest specified for '%s'" "are too large" % name)
            return idxs
        else:
            if get_slice:
                return slice(start, end)
            return self.make_idx_array(start, end)

    def norm(self):
        """
        Calculates the norm of this vector.

        Returns
        -------
        float
            Norm of our internal vector.
        """
        return norm(self.vec)

    def get_view(self, system, comm, varmap):
        """
        Return a new `VecWrapper` that is a view into this one.

        Args
        ----
        system : `System`
            System for which the view is being created.

        comm : an MPI communicator (real or fake)
            A communicator that is used in the creation of the view.

        varmap : dict
            Mapping of variable names in the old `VecWrapper` to the names
            they will have in the new `VecWrapper`.

        Returns
        -------
        `VecWrapper`
            A new `VecWrapper` that is a view into this one.
        """
        view = self.__class__(system._sysdata, comm)
        view_size = 0

        vardict = self._vardict
        start = -1

        # varmap is ordered, in the same order as vardict
        for name, pname in iteritems(varmap):
            if name in vardict:
                meta = vardict[name]
                view._vardict[pname] = meta
                if not meta.get("pass_by_obj") and not meta.get("remote"):
                    pstart, pend = self._slices[name]
                    if start == -1:
                        start = pstart
                        end = pend
                    else:
                        assert pstart == end, "%s not contiguous in block containing %s" % (name, varmap.keys())
                    end = pend
                    view._slices[pname] = (view_size, view_size + meta["size"])
                    view_size += meta["size"]

        if start == -1:  # no items found
            view.vec = self.vec[0:0]
        else:
            view.vec = self.vec[start:end]

        view._setup_prom_map()
        view.setup_flat()
        view._setup_access_functs()

        return view

    def make_idx_array(self, start, end):
        """
        Return an index vector of the right int type for
        the current implementation.

        Args
        ----
        start : int
            The starting index.

        end : int
            The ending index.

        Returns
        -------
        ndarray of idx_arr_type
            index array containing all indices from start up to but
            not including end
        """
        return numpy.arange(start, end, dtype=self.idx_arr_type)

    def to_idx_array(self, indices):
        """
        Given some iterator of indices, return an index array of the
        right int type for the current implementation.

        Args
        ----
        indices : iterator of ints
            An iterator of indices.

        Returns
        -------
        ndarray of idx_arr_type
            Index array containing all of the given indices.

        """
        return numpy.array(indices, dtype=self.idx_arr_type)

    def merge_idxs(self, idxs):
        """
        Return source and target index arrays, built up from
        smaller index arrays.

        Args
        ----
        idxs : array
            Indices.

        Returns
        -------
        ndarray of idx_arr_type
            Index array containing all of the merged indices.

        """
        if len(idxs) == 0:
            return self.make_idx_array(0, 0)

        return numpy.concatenate(idxs)

    def get_promoted_varname(self, abs_name):
        """
        Returns the relative pathname for the given absolute variable
        pathname.

        Args
        ----
        abs_name : str
            Absolute pathname of a variable.

        Returns
        -------
        rel_name : str
            Relative name mapped to the given absolute pathname.
        """
        try:
            return self._sysdata._to_prom_name[abs_name]
        except KeyError:
            raise KeyError("Relative name not found for variable '%s'" % abs_name)

    def get_states(self):
        """
        Returns
        -------
        list
            A list of names of state variables.
        """
        return [n for n, meta in iteritems(self._vardict) if meta.get("state")]

    def _get_vecvars(self):
        """
        Returns
        -------
            A list of names of variables found in our 'vec' array. This includes
            params that are not 'owned' and remote vars, which have size 0 array values.
        """
        return ((n, meta) for n, meta in iteritems(self._vardict) if not meta.get("pass_by_obj"))

    def setup_flat(self):
        """
        Provides a quick way to iterate over vector subviews.

        Returns
        -------
        A list of (name, array) for each local vector variable.
        """
        if self.flat is None:
            self.flat = OrderedDict([(n, m["val"]) for n, m in self._get_vecvars()])
        return self.flat

    def get_byobjs(self):
        """
        Returns
        -------
        list
            A list of names of variables that are passed by object rather than
            through scattering entries from one array to another.
        """
        return [(n, meta) for n, meta in iteritems(self._vardict) if meta.get("pass_by_obj")]

    def _scoped_abs_name(self, name):
        """
        Args
        ----
        name : str
            The absolute pathname of a variable.

        Returns
        -------
        str
            The given name as seen from the 'scope' of the `System` that
            contains this `VecWrapper`.
        """
        if self._sysdata.pathname:
            start = len(self._sysdata.pathname) + 1
        else:
            start = 0
        return name[start:]

    def dump(self, out_stream=sys.stdout):  # pragma: no cover
        """
        Args
        ----
        out_stream : file_like
            Where to send human readable output. Default is sys.stdout. Set to
            None to return a str.
        """

        if out_stream is None:
            out_stream = cStringIO()
            return_str = True
        else:
            return_str = False

        lens = [len(n) for n in self.keys()]
        nwid = max(lens) if lens else 10
        vlens = [len(repr(self[v])) for v in self.keys()]
        vwid = max(vlens) if vlens else 1
        if len(self.flat) != len(self):  # we have some pass by obj
            defwid = 8
        else:
            defwid = 1
        slens = [len("[{0[0]}:{0[1]}]".format(self._slices[v])) for v in self.keys() if v in self._slices] + [defwid]
        swid = max(slens)

        for v, meta in iteritems(self):
            if meta.get("pass_by_obj") or meta.get("remote"):
                continue
            if v in self._slices:
                uslice = "[{0[0]}:{0[1]}]".format(self._slices[v])
            else:
                uslice = ""
            template = "{0:<{nwid}} {1:<{swid}} {2:>{vwid}}\n"
            out_stream.write(template.format(v, uslice, repr(self[v]), nwid=nwid, swid=swid, vwid=vwid))

        for v, meta in iteritems(self):
            if meta.get("pass_by_obj") and not meta.get("remote"):
                template = "{0:<{nwid}} {1:<{swid}} {2}\n"
                out_stream.write(template.format(v, "(by obj)", repr(self[v]), nwid=nwid, swid=swid))
        if return_str:
            return out_stream.getvalue()

    def _setup_get_funct(self, name):
        """
        Returns a tuple of efficient closures (nonflat and flat) to access
        the named value.
        """

        meta = self._vardict[name]
        val = meta["val"]
        flatfunc = None

        if meta.get("remote"):
            return _remote_access_error, _remote_access_error

        if meta.get("pass_by_obj"):
            return _get_pbo, flatfunc

        shape = meta["shape"]
        scale, offset = meta.get("unit_conv", (None, None))
        if self.deriv_units:
            offset = 0.0
        is_scalar = shape == 1
        if is_scalar:
            shapes_same = True
        else:
            shapes_same = shape == val.shape

        # No unit conversion.
        # dparams vector does no unit conversion.
        if scale is None or self.deriv_units is True:
            flatfunc = _get_arr
            if is_scalar:
                func = _get_scalar
            elif shapes_same:
                func = flatfunc
            else:
                func = _get_arr_diff_shape

        # We have a unit conversion
        else:
            flatfunc = _get_arr_units
            if is_scalar:
                func = _get_scalar_units
            elif shapes_same:
                func = flatfunc
            else:
                func = _get_arr_units_diff_shape

        return func, flatfunc

    def _setup_set_funct(self, name):
        """ Sets up our fast set functions."""

        meta = self._vardict[name]

        if meta.get("remote"):
            return _remote_access_error
        elif "pass_by_obj" in meta and meta["pass_by_obj"]:
            return _set_pbo
        else:
            if meta["shape"] == 1:
                return _set_scalar
            else:
                return _set_arr

    def _setup_access_functs(self):
        self._access = {}
        for name in self:
            func, flatfunc = self._setup_get_funct(name)
            setfunc = self._setup_set_funct(name)
            self._access[name] = Accessor(func, setfunc, flatfunc, self._vardict[name])
Example #28
0
 def __iter__(self):
     for k in OrderedDict.__iter__(self):
         yield self.__map[k]
Example #29
0
class ParameterDictionary(AbstractIdentifiable):
    """
    Contains a set of ParameterContext objects keyed by name

    The ParameterDictionary is used to compose both Streams (specifically Data or Parameter Streams) as well as the
    internal Dataset (via the Coverage API).
    """

    def __init__(self, contexts=None):
        """
        Construct a new ParameterDictionary

        @param contexts an iterable collection of ParameterContext objects to add to the ParameterDictionary
        """
        AbstractIdentifiable.__init__(self)
        self._map = OrderedDict()
        self.__count = 0
        self.temporal_parameter_name = None

        if not contexts is None and hasattr(contexts, "__iter__"):
            for pc in contexts:
                if isinstance(pc, ParameterContext):
                    self.add_context(pc)

    def add_context(self, param_ctxt, is_temporal=False):
        """
        Add a ParameterContext

        @param param_ctxt The ParameterContext object to add
        @param is_temporal  If this ParameterContext should be used as the temporal parameter
        """
        if not isinstance(param_ctxt, ParameterContext):
            raise TypeError("param_ctxt must be a ParameterContext object")

        # CBM TODO: Fix this logic - never reaches "param_ctxt.axis = None",
        # that line and "if claims_time:" should replace the "raise NameError" which should be turned into a warning
        claims_time = param_ctxt.axis == AxisTypeEnum.TIME
        if is_temporal or claims_time:
            if self.temporal_parameter_name is None:
                self.temporal_parameter_name = param_ctxt.name
            else:
                raise NameError(
                    "This dictionary already has a parameter designated as 'temporal': %s", self.temporal_parameter_name
                )
            param_ctxt.axis = AxisTypeEnum.TIME
        else:
            if claims_time:
                param_ctxt.axis = None

        self.__count += 1
        self._map[param_ctxt.name] = (self.__count, param_ctxt)

    def get_context(self, param_name):
        """
        Retrieve a ParameterContext by name

        @param param_name   The name of the ParameterContext
        @returns    The ParameterContext at key 'param_name'
        @throws KeyError    If 'param_name' is not found within the object
        """
        if not param_name in self._map:
            raise KeyError("The ParameterDictionary does not contain the specified key '{0}'".format(param_name))

        return self._map[param_name][1]

    def get_temporal_context(self):
        if self.temporal_parameter_name is None:
            return self._map[self.temporal_parameter_name][1]
        else:
            raise KeyError("This dictionary does not have a parameter designated as 'temporal'")

    def get_context_by_ord(self, ordinal):
        """
        Retrieve a ParameterContext by ordinal

        @param ordinal   The ordinal of the ParameterContext
        @returns    The ParameterContext with the ordinal 'ordinal'
        @throws KeyError    A parameter with the provided ordinal does not exist
        """
        return self.get_context(self.key_from_ord(ordinal))

    def ord_from_key(self, param_name):
        """
        Retrieve the ordinal for a ParameterContext by name

        @param param_name   The name of the ParameterContext
        @returns    The ordinal of the ParameterContext at key 'param_name'
        @throws KeyError    If 'param_name' is not found within the object
        """
        if not param_name in self._map:
            raise KeyError("The ParameterDictionary does not contain the specified key '{0}'".format(param_name))

        return self._map[param_name][0]

    def key_from_ord(self, ordinal):
        """
        Retrieve the parameter name for an ordinal

        @param ordinal   The ordinal of the ParameterContext
        @returns    The parameter name for the provided ordinal
        @throws KeyError    A parameter with the provided ordinal does not exist
        """
        for k, v in self.iteritems():
            if v[0] == ordinal:
                return k

        raise KeyError("Ordinal '{0}' not found in ParameterDictionary".format(ordinal))

    def _todict(self):
        """
        Overrides Dictable._todict() to properly handle ordinals
        """
        # CBM TODO: try/except this to inform more pleasantly if it bombs
        res = dict((k, (v[0], v[1]._todict())) for k, v in self._map.iteritems())
        res.update(
            (k, v._todict() if hasattr(v, "_todict") else v) for k, v in self.__dict__.iteritems() if k != "_map"
        )
        res["cm_type"] = (self.__module__, self.__class__.__name__)
        return res

    @classmethod
    def _fromdict(cls, cmdict, arg_masks=None):
        """
        Overrides Dictable._fromdict() to properly handle ordinals
        """
        # CBM TODO: try/except this to inform more pleasantly if it bombs
        ret = ParameterDictionary()
        if isinstance(cmdict, dict):
            d = cmdict.copy()
            _ = d.pop("cm_type")
            for k, v in d.iteritems():
                if (
                    isinstance(v, (tuple, list))
                    and len(v) == 2
                    and isinstance(v[0], int)
                    and isinstance(v[1], dict)
                    and "cm_type" in v[1]
                ):
                    pc = ParameterContext._fromdict(v[1])
                    ret._map[pc.name] = (v[0], pc)
                elif isinstance(v, dict) and "cm_type" in v:  # CBM TODO: Don't think we ever get here?!
                    ms, cs = v["cm_type"]
                    module = __import__(ms, fromlist=[cs])
                    classobj = getattr(module, cs)
                    setattr(ret, k, classobj._fromdict(v))
                else:
                    setattr(ret, k, v)

        return ret

    def size(self):
        return self.__count

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

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

    def iteritems(self):
        return self._map.iteritems()

    def itervalues(self):
        return self._map.itervalues()

    def keys(self):
        return self._map.keys()

    def compare(self, other):
        """
        Performs a cross-wise comparison of the two ParameterDictionary objects.  Each member of self is compared to each member of other.

        @param other    A ParameterDictionary instance to compare to self
        @returns    A dictionary with keys from self and lists of matching keys from other
        """
        if not isinstance(other, ParameterDictionary):
            raise TypeError("'other' must be an instance of ParameterDictionary")

        ret = {}
        other_keys = other.keys()
        for ks, vs in self.iteritems():
            log.debug("Checking self key '%s'", ks)
            loop = True
            ret[ks] = []
            if ks in other_keys:
                log.debug("Found key '%s' in other, comparing values", ks)
                vo = other.get_context(ks)
                if vs[1] == vo:
                    log.debug("Value of '%s' in self == value of '%s' in other", ks, ks)
                    ret[ks].append(ks)
                    del other_keys[other_keys.index(ks)]
                    loop = False
            if loop:
                for ko in list(other_keys):
                    vo = other.get_context(ko)
                    if vs[1] == vo:
                        log.debug("Value of '%s' in self == value of '%s' in other", ks, ko)
                        ret[ks].append(ks)
                        del other_keys[other_keys.index(ks)]

        # Back-check any leftover keys
        log.debug("Back-checking leftover keys from other")
        for ko in list(other_keys):
            for ks, vs in self.iteritems():
                vo = other.get_context(ko)
                if vs[1] == vo:
                    log.debug("Value of '%s' in self == value of '%s' in other", ks, ko)
                    ret[ks].append(ko)
                    del other_keys[other_keys.index(ko)]

        ret[None] = other_keys

        return ret

    def __eq__(self, other):
        try:
            res = self.compare(other)
            for k, v in res.iteritems():
                if k is None:
                    if len(v) > 0:
                        return False
                elif len(v) != 1 or k != v[0]:
                    return False

            return True
        except TypeError:
            return False

    def __ne__(self, other):
        return not self == other
Example #30
0
class Stack(object):
    """ Base class to store pandas objects, with special operations to
    return as 3d data (eg panel) and to apply functions itemwise.  Items are
    stored in an ordered dict."""

    itemlabel = "Item"

    _magic = ["__len__", "__iter__", "__reversed__", "__contains__"]

    def __init__(self, data, keys=None, name="", sort_items=False):
        self.name = name

        # Dictionary input
        if isinstance(data, dict):
            logger.debug('Initializing "%s" from dictionary.' % self.full_name)
            if sort_items:
                logger.debug("Sorting keys")
                self._data = OrderedDict(sorted(data.keys(), key=lambda t: t[0]))

            else:
                self._data = OrderedDict(data)

        else:

            if not isinstance(data, Iterable):
                logger.info("%s constructed from non-iterable... converting " "data to an iterable" % self.full_name)
                data = [data]

            if keys:
                if not isinstance(keys, Iterable):
                    logger.info(
                        "%s constructed from non-iterable... converting " "keys to an iterable" % self.full_name
                    )
                    keys = [keys]

                if len(keys) != len(data):
                    raise ValueError("Length mistmatch: keys and data (%s,%s)" % (len(keys), len(data)))

            # If keys not passed, generate them
            else:
                # Zipped data ((key, df), (key, df))
                try:
                    keys, data = zip(*data)
                except Exception:
                    keys = self._gen_keys(len(data))
                    if len(keys) > 1:
                        logger.warn("Generating keys %s-%s" % (keys[0], keys[-1]))
                    else:
                        logger.warn("Generating key %s" % keys[0])

            self._data = OrderedDict([(key, data[i]) for (i, key) in enumerate(keys)])

    @property
    def _address(self):
        """ Property to make easily accesible by multicanvas """
        return mem_address(super(Stack, self).__repr__())

    def _gen_keys(self, length):
        """ Return a list of itemlables (item0, item1 etc...) using
            self.itemlabel and a length"""

        logger.debug("Items not found on %s: generating item list" % self.full_name)
        return [self.itemlabel + str(i) for i in range(length)]

    # --------------------
    # Dictionary Interface
    def __getitem__(self, keyslice):
        """ If single name, used dict interface.  If slice or integer, uses 
        list interface.  All results parameterized to key, data pairs, passed
        directly into a new Stack.
        """
        # Slice as list of strings or int [0, 'foo', 2, 'bar']
        if hasattr(keyslice, "__iter__"):

            tuples_out = []
            for item in keyslice:
                if isinstance(item, str):
                    item = self._data.keys().index(item)
                tuples_out.append(self._data.items()[item])

        else:
            if isinstance(keyslice, int) or isinstance(keyslice, slice):
                tuples_out = self._data.items()[keyslice]
            else:
                tuples_out = [(keyslice, self._data[keyslice])]  # keyslice is name

        # If single item, return TimeSpectra, else, return new Stack
        # Canonical slicing implementaiton; don't change unless good reason
        # Because len() wonky with nested tuples (eg (x,y) and [(x1,y1),(x2,y2)]
        # are both length two, this will work:

        if sum(1 for x in tuples_out) == 2:
            return tuples_out[1]  # Return timespectra
        else:
            return self.__class__(tuples_out)

    def __delitem__(self, keyslice):
        """ Delete a single name, or a keyslice from names/canvas """

        if isinstance(keyslice, str):
            idx = self.names.index(keyslice)
            self.pop(idx)
        else:
            raise NotImplementedError("Deletion only supports single entry")

    def __setitem__(self, name, canvas):
        """ """
        if name in self.names:
            idx = self.names.index(name)
            self.pop(idx)
            self.insert(idx, name, canvas)

        else:
            self.names.append(name)

    def __getattr__(self, attr):
        """ If attribute not found, try attribute lookup in dictionary.  If
        that is not found, try finding attribute on self._data.
        
        For example, self.keys() will first look for self['keys'].  Since
        this isn't found, it calls self._data.keys().  But if I do 
        self.Item1, then it returns self['Item1'].  The very rare conflict
        case that a user has named the items a method that may already exist
        in the dictionary (eg items=['a','b','keys'] is addressed.
        """

        if attr in self._data.keys():
            if hasattr(self._data, attr):
                raise AttributeError(
                    '"%s attribute" found in both the items\
                and as a method of the underlying dictionary object.'
                    % (attr)
                )
            else:
                return self[attr]
        return getattr(self._data, attr)

    # Attributes deferred to self.data /dictionary
    def __len__(self):
        return self._data.__len__()

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

    def __reversed__(self):
        return self._data.__reversed__()

    def __contains__(self):
        return self._data.__contains__()

    def as_3d(self):
        """ Return 3d structure of data.  Default is panel."""
        raise Panel(data=self._data)

        ### Data types without labels

    # Is this realy necessary?  See pyparty.ParticleManger for possibly more consistent implementation
    def get_all(self, attr, astype=tuple):
        """Generator/tuple etc.. of (item, attribute) pairs. """

        return put._parse_generator(((item[0], getattr(item[1], attr)) for item in self.items()), astype)

    def _get_unique(self, attr):
        """ Inspects Stack itemwise for an attribute for unique values.
        If non-unique value for the attributes are found, returns
        "mixed". 
        """
        unique = set(self.get_all(attr, astype=dict).values())
        if len(unique) > 1:
            return "mixed"
        else:
            return tuple(unique)[0]  # set doesn't support indexing

    def set_all(self, attr, val, inplace=False):
        """ Set attributes itemwise.  
            If not inplace, returns new instance of self"""
        if inplace:
            for (key, item) in self.items():
                try:
                    setattr(item, attr, val)
                except Exception as E:
                    raise Exception(
                        'Could not set %s in "%s".  Received the following \
                     exception:\n "%s"'
                        % (attr, key, E)
                    )
        else:
            out = deepcopy(self._data)  # DEEPCOPY
            for item in out:
                setattr(out[item], attr, val)
            return self.__class__(out)

    def apply(self, func, *args, **kwargs):
        """ Applies a user-passed function, or calls an instance method itemwise.
        
        
        Parameters:
        -----------
        func: str or function
            If string, must correspond to a method on the object stored 
            itemwise in the stack.  If a function, appliked itemwise to
            objects stored.  
              
        inplace: False 
            Special kwarg.  If true, self._data modified inplace, 
            otherwise new specstack is returned.
                         
        *args, **kwargs: 
            func arguments.
          
        Returns:
        --------
        If not inplace, returns SpecStack after itemwise application.
            
        """

        inplace = kwargs.pop("inplace", False)

        if isinstance(func, basestring):
            if inplace:
                for item in self:
                    self[item] = getattr(self[item], func)(*args, **kwargs)

            else:
                return self.__class__(OrderedDict([(k, getattr(v, func)(*args, **kwargs)) for k, v in self.items()]))

        # function, numpyfunction etc...
        else:
            if inplace:
                for item in self:
                    self[item] = self[item].apply(func)(*args, **kwargs)

            else:
                return self.__class__(OrderedDict([(k, v.apply(func, *args, **kwargs)) for k, v in self.items()]))

    @property
    def full_name(self):
        """ Timespectra:name or Timespectra:unnamed.  Useful for scripts mostly """
        outname = getattr(self, "name", "unnamed")
        return "%s:%s" % (self.__class__.__name__, self.name)