from decimal import Decimal, ROUND_FLOOR import decimal from typing import List from immutablelist.AbstractImmutableList import AbstractImmutableList from immutablelist.ImmutableList import ImmutableList class Range(AbstractImmutableList[Decimal]): ''' Range is an immutable list of numbers from low to high with given step size. This class is compatible with pyson. ''' def __init__(self, low:Decimal, high: Decimal, step:Decimal): if low==None or high==None or step==None: raise ValueError("low high and step must be not None") self._low=low self._high=high self._step=step def getLow(self): return self._low def getHigh(self): return self._high def getStep(self): return self._step def get(self, index:Decimal)-> Decimal: return self._low+ self._step * index def size(self) -> Decimal: if self._low > self._high: return Decimal(0) return (( self._high - self._low )/self._step) \ .quantize(Decimal('1'), rounding=ROUND_FLOOR) + Decimal('1') def __eq__(self, other)->bool: return isinstance(other, self.__class__) and \ self._low == other._low and \ self._high == other._high and\ self._step == other._step def __repr__(self): return "Range["+str(self._low)+","+str(self._high)+","+str(self._step)+"]" def __hash__(self): result = 1 result = 31 * result + hash(self._high) result = 31 * result + hash(self._low) result = 31 * result + hash(self._step) return result