class Cell(object): @pv.verify_method(pv.Var(a_name="vector", a_type=Vector), pv.Var(a_name="value", a_type=Value)) def __init__(self, vector, value): self._vector = vector self._value = value @property def vector(self): return self._vector @property def value(self): return self._value
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)))
def __eq__(self, right): pv.Var(a_name="right", a_type=Bound).verify(self.__eq__, right) return all(left == right for left, right in [( self.value, right.value), (self.overlap, right.overlap), (self.has_overlap, right.has_overlap)])
class Chunk(object): @pv.verify_method(pv.Var(a_name="box", a_type=Box), pv.Proxy()) def __init__(self, box, filler): if filler is None: raise TypeError("Chunk: expected filler, but received %s" % str(box)) self._box = box self._filler = filler
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 __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 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 __ne__(self, right): pv.Var(a_name="right", a_type=Box).verify(self.__ne__, right) return not (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 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 __contains__(self, right): pv.Var(a_name="right", a_type=Range).verify(self.__contains__, right) return self.minimum <= right and right < self.maximum
def __eq__(self, right): pv.Var(a_name="right", a_type=Range).verify(self.__eq__, right) return self.minimum == right.minimum and self.maximum == right.maximum
class Range(object): @pv.verify_method(pv.Var(a_name="minimum", a_type=Bound), pv.Var(a_name="maximum", a_type=Bound)) def __init__(self, minimum, maximum): if minimum.overlap != maximum.overlap: raise ValueError( "Range: overlap on minimum and maximum should be same, " "but minimum has %s overlap and maximum has %s overlap" % (minimum.overlap, maximum.overlap)) if minimum.value > maximum.value: raise ValueError( "Range: minimum should be lesser or equal than maximum, " "but minimum is %s and maximum is %s" % (minimum.value, maximum.value)) self._minimum = minimum self._maximum = maximum @property def minimum(self): return self._minimum @property def maximum(self): return self._maximum @property def overlap(self): return self.minimum.overlap def __eq__(self, right): pv.Var(a_name="right", a_type=Range).verify(self.__eq__, right) return self.minimum == right.minimum and self.maximum == right.maximum def __ne__(self, right): pv.Var(a_name="right", a_type=Range).verify(self.__ne__, right) return not (self == right) def __hash__(self): return sum(map(hash, [self.minimum, self.maximum])) def __nonzero__(self): return self.minimum != self.maximum def __str__(self): return "Range(%s,%s)" % tuple(map(str, [self.minimum, self.maximum])) def __repr__(self): return str(self) def __contains__(self, right): pv.Var(a_name="right", a_type=Range).verify(self.__contains__, right) return self.minimum <= right and right < self.maximum def value_count(self): bound_list = [self.minimum, self.maximum] overlap_count = len(filter(None, map(Bound.has_overlap, bound_list))) return self.maximum.value - self.minimum.value + overlap_count * self.overlap def value_iter(self): minimum = self.minimum.value if self.minimum.has_overlap: minimum -= self.overlap maximum = self.maximum.value if self.maximum.has_overlap: maximum += self.overlap return range(minimum, maximum)