-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpickle.py
More file actions
80 lines (64 loc) · 2.47 KB
/
pickle.py
File metadata and controls
80 lines (64 loc) · 2.47 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
"""
Pickle
------
The Pickle module provides a pickle-based version of the :py:class:`.DBContextStorage` class.
This class is used to store and retrieve context data in a pickle format.
It allows the `DFF` to easily store and retrieve context data in a format that is efficient
for serialization and deserialization and can be easily used in python.
Pickle is a python library that allows to serialize and deserialize python objects.
It is efficient and fast, but it is not recommended to use it to transfer data across
different languages or platforms because it's not cross-language compatible.
"""
import asyncio
import pickle
from typing import Hashable
try:
import aiofiles
import aiofiles.os
pickle_available = True
except ImportError:
pickle_available = False
from .database import DBContextStorage, threadsafe_method
from dff.script import Context
class PickleContextStorage(DBContextStorage):
"""
Implements :py:class:`.DBContextStorage` with `pickle` as driver.
:param path: Target file URI. Example: 'pickle://file.pkl'.
:type path: str
"""
def __init__(self, path: str):
DBContextStorage.__init__(self, path)
asyncio.run(self._load())
@threadsafe_method
async def len_async(self) -> int:
return len(self.dict)
@threadsafe_method
async def set_item_async(self, key: Hashable, value: Context):
self.dict.__setitem__(str(key), value)
await self._save()
@threadsafe_method
async def get_item_async(self, key: Hashable) -> Context:
await self._load()
return Context.cast(self.dict.__getitem__(str(key)))
@threadsafe_method
async def del_item_async(self, key: Hashable):
self.dict.__delitem__(str(key))
await self._save()
@threadsafe_method
async def contains_async(self, key: Hashable) -> bool:
await self._load()
return self.dict.__contains__(str(key))
@threadsafe_method
async def clear_async(self):
self.dict.clear()
await self._save()
async def _save(self):
async with aiofiles.open(self.path, "wb+") as file:
await file.write(pickle.dumps(self.dict))
async def _load(self):
if not await aiofiles.os.path.isfile(self.path) or (await aiofiles.os.stat(self.path)).st_size == 0:
self.dict = dict()
await self._save()
else:
async with aiofiles.open(self.path, "rb") as file:
self.dict = pickle.loads(await file.read())