forked from Election-Tech-Initiative/electionguard-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_store.py
More file actions
120 lines (96 loc) · 2.69 KB
/
data_store.py
File metadata and controls
120 lines (96 loc) · 2.69 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from collections.abc import Mapping
from typing import (
Dict,
Generic,
Iterable,
Iterator,
List,
Optional,
Tuple,
TypeVar,
)
_T = TypeVar("_T")
_U = TypeVar("_U")
class DataStore(Generic[_T, _U]):
"""
A lightweight convenience wrapper around a dictionary for data storage.
This implementation defines the common interface used to access stored
state elements.
"""
_store: Dict[_T, _U]
def __init__(self) -> None:
self._store = {}
def __iter__(self) -> Iterator:
return iter(self._store.items())
def all(self) -> List[_U]:
"""
Get all `SubmittedBallot` from the store
"""
return list(self._store.values())
def clear(self) -> None:
"""
Clear data from store
"""
self._store.clear()
def get(self, key: _T) -> Optional[_U]:
"""
Get value in store
:param key: key
:return: value if found
"""
return self._store.get(key)
def items(self) -> Iterable[Tuple[_T, _U]]:
"""
Gets all items in store as list
:return: List of (key, value)
"""
return self._store.items()
def keys(self) -> Iterable[_T]:
"""
Gets all keys in store as list
:return: List of keys
"""
return self._store.keys()
def __len__(self) -> int:
"""
Get length or count of store
:return: Count in store
"""
return len(self._store)
def pop(self, key: _T) -> Optional[_U]:
"""
Pop an object from the store if it exists.
:param key: key
"""
if key in self._store:
return self._store.pop(key)
return None
def set(self, key: _T, value: _U) -> None:
"""
Create or update a new value in store
:param key: key
:param value: value
"""
self._store[key] = value
def values(self) -> Iterable[_U]:
"""
Gets all values in store as list
:return: List of values
"""
return self._store.values()
class ReadOnlyDataStore(Generic[_T, _U], Mapping):
"""
A readonly view to a Data store
"""
def __init__(self, data: DataStore[_T, _U]):
self._data: DataStore[_T, _U] = data
def __getitem__(self, key: _T) -> Optional[_U]:
return self._data.get(key)
def __len__(self) -> int:
return len(self._data)
def __iter__(self) -> Iterator:
return iter(self._data.items())
def __eq__(self, other: object) -> bool:
if not isinstance(other, ReadOnlyDataStore):
return False
return ReadOnlyDataStore.__eq__(self, other)