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

Last change on this file since 1030 was 962, checked in by wouter, 5 months ago

#329 AbstractImmutableList iterator implement iter

File size: 1.2 KB
RevLine 
[222]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
[243]12 self._index = 0
[222]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)
[243]19 self._index += 1
[222]20 return val
[962]21 def __iter__(self):
22 return self
[222]23
24class AbstractImmutableList(ImmutableList[E]):
[285]25 _PRINT_LIMIT = 20;
26
[222]27 def __iter__(self):
[285]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.