source: utilitiespy/tudelft/utilities/immutablelist/Range.py@ 222

Last change on this file since 222 was 222, checked in by wouter, 3 years ago

#89 utilities to merge several modules. Added immutablelist

File size: 1.6 KB
Line 
1from decimal import Decimal, ROUND_FLOOR
2
3from tudelft.utilities.immutablelist.AbstractImmutableList import AbstractImmutableList
4
5
6class Range(AbstractImmutableList[Decimal]):
7 '''
8 Range is an immutable list of numbers from low to high with given step size.
9 This class is compatible with pyson.
10 '''
11 def __init__(self, low:Decimal, high: Decimal, step:Decimal):
12 if low==None or high==None or step==None:
13 raise ValueError("low high and step must be not None")
14 self._low=low
15 self._high=high
16 self._step=step
17
18 def getLow(self):
19 return self._low
20
21 def getHigh(self):
22 return self._high
23
24 def getStep(self):
25 return self._step
26
27 def get(self, index:Decimal)-> Decimal:
28 return self._low+ self._step * index
29
30 def size(self) -> Decimal:
31 if self._low > self._high:
32 return Decimal(0)
33 return (( self._high - self._low )/self._step) \
34 .quantize(Decimal('1'), rounding=ROUND_FLOOR) + Decimal('1')
35
36 def __eq__(self, other)->bool:
37 return isinstance(other, self.__class__) and \
38 self._low == other._low and \
39 self._high == other._high and\
40 self._step == other._step
41
42 def __repr__(self):
43 return "Range["+str(self._low)+","+str(self._high)+","+str(self._step)+"]"
44
45 def __hash__(self):
46 result = 1
47 result = 31 * result + hash(self._high)
48 result = 31 * result + hash(self._low)
49 result = 31 * result + hash(self._step)
50 return result
51
Note: See TracBrowser for help on using the repository browser.