Ejemplo n.º 1
0
class DimensionList(object):
    @pv.verify_method(pv.Iterable(a_name="dimension_iter", a_type=Dimension))
    def __init__(self, dimension_iter):
        self._list = list(dimension_iter)

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

    def __iter__(self):
        return iter(self._list)

    def box_count(self):
        return reduce(mul, [len(dimension) for dimension in self])

    def box_iter(self):
        # 1) from every i'th dimension receive the Range list.
        #    As result, we have list of list of Range
        # 2) product Range lists (for get every combination of ranges)
        # 3) iterate over result from (2), receive arguments for Box, create Boxes
        return [Box(range_list) for range_list in product(*map(iter, self))]

    def __str__(self):
        return "DimensionList<%s>" % self.create()

    def __repr__(self):
        return str(self)

    def create(self):
        return ','.join(map(Dimension.create, list(self)))
Ejemplo n.º 2
0
class Value(object):
    @pv.verify_method(pv.Iterable(a_name="value", a_type=int, a_none=True))
    def __init__(self, value):
        self._value = value

    def compatible(self, right):
        pv.Var(a_name="right", a_type=Value).verify(self.compatible, right)
        if bool(self) != bool(right):
            raise TypeError(
                "Value: compatible expects both is None or both is "
                "not None, but self is %s and right is %s" % (self, right))
        if not self is None:
            if len(self) != len(right):
                raise TypeError("Value: compatible expects same dimension "
                                "count of values, but self has %s "
                                "and right has %s" % (len(self), len(right)))

    def __nonzero__(self):
        return not (self._value is None)

    def __len__(self):
        'Count of attributes'
        if self._value is None:
            raise ValueError("Value: is empty, len undefined")
        else:
            return len(self._value)

    def __iter__(self):
        'Iterate over attribute values (value should be not empty)'
        return iter(self._value)

    def __cmp__(self, right):
        'Compare two values'
        pv.Var(a_name="right", a_type=Value).verify(self.__cmp__, right)
        self.compatible(right)
        for (left, right) in izip(self, right):
            result = cmp(left, right)
            if result != 0:
                return result
        return 0

    def __hash__(self):
        return sum(map(hash, self))

    def __str__(self):
        if self._value is None:
            return '()'
        else:

            def to_string(value):
                if value is None:
                    return ''
                else:
                    return str(value)

            return '(%s)' % ','.join(map(to_string, self._value))

    def __repr__(self):
        return "Value%s" % str(self)
Ejemplo n.º 3
0
class AttributeList(object):
    @pv.verify_method(pv.Iterable(a_name="attribute_iter", a_type=Attribute))
    def __init__(self, attribute_iter):
        self._list = list(attribute_iter)

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

    def __iter__(self):
        return iter(self._list)

    def create(self):
        return ','.join(map(Attribute.create, self._list))
Ejemplo n.º 4
0
class Array(object):
    @pv.verify_method(name=pv.Str("name"),
                      attribute_list=pv.Iterable("attribute_list",
                                                 a_type=Attribute),
                      dimension_list=pv.Iterable("dimension_list",
                                                 a_type=Dimension),
                      filler=pv.Proxy())
    def __init__(self,
                 name=None,
                 attribute_list=None,
                 dimension_list=None):  #, filler=None):
        #if filler is None:
        #    raise TypeError("Array: expected filler, but received %s" % str(filler))
        self._name = name
        self._attribute = attribute_list
        self._dimension = dimension_list
        #self._filler=    filler(self)

    @property
    def name(self):
        return self._name

    @property
    def attribute_list(self):
        return self._attribute

    @property
    def dimension_list(self):
        return self._dimension

    @property
    def filler(self):
        return self._filler

    def __len__(self):
        'Total count of chunks inside the array'
        return self._dimension.box_count()

    def __iter__(self):
        'Iterate over chunks inside the array'

        def create_chunk(box):
            return Chunk(box, self.filler)

        return imap(create_chunk, self._dimension.box_iter())

    #def dense(self):
    #    return self.cell_print(dense=True)

    #def sparse(self):
    #    return self.cell_print(dense=False)

    #@pv.verify_method(dense=pv.Bool("dense"))
    #def cell_print(self, dense=None):
    #    sep= ';%s' % os.linesep
    #    if dense:
    #        f= Chunk.dense
    #    else:
    #        f= Chunk.sparse
    #    return sep.join(map(f, list(self)))

    def create(self):
        return "create array %s <%s> [%s]" % (self.name,
                                              self.attribute_list.create(),
                                              self.dimension_list.create())

    def remove(self):
        return "remove(%s)" % self._name
Ejemplo n.º 5
0
class Vector(object):
    'The n-dimensional vector'

    @pv.verify_method(pv.Iterable(a_name="vector", a_type=int))
    def __init__(self, vector):
        self._vector = vector

    def __hash__(self):
        return sum(map(hash, self))

    def __len__(self):
        'Total count of components'
        return len(self._vector)

    def __str__(self):
        'String representation of vector components'
        return '{%s}' % ','.join(map(str, self._vector))

    def __repr__(self):
        return "Vector%s" % str(self)

    #def dense(self):
    #    return ''

    #def sparse(self):
    #    return str(self)

    def __getitem__(self, index):
        "Get index's component"
        return self._vector[index]

    def __iter__(self):
        'Iterate over components'
        return iter(self._vector)

    def __neg__(self):
        return Vector(-item for item in self)

    def __pos__(self):
        return Vector(self)

    def compatible(self, right):
        pv.Var(a_name="right", a_type=Vector).verify(self.compatible, right)
        if len(self) != len(right):
            raise TypeError("Vector: compatible expects same "
                            "dimension count of vectors, "
                            "but self has %s and right has %s" %
                            (len(self), len(right)))

    @property
    def zero(self):
        return Vector(0 for value in self)

    def __cmp__(self, right):
        'Compare two vectors'
        self.compatible(right)
        for (left, right) in izip(self, right):
            result = cmp(left, right)
            if result != 0:
                return result
        return 0

    def __add__(self, right):
        self.compatible(right)
        return Vector((left + right) for (left, right) in zip(self, right))

    def __sub__(self, right):
        self.compatible(right)
        return Vector((left - right) for (left, right) in zip(self, right))
Ejemplo n.º 6
0
class Box(object):
    @pv.verify_method(pv.Iterable(a_name="range_iter", a_type=Range))
    def __init__(self, range_iter):
        "every i'th range from range_iter - range from i'th dimension"
        self._range = range_iter

    def compatible(self, right):
        pv.Var(a_name="right", a_type=Box).verify(self.compatible, right)
        if len(self) != len(right):
            raise TypeError("Box: compatible expects same dimension count "
                            "of boxes, but self has %s and right has %s" %
                            (len(self), len(right)))

    def __nonzero__(self):
        return all(self)

    def __str__(self):
        return "Box(%s)" % ','.join(map(str, self))

    def __repr__(self):
        return str(self)

    def __contains__(self, right):
        'Vector inside the box'
        pv.Var(a_name="right", a_type=Box).verify(self.__contains__, right)
        if len(self) != len(right):
            raise TypeError(
                "Box: contains expects same dimension count of "
                "box and vector, but self has %s and right has %s" %
                (len(self), len(right)))
        return all(_item in _range for _range, _item in izip(self, right))

    def __eq__(self, right):
        pv.Var(a_name="right", a_type=Box).verify(self.__eq__, right)
        self.compatible(right)
        return all(left == right for left, right in izip(self, right))

    def __ne__(self, right):
        pv.Var(a_name="right", a_type=Box).verify(self.__ne__, right)
        return not (self == right)

    def __hash__(self):
        return mul(map(hash, [self._min, self._max]))

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

    def __iter__(self):
        return iter(self._range)

    def cell_count(self):
        return

    def vector_count(self):
        return mul(map(Range.value_count, self))

    def vector_iter(self):
        return imap(Vector, product(*imap(Range.value_iter, self)))

    @property
    def minimum(self):
        return Vector([current_range.minimum.value for current_range in self])