gitscope
https://dev.kronoz.win/r/marzneshin.git
app/utils/store.py · v0.1.0
Source
1 a47eb4c Dawsh 2y ago class MemoryStorage:
2 a47eb4c Dawsh 2y ago def __init__(self):
3 a47eb4c Dawsh 2y ago self._data = {}
4 a47eb4c Dawsh 2y ago
5 a47eb4c Dawsh 2y ago def set(self, key, value):
6 a47eb4c Dawsh 2y ago self._data[key] = value
7 a47eb4c Dawsh 2y ago
8 a47eb4c Dawsh 2y ago def get(self, key, default=None):
9 a47eb4c Dawsh 2y ago return self._data.get(key, default)
10 a47eb4c Dawsh 2y ago
11 a47eb4c Dawsh 2y ago def delete(self, key):
12 a47eb4c Dawsh 2y ago self._data.pop(key, None)
13 a47eb4c Dawsh 2y ago
14 a47eb4c Dawsh 2y ago def clear(self):
15 a47eb4c Dawsh 2y ago self._data.clear()
16 a47eb4c Dawsh 2y ago
17 a47eb4c Dawsh 2y ago
18 a47eb4c Dawsh 2y ago class ListStorage(list):
19 a47eb4c Dawsh 2y ago def __init__(self, update_func):
20 a47eb4c Dawsh 2y ago super().__init__()
21 a47eb4c Dawsh 2y ago self.update_func = update_func
22 a47eb4c Dawsh 2y ago
23 a47eb4c Dawsh 2y ago def __getitem__(self, index):
24 a47eb4c Dawsh 2y ago if not self:
25 a47eb4c Dawsh 2y ago self.update()
26 a47eb4c Dawsh 2y ago
27 a47eb4c Dawsh 2y ago return super().__getitem__(index)
28 a47eb4c Dawsh 2y ago
29 a47eb4c Dawsh 2y ago def __iter__(self):
30 a47eb4c Dawsh 2y ago if not self:
31 a47eb4c Dawsh 2y ago self.update()
32 a47eb4c Dawsh 2y ago
33 a47eb4c Dawsh 2y ago return super().__iter__()
34 a47eb4c Dawsh 2y ago
35 a47eb4c Dawsh 2y ago def __str__(self):
36 a47eb4c Dawsh 2y ago if not self:
37 a47eb4c Dawsh 2y ago self.update()
38 a47eb4c Dawsh 2y ago
39 a47eb4c Dawsh 2y ago return super().__str__()
40 a47eb4c Dawsh 2y ago
41 a47eb4c Dawsh 2y ago def update(self):
42 a47eb4c Dawsh 2y ago self.update_func(self)
43 a47eb4c Dawsh 2y ago
44 a47eb4c Dawsh 2y ago
45 a47eb4c Dawsh 2y ago class DictStorage(dict):
46 a47eb4c Dawsh 2y ago def __init__(self, update_func):
47 a47eb4c Dawsh 2y ago super().__init__()
48 a47eb4c Dawsh 2y ago self.update_func = update_func
49 a47eb4c Dawsh 2y ago
50 a47eb4c Dawsh 2y ago def __getitem__(self, key):
51 a47eb4c Dawsh 2y ago if not self:
52 a47eb4c Dawsh 2y ago self.update()
53 a47eb4c Dawsh 2y ago
54 a47eb4c Dawsh 2y ago return super().__getitem__(key)
55 a47eb4c Dawsh 2y ago
56 a47eb4c Dawsh 2y ago def __iter__(self):
57 a47eb4c Dawsh 2y ago if not self:
58 a47eb4c Dawsh 2y ago self.update()
59 a47eb4c Dawsh 2y ago
60 a47eb4c Dawsh 2y ago return super().__iter__()
61 a47eb4c Dawsh 2y ago
62 a47eb4c Dawsh 2y ago def __str__(self):
63 a47eb4c Dawsh 2y ago if not self:
64 a47eb4c Dawsh 2y ago self.update()
65 a47eb4c Dawsh 2y ago
66 a47eb4c Dawsh 2y ago return super().__str__()
67 a47eb4c Dawsh 2y ago
68 a47eb4c Dawsh 2y ago def values(self):
69 a47eb4c Dawsh 2y ago if not self:
70 a47eb4c Dawsh 2y ago self.update()
71 a47eb4c Dawsh 2y ago
72 a47eb4c Dawsh 2y ago return super().values()
73 a47eb4c Dawsh 2y ago
74 a47eb4c Dawsh 2y ago def keys(self):
75 a47eb4c Dawsh 2y ago if not self:
76 a47eb4c Dawsh 2y ago self.update()
77 a47eb4c Dawsh 2y ago
78 a47eb4c Dawsh 2y ago return super().keys()
79 a47eb4c Dawsh 2y ago
80 a47eb4c Dawsh 2y ago def get(self, key, default=None):
81 a47eb4c Dawsh 2y ago if not self:
82 a47eb4c Dawsh 2y ago self.update()
83 a47eb4c Dawsh 2y ago
84 a47eb4c Dawsh 2y ago return super().get(key, default)
85 a47eb4c Dawsh 2y ago
86 a47eb4c Dawsh 2y ago def update(self):
87 a47eb4c Dawsh 2y ago self.update_func(self)