-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.py
48 lines (41 loc) · 1.53 KB
/
manager.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
class Manager:
def __init__(self, _class):
self._claas = _class
def __str__(self):
return f'Manager : {self._claas}'
def search(self, **kwargs):
results = []
for obj in self._claas.object_list:
answers = []
for key, value in kwargs.items():
if key.endswith('__min'):
key = key[:-5]
compare_key = 'min'
elif key.endswith('__max'):
key = key[:-5]
compare_key = 'max'
else:
compare_key = 'equal'
if hasattr(obj, key):
if compare_key == 'min':
result = bool(getattr(obj, key) >= value)
answers.append(result)
elif compare_key == 'max':
result = bool(getattr(obj, key) <= value)
answers.append(result)
else:
result = bool(getattr(obj, key) == value)
answers.append(result)
else:
answers.append(False)
if all(answers):
results.append(obj)
return results
def get(self, **kwargs):
for key, value in kwargs.items():
for obj in self._claas.object_list:
if hasattr(obj, key) and getattr(obj, key) == value:
return obj
return None
def count(self):
return len(self._claas.object_list)