pyloid 0.26.2__py3-none-any.whl → 0.26.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyloid/store.py CHANGED
@@ -1,179 +1,257 @@
1
1
  import os
2
- from pickledb import PickleDB
3
- from typing import Any, List, Optional
2
+ from pickledb import (
3
+ PickleDB,
4
+ )
5
+ from typing import (
6
+ Any,
7
+ List,
8
+ Optional,
9
+ )
4
10
 
5
11
 
6
12
  class Store:
7
- def __init__(self, path: str):
8
- """
9
- Initialize a Store instance.
10
-
11
- Parameters
12
- ----------
13
- path: str
14
- Path to the database file where data will be stored
15
-
16
- Examples
17
- --------
18
- >>> store = Store("data.json")
19
- """
20
- os.makedirs(os.path.dirname(path), exist_ok=True)
21
- self.db = PickleDB(path)
22
-
23
- def get(self, key: str, default: Any = None) -> Any:
24
- """
25
- Retrieve the value associated with the specified key.
26
-
27
- Parameters
28
- ----------
29
- key: str
30
- The key to look up in the database
31
- default: Any
32
- The value to return if the value does not exist in the database
33
-
34
- Returns
35
- -------
36
- Any
37
- The value associated with the key, or None if the key doesn't exist
38
-
39
- Examples
40
- --------
41
- >>> store = Store("data.json")
42
- >>> store.set("user", {"name": "John Doe", "age": 30})
43
- True
44
- >>> user = store.get("user")
45
- >>> print(user)
46
- {'name': 'John Doe', 'age': 30}
47
- >>> print(store.get("non_existent_key"))
48
- None
49
- >>> print(store.get("non_existent_key", "default_value"))
50
- 'default_value'
51
- """
52
- stored_value = self.db.get(key)
53
- return stored_value if stored_value is not None else default
54
-
55
- def set(self, key: str, value: Any) -> bool:
56
- """
57
- Add or update a key-value pair in the database.
58
-
59
- Parameters
60
- ----------
61
- key: str
62
- The key to set in the database
63
- value: Any
64
- The value to associate with the key (must be a JSON-serializable Python data type)
65
-
66
- Returns
67
- -------
68
- bool
69
- Always returns True to indicate the operation was performed
70
-
71
- Examples
72
- --------
73
- >>> store = Store("data.json")
74
- >>> store.set("settings", {"theme": "dark", "notifications": True})
75
- True
76
- >>> store.set("counter", 42)
77
- True
78
- >>> store.set("items", ["apple", "banana", "orange"])
79
- True
80
- """
81
- return self.db.set(key, value)
82
-
83
- def remove(self, key: str) -> bool:
84
- """
85
- Delete the value associated with the key from the database.
86
-
87
- Parameters
88
- ----------
89
- key: str
90
- The key to remove from the database
91
-
92
- Returns
93
- -------
94
- bool
95
- True if the key was deleted, False if the key didn't exist
96
-
97
- Examples
98
- --------
99
- >>> store = Store("data.json")
100
- >>> store.set("temp", "temporary data")
101
- True
102
- >>> store.remove("temp")
103
- True
104
- >>> store.remove("non_existent_key")
105
- False
106
- """
107
- return self.db.remove(key)
108
-
109
- def all(self) -> List[str]:
110
- """
111
- Retrieve a list of all keys in the database.
112
-
113
- Returns
114
- -------
115
- List[str]
116
- A list containing all keys currently stored in the database
117
-
118
- Examples
119
- --------
120
- >>> store = Store("data.json")
121
- >>> store.set("key1", "value1")
122
- True
123
- >>> store.set("key2", "value2")
124
- True
125
- >>> keys = store.all()
126
- >>> print(keys)
127
- ['key1', 'key2']
128
- """
129
- return self.db.all()
130
-
131
- def purge(self) -> bool:
132
- """
133
- Clear all keys and values from the database.
134
-
135
- Returns
136
- -------
137
- bool
138
- Always returns True to indicate the operation was performed
139
-
140
- Examples
141
- --------
142
- >>> store = Store("data.json")
143
- >>> store.set("key1", "value1")
144
- True
145
- >>> store.set("key2", "value2")
146
- True
147
- >>> store.purge()
148
- True
149
- >>> print(store.all())
150
- []
151
- """
152
- return self.db.purge()
153
-
154
- def save(self, option: Optional[int] = None) -> bool:
155
- """
156
- Save the current state of the database to file.
157
-
158
- Parameters
159
- ----------
160
- option: Optional[int]
161
- Optional orjson.OPT_* flags that configure serialization behavior.
162
- These flags can control formatting, special type handling, etc.
163
-
164
- Returns
165
- -------
166
- bool
167
- True if the operation was successful, False otherwise
168
-
169
- Examples
170
- --------
171
- >>> store = Store("data.json")
172
- >>> store.set("key", "value")
173
- True
174
- >>> store.save()
175
- True
176
- """
177
- if option is not None:
178
- return self.db.save(option)
179
- return self.db.save()
13
+ def __init__(
14
+ self,
15
+ path: str,
16
+ ):
17
+ """
18
+ Initialize a Store instance.
19
+
20
+ Parameters
21
+ ----------
22
+ path: str
23
+ Path to the database file where data will be stored
24
+
25
+ Examples
26
+ --------
27
+ >>> store = Store('data.json')
28
+ """
29
+ os.makedirs(
30
+ os.path.dirname(path),
31
+ exist_ok=True,
32
+ )
33
+ self.db = PickleDB(path)
34
+
35
+ def get(
36
+ self,
37
+ key: str,
38
+ default: Any = None,
39
+ ) -> Any:
40
+ """
41
+ Retrieve the value associated with the specified key.
42
+
43
+ Parameters
44
+ ----------
45
+ key: str
46
+ The key to look up in the database
47
+ default: Any
48
+ The value to return if the value does not exist in the database
49
+
50
+ Returns
51
+ -------
52
+ Any
53
+ The value associated with the key, or None if the key doesn't exist
54
+
55
+ Examples
56
+ --------
57
+ >>> store = Store('data.json')
58
+ >>> store.set(
59
+ ... 'user',
60
+ ... {
61
+ ... 'name': 'John Doe',
62
+ ... 'age': 30,
63
+ ... },
64
+ ... )
65
+ True
66
+ >>> user = store.get('user')
67
+ >>> print(user)
68
+ {'name': 'John Doe', 'age': 30}
69
+ >>> print(store.get('non_existent_key'))
70
+ None
71
+ >>> print(
72
+ ... store.get(
73
+ ... 'non_existent_key',
74
+ ... 'default_value',
75
+ ... )
76
+ ... )
77
+ 'default_value'
78
+ """
79
+ stored_value = self.db.get(key)
80
+ return stored_value if stored_value is not None else default
81
+
82
+ def set(
83
+ self,
84
+ key: str,
85
+ value: Any,
86
+ ) -> bool:
87
+ """
88
+ Add or update a key-value pair in the database.
89
+
90
+ Parameters
91
+ ----------
92
+ key: str
93
+ The key to set in the database
94
+ value: Any
95
+ The value to associate with the key (must be a JSON-serializable Python data type)
96
+
97
+ Returns
98
+ -------
99
+ bool
100
+ Always returns True to indicate the operation was performed
101
+
102
+ Examples
103
+ --------
104
+ >>> store = Store('data.json')
105
+ >>> store.set(
106
+ ... 'settings',
107
+ ... {
108
+ ... 'theme': 'dark',
109
+ ... 'notifications': True,
110
+ ... },
111
+ ... )
112
+ True
113
+ >>> store.set(
114
+ ... 'counter',
115
+ ... 42,
116
+ ... )
117
+ True
118
+ >>> store.set(
119
+ ... 'items',
120
+ ... [
121
+ ... 'apple',
122
+ ... 'banana',
123
+ ... 'orange',
124
+ ... ],
125
+ ... )
126
+ True
127
+ """
128
+ return self.db.set(
129
+ key,
130
+ value,
131
+ )
132
+
133
+ def remove(
134
+ self,
135
+ key: str,
136
+ ) -> bool:
137
+ """
138
+ Delete the value associated with the key from the database.
139
+
140
+ Parameters
141
+ ----------
142
+ key: str
143
+ The key to remove from the database
144
+
145
+ Returns
146
+ -------
147
+ bool
148
+ True if the key was deleted, False if the key didn't exist
149
+
150
+ Examples
151
+ --------
152
+ >>> store = Store('data.json')
153
+ >>> store.set(
154
+ ... 'temp',
155
+ ... 'temporary data',
156
+ ... )
157
+ True
158
+ >>> store.remove('temp')
159
+ True
160
+ >>> store.remove('non_existent_key')
161
+ False
162
+ """
163
+ return self.db.remove(key)
164
+
165
+ def all(
166
+ self,
167
+ ) -> List[str]:
168
+ """
169
+ Retrieve a list of all keys in the database.
170
+
171
+ Returns
172
+ -------
173
+ List[str]
174
+ A list containing all keys currently stored in the database
175
+
176
+ Examples
177
+ --------
178
+ >>> store = Store('data.json')
179
+ >>> store.set(
180
+ ... 'key1',
181
+ ... 'value1',
182
+ ... )
183
+ True
184
+ >>> store.set(
185
+ ... 'key2',
186
+ ... 'value2',
187
+ ... )
188
+ True
189
+ >>> keys = store.all()
190
+ >>> print(keys)
191
+ ['key1', 'key2']
192
+ """
193
+ return self.db.all()
194
+
195
+ def purge(
196
+ self,
197
+ ) -> bool:
198
+ """
199
+ Clear all keys and values from the database.
200
+
201
+ Returns
202
+ -------
203
+ bool
204
+ Always returns True to indicate the operation was performed
205
+
206
+ Examples
207
+ --------
208
+ >>> store = Store('data.json')
209
+ >>> store.set(
210
+ ... 'key1',
211
+ ... 'value1',
212
+ ... )
213
+ True
214
+ >>> store.set(
215
+ ... 'key2',
216
+ ... 'value2',
217
+ ... )
218
+ True
219
+ >>> store.purge()
220
+ True
221
+ >>> print(store.all())
222
+ []
223
+ """
224
+ return self.db.purge()
225
+
226
+ def save(
227
+ self,
228
+ option: Optional[int] = None,
229
+ ) -> bool:
230
+ """
231
+ Save the current state of the database to file.
232
+
233
+ Parameters
234
+ ----------
235
+ option: Optional[int]
236
+ Optional orjson.OPT_* flags that configure serialization behavior.
237
+ These flags can control formatting, special type handling, etc.
238
+
239
+ Returns
240
+ -------
241
+ bool
242
+ True if the operation was successful, False otherwise
243
+
244
+ Examples
245
+ --------
246
+ >>> store = Store('data.json')
247
+ >>> store.set(
248
+ ... 'key',
249
+ ... 'value',
250
+ ... )
251
+ True
252
+ >>> store.save()
253
+ True
254
+ """
255
+ if option is not None:
256
+ return self.db.save(option)
257
+ return self.db.save()