-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_iterators.py
executable file
·87 lines (65 loc) · 2.67 KB
/
class_iterators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from typing import ItemsView
class ClassIterator:
def __init__(self, group_list=None, primary_key: str = "id") -> None:
self._index = 0
self.__primary_key = primary_key
try:
if group_list is None: self.__group_list = []
else: self.__group_list = group_list
except:
self.__group_list = []
def __str__(self) -> str:
all_obj_list = []
all_obj_list.append('\n')
all_obj_list.append(f'#################################\n')
for count, item in enumerate(self.__group_list):
all_obj_list.append(f'Item: {count + 1} \n')
str_list = [f'{k}: {v}' for k, v in item.__dict__.items()]
all_obj_list.append('\n'.join(str_list))
all_obj_list.append(f'\n################################# \n')
return '\n'.join(all_obj_list)
def __repr__(self) -> str:
all_obj_list = []
count_actual = 0
all_obj_list.append('Item properties: ')
for count, item in enumerate(self.__group_list):
all_obj_list.append(f'Number of regular attributes: {len(item.__dict__)}')
if count == 0: all_obj_list.append(f'List of regular attributes {item.__dict__}')
all_obj_list.append(f'Full list of attributes: {dir(item)}')
count_actual = count
all_obj_list.insert(0, f'Number of Items: {count_actual}')
return ' '.join(all_obj_list)
def __next__(self) -> object:
if self._index < len(self.__group_list):
result = self.__group_list[self._index]
self._index += 1
return result
#end of iteration
self._index = 0
raise StopIteration
def __iter__(self) -> object:
return self
def __len__(self) -> object:
return len(self.__group_list)
def __getitem__(self, key) -> object:
try:
for group in self.__group_list:
v = getattr(group, self.__primary_key)
if str(getattr(group, self.__primary_key)) == str(key): return group
except:
return None
raise(KeyError('invalid key'))
def __contains__(self, key) -> bool:
for item in self.__group_list:
if getattr(item, self.__primary_key) == key: return True
return False
def reset_iterator(self):
self._index = 0
def append(self, group) -> None:
self.__group_list.append(group)
def exists(self, key) -> bool:
if self[key] is None: return False
return True
@property
def objList(self) -> list:
return self.__group_list