cloud-python-vars 0.1.1__tar.gz → 0.2.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cloud-python-vars
3
- Version: 0.1.1
3
+ Version: 0.2.0
4
4
  Summary: To manage and create raw Python variables on cloud servers.
5
5
  Author-email: ThatOnePythonDev <khakhang2h5e@gmail.com>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cloud-python-vars
3
- Version: 0.1.1
3
+ Version: 0.2.0
4
4
  Summary: To manage and create raw Python variables on cloud servers.
5
5
  Author-email: ThatOnePythonDev <khakhang2h5e@gmail.com>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -96,11 +96,11 @@ async def main() -> None: # You're recommended to put this in an asynchronous fu
96
96
 
97
97
  from __future__ import annotations
98
98
 
99
- from typing import Any, Literal, Never
100
-
101
- import time
102
99
  import asyncio
103
100
  import threading
101
+ import time
102
+ from typing import Any, Literal, override
103
+
104
104
  import upstash_redis as redis
105
105
 
106
106
 
@@ -120,6 +120,7 @@ class CloudProxy:
120
120
 
121
121
  self.value: Any = value
122
122
 
123
+ @override
123
124
  def __repr__(self) -> str:
124
125
  return f"CloudProxy({self.value})"
125
126
 
@@ -137,6 +138,8 @@ class CloudVar(redis.Redis):
137
138
  rest_retry_interval: float = 3,
138
139
  allow_telemetry: bool = True,
139
140
  read_your_writes: bool = True,
141
+ delay: float = 0.1,
142
+ tb: Literal["strict", "log", "ignore"] = "log"
140
143
  ) -> None:
141
144
  """
142
145
  To initialize the class.
@@ -148,6 +151,8 @@ class CloudVar(redis.Redis):
148
151
  :param rest_retry_interval: The number of seconds the client waits before retrying a new request. The default is 3.
149
152
  :param allow_telemetry: Controls whether the client is allowed to send telemetry data. The default is True.
150
153
  :param read_your_writes: Allows whether the client is allowed to read your writes. The default is True.
154
+ :param delay: The delay to prevent CPU over-usage and network spamming. The default is 0.1.
155
+ :param tb: Controls what to do when an error occurs. 'strict' for letting it raise, 'log' for catching and logging the error, 'ignore' for passing silently. The default is 'log'.
151
156
 
152
157
  :type url: str
153
158
  :type token: str
@@ -156,23 +161,34 @@ class CloudVar(redis.Redis):
156
161
  :type rest_retry_interval: float
157
162
  :type allow_telemetry: bool
158
163
  :type read_your_writes: bool
164
+ :type delay: float
165
+ :type tb: Literal["strict", "log", "ignore"]
159
166
  """
160
167
 
161
168
  super().__init__(url, token, rest_encoding, rest_retries, rest_retry_interval, allow_telemetry, read_your_writes,)
162
169
 
163
- self.__data: dict[str, CloudProxy] = {k: v for k, v in self.items().items()}
170
+ self.__data: dict[str, CloudProxy] = {k: CloudProxy(v) for k, v in self.items().items()}
164
171
 
165
172
  self.__thread: threading.Thread = threading.Thread(target=self.__loop)
166
173
  self.__thread.start()
167
174
 
175
+ self.delay: float = delay
176
+ self.tb: str = tb
177
+
168
178
  def __loop(self) -> None:
169
179
  try:
170
180
  while True:
171
181
  for k, v in self.__data.copy().items():
172
182
  self.set(k, repr(v))
173
- time.sleep(0.1)
174
- except:
175
- pass
183
+ time.sleep(self.delay)
184
+ except Exception as e:
185
+ match self.tb:
186
+ case "strict":
187
+ raise
188
+ case "log":
189
+ print(e.__traceback__)
190
+ case _:
191
+ pass
176
192
 
177
193
  def __setitem__(self, key: str, value: Any) -> None:
178
194
  """
@@ -219,6 +235,9 @@ class CloudVar(redis.Redis):
219
235
  values: list[Any] = [self[key] for key in keys]
220
236
  self.delete(*keys)
221
237
 
238
+ for key in keys:
239
+ del self.__data[key]
240
+
222
241
  return tuple(values)
223
242
 
224
243
  def clear(self) -> None:
@@ -229,6 +248,7 @@ class CloudVar(redis.Redis):
229
248
 
230
249
  all_keys: list[str] = self.keys(r"[\s\S]*")
231
250
  self.delete(*all_keys)
251
+ self.__data.clear()
232
252
 
233
253
  def items(self) -> dict[str, Any]:
234
254
  """
@@ -255,6 +275,8 @@ class AsyncCloudVar(redis.AsyncRedis):
255
275
  rest_retry_interval: float = 3,
256
276
  allow_telemetry: bool = True,
257
277
  read_your_writes: bool = True,
278
+ delay: float = 0.1,
279
+ tb: Literal["strict", "log", "ignore"] = "log"
258
280
  ) -> None:
259
281
  """
260
282
  To initialize the class.
@@ -280,21 +302,30 @@ class AsyncCloudVar(redis.AsyncRedis):
280
302
 
281
303
  self.__data: dict[str, CloudProxy] = {}
282
304
 
305
+ self.delay: float = delay
306
+ self.tb: str = tb
307
+
283
308
  def start(self) -> None:
284
309
  """Starts the instance's process."""
285
310
 
286
- async def task() -> Never | None:
311
+ async def task() -> None:
287
312
  self.__data = {k: v for k, v in (await self.items()).items()}
288
313
 
289
314
  try:
290
315
  while True:
291
316
  for k, v in self.__data.copy().items():
292
- await self.set(k, repr(v))
317
+ _ = await self.set(k, repr(v))
293
318
  await asyncio.sleep(0.1)
294
- except:
295
- pass
319
+ except Exception as e:
320
+ match self.tb:
321
+ case "strict":
322
+ raise
323
+ case "log":
324
+ print(e.__traceback__)
325
+ case _:
326
+ pass
296
327
 
297
- asyncio.create_task(task())
328
+ _ = asyncio.create_task(task())
298
329
 
299
330
  def __setitem__(self, key: str, value: Any) -> None:
300
331
  """
@@ -341,6 +372,9 @@ class AsyncCloudVar(redis.AsyncRedis):
341
372
  values: list[Any] = [self[key] for key in keys]
342
373
  await self.delete(*keys)
343
374
 
375
+ for key in keys:
376
+ del self.__data[key]
377
+
344
378
  return tuple(values)
345
379
 
346
380
  async def clear(self) -> None:
@@ -352,6 +386,8 @@ class AsyncCloudVar(redis.AsyncRedis):
352
386
  all_keys: list[str] = await self.keys(r"[\s\S]*")
353
387
  await self.delete(*all_keys)
354
388
 
389
+ self.__data.clear()
390
+
355
391
  async def items(self) -> dict[str, Any]:
356
392
  """
357
393
  Returns a dictionary of key-value pairs on the client.
@@ -361,4 +397,4 @@ class AsyncCloudVar(redis.AsyncRedis):
361
397
  :returns: The key-value pairs from the client.
362
398
  """
363
399
 
364
- return {k: self.get(k) for k in await self.keys(r"[\s\S]*") if self.get(k)}
400
+ return {k: await self.get(k) for k in await self.keys(r"[\s\S]*") if await self.get(k)}
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "cloud-python-vars"
7
- version = "0.1.1"
7
+ version = "0.2.0"
8
8
  authors = [
9
9
  { name="ThatOnePythonDev", email="khakhang2h5e@gmail.com" },
10
10
  ]