source: immutablelistpy/immutablelist/Range.py@ 209

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

#85 cleaned up and tested immutablelistpy. Removed pyson dependency, added doc

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