source: utilitiespy/tudelft/utilities/immutablelist/AbstractImmutableList.py@ 1120

Last change on this file since 1120 was 962, checked in by wouter, 4 months ago

#329 AbstractImmutableList iterator implement iter

File size: 1.2 KB
Line 
1from decimal import Decimal
2from typing import TypeVar
3
4from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
5
6
7E = TypeVar('E')
8class ItemIterator:
9 def __init__(self, l:ImmutableList[E]):
10 self._l = l
11 # member variable to keep track of current index
12 self._index = 0
13 def __next__(self):
14 '''Returns the next value from team object's lists '''
15 if self._index >= self._l.size():
16 # End of Iteration
17 raise StopIteration
18 val=self._l.get(self._index)
19 self._index += 1
20 return val
21 def __iter__(self):
22 return self
23
24class AbstractImmutableList(ImmutableList[E]):
25 _PRINT_LIMIT = 20;
26
27 def __iter__(self):
28 return ItemIterator(self)
29
30 def __repr__(self) :
31 if self.size() > self._PRINT_LIMIT:
32 end = self._PRINT_LIMIT;
33 else:
34 end = self.size()
35
36 string = "[";
37 for n in range(end):
38 string += ( "," if n!=0 else "") + str(self.get(n))
39
40 remain = self.size() - end
41 if remain > 0:
42 string += ",..." + str(remain) + " more..."
43
44 string += "]";
45 return string;
46
Note: See TracBrowser for help on using the repository browser.