persistentvalues 0.0.1__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.
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from hashlib import sha256
|
|
2
|
+
import os
|
|
3
|
+
import __main__
|
|
4
|
+
from os import PathLike
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from cachier import cachier
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PersistentValue:
|
|
11
|
+
|
|
12
|
+
def __init__(self, value: Any=None, id: str=None, access: str | int="script", cache_dir: str | PathLike | None=None, separate_files: bool=False):
|
|
13
|
+
"""
|
|
14
|
+
The PersistentValue object. Stores the value in the cache with access determined by the access value.
|
|
15
|
+
|
|
16
|
+
:param value: The value to store
|
|
17
|
+
:param id: Unique identifier of the persistent value
|
|
18
|
+
:param access: Whether to store the value locally for only the current script ("script" or 0), locally for the current working directory ("cwd" or "local" or 1), or globally across the whole device ("global" or 2).
|
|
19
|
+
:param cache_dir: (Default automatic) The directory to store the cached value in. Default relies on access, "~/.cachier/" for global, ".cache/" for the rest
|
|
20
|
+
:param separate_files: Instead of a single cache file per-function, each function's cache is split between several files, one for each argument set. This can help if your per-function cache files become too large.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
self.cache_dir = cache_dir or (None if access in ("global", 2, "2") else ".cache/")
|
|
24
|
+
self.cached = True
|
|
25
|
+
|
|
26
|
+
@cachier(cache_dir=self.cache_dir, separate_files=separate_files)
|
|
27
|
+
def get_value(id):
|
|
28
|
+
_ = id
|
|
29
|
+
self.cached = False
|
|
30
|
+
return self._value
|
|
31
|
+
self.get_value = get_value
|
|
32
|
+
|
|
33
|
+
if access not in (0, 1, 2, "0", "1", "2", "script", "cwd", "local", "global"):
|
|
34
|
+
raise ValueError("Access must be 0, 1, 2, '0', '1', '2', 'script', 'cwd', 'local', or 'global'.")
|
|
35
|
+
|
|
36
|
+
if not hasattr(__main__, "__file__"):
|
|
37
|
+
access = 1
|
|
38
|
+
|
|
39
|
+
self.id = sha256((repr(id if id is not None else value) + (__main__.__file__ if access in ("script", 0, "0") else (os.getcwd() if access in ("cwd", "local", 1, "1") else ""))).encode()).hexdigest()
|
|
40
|
+
|
|
41
|
+
self._value = value
|
|
42
|
+
|
|
43
|
+
def __setattr__(self, key, value):
|
|
44
|
+
if key in ['_value', 'id', 'value', 'cache_dir', 'get_value', 'cached']:
|
|
45
|
+
return object.__setattr__(self, key, value)
|
|
46
|
+
return setattr(self.value, key, value)
|
|
47
|
+
|
|
48
|
+
def __getattr__(self, name):
|
|
49
|
+
return getattr(self.value, name)
|
|
50
|
+
|
|
51
|
+
def __getitem__(self, key):
|
|
52
|
+
return self.value[key]
|
|
53
|
+
|
|
54
|
+
def __setitem__(self, key, value):
|
|
55
|
+
new_value = self.value.copy()
|
|
56
|
+
new_value[key] = value
|
|
57
|
+
self.value = new_value
|
|
58
|
+
|
|
59
|
+
def set(self, value):
|
|
60
|
+
self.value = value
|
|
61
|
+
|
|
62
|
+
def sync(self):
|
|
63
|
+
self._value = self.value
|
|
64
|
+
|
|
65
|
+
def update_cache(self):
|
|
66
|
+
self.get_value(self.id, cachier__overwrite_cache=True)
|
|
67
|
+
|
|
68
|
+
def clear_cache(self):
|
|
69
|
+
self.get_value.clear_cache()
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def value(self):
|
|
73
|
+
return self.get_value(self.id)
|
|
74
|
+
|
|
75
|
+
@value.setter
|
|
76
|
+
def value(self, value):
|
|
77
|
+
self._value = value
|
|
78
|
+
self.update_cache()
|
|
79
|
+
|
|
80
|
+
def __call__(self):
|
|
81
|
+
return self.value
|
|
82
|
+
|
|
83
|
+
def __eq__(self, other):
|
|
84
|
+
return object.__eq__(self.value, other)
|
|
85
|
+
|
|
86
|
+
def __hash__(self):
|
|
87
|
+
return self.value.__hash__()
|
|
88
|
+
|
|
89
|
+
def __str__(self):
|
|
90
|
+
return self.value.__str__()
|
|
91
|
+
|
|
92
|
+
def __repr__(self):
|
|
93
|
+
return self.value.__repr__()
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: persistentvalues
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A really tiny cachier wrapper for persistent value storage
|
|
5
|
+
Project-URL: Homepage, https://github.com/arukusuiinpu/persistentvalues
|
|
6
|
+
Project-URL: Issues, https://github.com/arukusuiinpu/persistentvalues/issues
|
|
7
|
+
Author-email: arukusuiinpu <Alex.Nompe@yandex.ru>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Requires-Dist: cachier
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# persistentvalues
|
|
18
|
+
|
|
19
|
+
Contains a PersistentValue object that stores the input object as self.value and allows you to interact with it directly as if the object is its own value.
|
|
20
|
+
|
|
21
|
+
persistentValue.(any function attribute that modifies its own value) <- doesn't work, the object requires explicit assignments to update the value and cache it.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
To modify the stored value, use:
|
|
25
|
+
```pycon
|
|
26
|
+
persistentValue.value = (something)
|
|
27
|
+
```
|
|
28
|
+
or
|
|
29
|
+
```pycon
|
|
30
|
+
persistentValue.set((something))
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
To get the stored value, use:
|
|
35
|
+
```pycon
|
|
36
|
+
(variable) = persistentValue.value
|
|
37
|
+
```
|
|
38
|
+
or
|
|
39
|
+
```pycon
|
|
40
|
+
(variable) = persistentValue()
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
The input value of PersistentValue(0) is an id determining what cache corresponds to which PersistentValue. That means the following code will use the same cached value despite the intention obviously being in creation of two different persistent values:
|
|
45
|
+
```pycon
|
|
46
|
+
a = PersistentValue(0) # <- create a new persistentValue
|
|
47
|
+
b = PersistentValue(0) # <- has the same id as the previous one, so behaves like the same object
|
|
48
|
+
|
|
49
|
+
a += 2 # <- now stores 2
|
|
50
|
+
b += 3 # <- now stores 5
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
You can define your own id using:
|
|
55
|
+
```pycon
|
|
56
|
+
a = PersistentValue(0, id=(your id))
|
|
57
|
+
```
|
|
58
|
+
Which means:
|
|
59
|
+
```pycon
|
|
60
|
+
a = PersistentValue(0, "a") # <- create a new persistentValue with id "a"
|
|
61
|
+
b = PersistentValue(0, "b") # <- create a new persistentValue with id "b"
|
|
62
|
+
|
|
63
|
+
a += 2 # <- "a" now stores 2
|
|
64
|
+
b += 3 # <- "b" now stores 3
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
Your persistent values may have different types of access depending on your intentions:
|
|
69
|
+
```pycon
|
|
70
|
+
acc_0 = PersistentValue(None, id="acc_0", access=("script" or 0))
|
|
71
|
+
"""
|
|
72
|
+
^ (Default) Local script access, values with the same id from different scripts don't overlap.
|
|
73
|
+
Python console is the exception, it is actually access=1 by default.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
acc_1 = PersistentValue(None, id="acc_1", access=("cwd" or "local" or 1))
|
|
77
|
+
"""
|
|
78
|
+
^ Local current working directory access, values with the same id from different working directories
|
|
79
|
+
don't overlap, but can be accessed across different scripts from the same working directory.
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
acc_2 = PersistentValue(None, id="acc_2", access=("global" or 2))
|
|
83
|
+
"""
|
|
84
|
+
^ Global device access, any values with the same id will be accessible across any scripts on your
|
|
85
|
+
device from any projects as long as they all use the same cache_dir folder (Default "~/.cachier/").
|
|
86
|
+
Requires access to the cache_dir from the script context.
|
|
87
|
+
"""
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
Additional parameters:
|
|
92
|
+
```pycon
|
|
93
|
+
PersistentValue(cache_dir=(your path))
|
|
94
|
+
"""
|
|
95
|
+
^ Cache path to store values at. Overrides the default automatic one determined by access.
|
|
96
|
+
|
|
97
|
+
Default:
|
|
98
|
+
access=0 -> ".cache/"
|
|
99
|
+
access=1 -> ".cache/"
|
|
100
|
+
access=2 -> "~/.cachier/"
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
PersistentValue(separate_files=(True or False))
|
|
104
|
+
"""
|
|
105
|
+
^ Instead of a single cache file per-function, each function's cache is split between
|
|
106
|
+
several files, one for each argument set. This can help if your per-function cache
|
|
107
|
+
files become too large.
|
|
108
|
+
|
|
109
|
+
Default: False
|
|
110
|
+
"""
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
PersistentValue functions:
|
|
115
|
+
```pycon
|
|
116
|
+
persistentValue.sync() # <- Syncs the uncached stored value with the cached one. (sets self._value = (cached)self.value)
|
|
117
|
+
|
|
118
|
+
persistentValue.update_cache() # <- Syncs the cached stored value with the uncached one. (sets (cached)self.value = self._value)
|
|
119
|
+
|
|
120
|
+
persistentValue.clear_cache() # <- Clears the cached value
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
Example usage:
|
|
125
|
+
```python
|
|
126
|
+
import random
|
|
127
|
+
from persistentvalues import PersistentValue
|
|
128
|
+
|
|
129
|
+
a = """Option 0
|
|
130
|
+
Option 1
|
|
131
|
+
Option 2
|
|
132
|
+
Option 3
|
|
133
|
+
Option 4
|
|
134
|
+
Option 5
|
|
135
|
+
Option 6""".split("\n")
|
|
136
|
+
|
|
137
|
+
b = PersistentValue([(i, 0) for i in a], "b") # <- store the initial value under id "b"
|
|
138
|
+
last = PersistentValue([None for _ in range(2)]) # <- store the initial value under procedural id derived from the initial value
|
|
139
|
+
|
|
140
|
+
def get_random_activity():
|
|
141
|
+
global last, b
|
|
142
|
+
ch = random.choices(b(), weights=[(1 / j if i not in last() else 0) if j > 0 else 1 for i, j in b()], k=1)[0] # <- get the values here and use them just like the initial ones
|
|
143
|
+
|
|
144
|
+
b[b.index(ch)] = (ch[0], ch[1] + 1) # <- modify b using assignment
|
|
145
|
+
|
|
146
|
+
last.value = [ch[0]] + last()[:-1] # <- modify last using assignment
|
|
147
|
+
|
|
148
|
+
return ch[0]
|
|
149
|
+
|
|
150
|
+
if __name__ == "__main__":
|
|
151
|
+
print(get_random_activity()) # <- each
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Console Example:
|
|
155
|
+
```pycon
|
|
156
|
+
>>> from persistentvalues import PersistentValue
|
|
157
|
+
>>> a = PersistentValue(0)
|
|
158
|
+
>>> a.value += 1
|
|
159
|
+
>>> a
|
|
160
|
+
1
|
|
161
|
+
|
|
162
|
+
Process finished with exit code 0
|
|
163
|
+
|
|
164
|
+
>>> from persistentvalues import PersistentValue
|
|
165
|
+
>>> a = PersistentValue(0)
|
|
166
|
+
>>> a
|
|
167
|
+
1
|
|
168
|
+
|
|
169
|
+
Process finished with exit code 0
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Local current working directory access:
|
|
173
|
+
```pycon
|
|
174
|
+
# ./test_pers0.py
|
|
175
|
+
|
|
176
|
+
from persistentvalues import PersistentValue
|
|
177
|
+
|
|
178
|
+
a = PersistentValue(0, access=1)
|
|
179
|
+
|
|
180
|
+
a.value += 1
|
|
181
|
+
|
|
182
|
+
print(a)
|
|
183
|
+
1
|
|
184
|
+
|
|
185
|
+
Process finished with exit code 0
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
```pycon
|
|
189
|
+
# ./test_pers1.py
|
|
190
|
+
|
|
191
|
+
from persistentvalues import PersistentValue
|
|
192
|
+
|
|
193
|
+
a = PersistentValue(0, access=1)
|
|
194
|
+
|
|
195
|
+
print(a)
|
|
196
|
+
1
|
|
197
|
+
|
|
198
|
+
Process finished with exit code 0
|
|
199
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
persistentvalues/__init__.py,sha256=m7SyJmOrDFSl5s3XRYJ-8u1dsVMnrYqTSy1auoNNahw,86
|
|
2
|
+
persistentvalues/persistentvalues.py,sha256=y-kHdUwcPewgUquRbhcNMxGI_0g-1dP3PQENZflKGxA,3345
|
|
3
|
+
persistentvalues-0.0.1.dist-info/METADATA,sha256=yekJ3l_COpyrr_qUxKTeUYqNlNq80Rmkt3X7KTxXpaI,5391
|
|
4
|
+
persistentvalues-0.0.1.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
|
|
5
|
+
persistentvalues-0.0.1.dist-info/licenses/LICENSE,sha256=E0aq-h-uxaHh__yC8OQvSLv_LbCAv9_AdALvB64WvVg,1090
|
|
6
|
+
persistentvalues-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 arukusuiinpu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|