memory-oc 1.0.2__tar.gz → 1.0.4__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.
- {memory_oc-1.0.2 → memory_oc-1.0.4}/PKG-INFO +91 -29
- {memory_oc-1.0.2 → memory_oc-1.0.4}/README.md +88 -26
- {memory_oc-1.0.2 → memory_oc-1.0.4}/memory/__init__.py +143 -49
- {memory_oc-1.0.2 → memory_oc-1.0.4}/pyproject.toml +2 -2
- {memory_oc-1.0.2 → memory_oc-1.0.4}/.gitignore +0 -0
- {memory_oc-1.0.2 → memory_oc-1.0.4}/LICENSE +0 -0
- {memory_oc-1.0.2 → memory_oc-1.0.4}/memory/__main__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: memory_oc
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.4
|
|
4
4
|
Summary: A session library using redis
|
|
5
5
|
Project-URL: Source, https://github.com/ouroboroscoding/memory-python
|
|
6
6
|
Project-URL: Tracker, https://github.com/ouroboroscoding/memory-python/issues
|
|
@@ -9,7 +9,7 @@ Maintainer-email: "Chris Nasr - Ouroboros Coding Inc." <chris@ouroboroscoding.co
|
|
|
9
9
|
License-Expression: MIT
|
|
10
10
|
License-File: LICENSE
|
|
11
11
|
Requires-Python: >=3.10
|
|
12
|
-
Requires-Dist: jobject<1.1,>=1.0.
|
|
12
|
+
Requires-Dist: jobject<1.1,>=1.0.5
|
|
13
13
|
Requires-Dist: json-fix<1.1,>=1.0.0
|
|
14
14
|
Requires-Dist: jsonb<1.1,>=1.0.0
|
|
15
15
|
Requires-Dist: namedredis<1.1,>=1.0.2
|
|
@@ -32,15 +32,18 @@ for changes from release to release.
|
|
|
32
32
|
- [Module Install](#module-install)
|
|
33
33
|
- [Configuration](#configuration)
|
|
34
34
|
- [Methods](#methods)
|
|
35
|
-
- [close](#
|
|
36
|
-
- [create](#
|
|
37
|
-
- [
|
|
35
|
+
- [close](#close)
|
|
36
|
+
- [create](#create)
|
|
37
|
+
- [from_dict](#from_dict)
|
|
38
|
+
- [load](#load)
|
|
38
39
|
- [Class](#class)
|
|
39
|
-
- [close](#
|
|
40
|
-
- [extend](#
|
|
41
|
-
- [key](#
|
|
42
|
-
- [save](#
|
|
43
|
-
- [
|
|
40
|
+
- [close](#close-1)
|
|
41
|
+
- [extend](#extend)
|
|
42
|
+
- [key](#key)
|
|
43
|
+
- [save](#save)
|
|
44
|
+
- [to_dict](#to_dict)
|
|
45
|
+
- [ttl](#ttl)
|
|
46
|
+
- [update](#update)
|
|
44
47
|
|
|
45
48
|
## Module Install
|
|
46
49
|
|
|
@@ -79,12 +82,12 @@ The `redis` section lists Redis connections by name and details to connect. Then
|
|
|
79
82
|
[ [top](#memory_oc) / [contents](#contents) ]
|
|
80
83
|
|
|
81
84
|
## Methods
|
|
82
|
-
Memory comes with
|
|
85
|
+
Memory comes with four methods, to [`close`](#close), [`create`](#create),
|
|
86
|
+
[`from_dict`](#from_dict) and [`load`](#load) sessions.
|
|
83
87
|
|
|
84
88
|
### close
|
|
85
89
|
Immediately closes and deletes a session from Redis.
|
|
86
90
|
|
|
87
|
-
#### example
|
|
88
91
|
```python
|
|
89
92
|
import memory
|
|
90
93
|
|
|
@@ -92,6 +95,8 @@ import memory
|
|
|
92
95
|
memory.close(somekey)
|
|
93
96
|
```
|
|
94
97
|
|
|
98
|
+
[ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
|
|
99
|
+
|
|
95
100
|
### create
|
|
96
101
|
Creates a new session. Takes 3 optional arguments
|
|
97
102
|
|
|
@@ -101,7 +106,6 @@ Creates a new session. Takes 3 optional arguments
|
|
|
101
106
|
| `ttl` | unsigned integer | The time to live in seconds for the session. |
|
|
102
107
|
| `data` | dict | The initial data to be stored in the session. |
|
|
103
108
|
|
|
104
|
-
#### example
|
|
105
109
|
```python
|
|
106
110
|
import memory
|
|
107
111
|
|
|
@@ -118,10 +122,29 @@ session.save()
|
|
|
118
122
|
return session.key()
|
|
119
123
|
```
|
|
120
124
|
|
|
125
|
+
[ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
|
|
126
|
+
|
|
127
|
+
### from_dict
|
|
128
|
+
Returns a new instance generated from a dict returned by [`to_dict`](#instanceto_dict)
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
import memory
|
|
132
|
+
|
|
133
|
+
# Load a session
|
|
134
|
+
session = memory.load(somekey)
|
|
135
|
+
|
|
136
|
+
# Convert to a dict
|
|
137
|
+
d = session.to_dict()
|
|
138
|
+
|
|
139
|
+
# Revert to a session
|
|
140
|
+
session_copy = memory.from_dict(d)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
[ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
|
|
144
|
+
|
|
121
145
|
### load
|
|
122
146
|
Loads a session from a passed key.
|
|
123
147
|
|
|
124
|
-
#### example
|
|
125
148
|
```python
|
|
126
149
|
import memory
|
|
127
150
|
|
|
@@ -135,16 +158,15 @@ session['name'] = 'Samuel'
|
|
|
135
158
|
session.save()
|
|
136
159
|
```
|
|
137
160
|
|
|
138
|
-
[ [top](#memory_oc) / [contents](#contents) ]
|
|
161
|
+
[ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
|
|
139
162
|
|
|
140
163
|
## Class
|
|
141
|
-
The class is what is returned from the [create](#create)
|
|
142
|
-
[load](#load) methods
|
|
164
|
+
The class is what is returned from the [`create`](#create),
|
|
165
|
+
[`from_dict`](#from_dict), and [`load`](#load) methods
|
|
143
166
|
|
|
144
|
-
###
|
|
167
|
+
### close
|
|
145
168
|
Closes the session in Redis, making it no longer available to anyone.
|
|
146
169
|
|
|
147
|
-
#### example
|
|
148
170
|
```python
|
|
149
171
|
import memory
|
|
150
172
|
|
|
@@ -155,10 +177,11 @@ session = memory.load(somekey)
|
|
|
155
177
|
session.close()
|
|
156
178
|
```
|
|
157
179
|
|
|
158
|
-
|
|
180
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
181
|
+
|
|
182
|
+
### extend
|
|
159
183
|
Extends the TTL of the session by the initial TTL given.
|
|
160
184
|
|
|
161
|
-
#### exmaple
|
|
162
185
|
```python
|
|
163
186
|
import memory
|
|
164
187
|
|
|
@@ -169,10 +192,11 @@ session = memory.load(somekey)
|
|
|
169
192
|
session.extend()
|
|
170
193
|
```
|
|
171
194
|
|
|
172
|
-
|
|
195
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
196
|
+
|
|
197
|
+
### key
|
|
173
198
|
Returns the key of the session.
|
|
174
199
|
|
|
175
|
-
#### example
|
|
176
200
|
```python
|
|
177
201
|
import memory
|
|
178
202
|
|
|
@@ -183,10 +207,11 @@ session = memory.create()
|
|
|
183
207
|
print(session.key())
|
|
184
208
|
```
|
|
185
209
|
|
|
186
|
-
|
|
210
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
211
|
+
|
|
212
|
+
### save
|
|
187
213
|
Stores changes in Redis after updates.
|
|
188
214
|
|
|
189
|
-
#### example
|
|
190
215
|
```python
|
|
191
216
|
import memory
|
|
192
217
|
|
|
@@ -200,10 +225,47 @@ session['name'] = 'Samuel'
|
|
|
200
225
|
session.save()
|
|
201
226
|
```
|
|
202
227
|
|
|
203
|
-
|
|
228
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
229
|
+
|
|
230
|
+
### to_dict
|
|
231
|
+
Returns a `dict` representation of the instance which can be printed / converted
|
|
232
|
+
or converted to json. Can be turned back into an instance by passing it to
|
|
233
|
+
[from_dict](#instancefrom_dict)
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
import memory
|
|
237
|
+
|
|
238
|
+
# Load a session
|
|
239
|
+
session = memory.load(somekey)
|
|
240
|
+
|
|
241
|
+
# Print to console
|
|
242
|
+
print(session.to_dict())
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
246
|
+
|
|
247
|
+
### ttl
|
|
248
|
+
Getter / Setter for the TTL on the session.
|
|
249
|
+
|
|
250
|
+
Set
|
|
251
|
+
```python
|
|
252
|
+
import memory
|
|
253
|
+
|
|
254
|
+
# Load session
|
|
255
|
+
session = memory.load(somekey)
|
|
256
|
+
|
|
257
|
+
# Returns the current ttl
|
|
258
|
+
session.ttl()
|
|
259
|
+
|
|
260
|
+
# Updates the ttl to 5 minutes and refreshes the session
|
|
261
|
+
session.ttl(300)
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
265
|
+
|
|
266
|
+
### update
|
|
204
267
|
Works the same as dict update() so you can store multiple keys at once
|
|
205
268
|
|
|
206
|
-
#### example
|
|
207
269
|
```python
|
|
208
270
|
import memory
|
|
209
271
|
|
|
@@ -220,4 +282,4 @@ session.update({
|
|
|
220
282
|
session.save()
|
|
221
283
|
```
|
|
222
284
|
|
|
223
|
-
[ [top](#memory_oc) / [contents](#contents) ]
|
|
285
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
@@ -14,15 +14,18 @@ for changes from release to release.
|
|
|
14
14
|
- [Module Install](#module-install)
|
|
15
15
|
- [Configuration](#configuration)
|
|
16
16
|
- [Methods](#methods)
|
|
17
|
-
- [close](#
|
|
18
|
-
- [create](#
|
|
19
|
-
- [
|
|
17
|
+
- [close](#close)
|
|
18
|
+
- [create](#create)
|
|
19
|
+
- [from_dict](#from_dict)
|
|
20
|
+
- [load](#load)
|
|
20
21
|
- [Class](#class)
|
|
21
|
-
- [close](#
|
|
22
|
-
- [extend](#
|
|
23
|
-
- [key](#
|
|
24
|
-
- [save](#
|
|
25
|
-
- [
|
|
22
|
+
- [close](#close-1)
|
|
23
|
+
- [extend](#extend)
|
|
24
|
+
- [key](#key)
|
|
25
|
+
- [save](#save)
|
|
26
|
+
- [to_dict](#to_dict)
|
|
27
|
+
- [ttl](#ttl)
|
|
28
|
+
- [update](#update)
|
|
26
29
|
|
|
27
30
|
## Module Install
|
|
28
31
|
|
|
@@ -61,12 +64,12 @@ The `redis` section lists Redis connections by name and details to connect. Then
|
|
|
61
64
|
[ [top](#memory_oc) / [contents](#contents) ]
|
|
62
65
|
|
|
63
66
|
## Methods
|
|
64
|
-
Memory comes with
|
|
67
|
+
Memory comes with four methods, to [`close`](#close), [`create`](#create),
|
|
68
|
+
[`from_dict`](#from_dict) and [`load`](#load) sessions.
|
|
65
69
|
|
|
66
70
|
### close
|
|
67
71
|
Immediately closes and deletes a session from Redis.
|
|
68
72
|
|
|
69
|
-
#### example
|
|
70
73
|
```python
|
|
71
74
|
import memory
|
|
72
75
|
|
|
@@ -74,6 +77,8 @@ import memory
|
|
|
74
77
|
memory.close(somekey)
|
|
75
78
|
```
|
|
76
79
|
|
|
80
|
+
[ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
|
|
81
|
+
|
|
77
82
|
### create
|
|
78
83
|
Creates a new session. Takes 3 optional arguments
|
|
79
84
|
|
|
@@ -83,7 +88,6 @@ Creates a new session. Takes 3 optional arguments
|
|
|
83
88
|
| `ttl` | unsigned integer | The time to live in seconds for the session. |
|
|
84
89
|
| `data` | dict | The initial data to be stored in the session. |
|
|
85
90
|
|
|
86
|
-
#### example
|
|
87
91
|
```python
|
|
88
92
|
import memory
|
|
89
93
|
|
|
@@ -100,10 +104,29 @@ session.save()
|
|
|
100
104
|
return session.key()
|
|
101
105
|
```
|
|
102
106
|
|
|
107
|
+
[ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
|
|
108
|
+
|
|
109
|
+
### from_dict
|
|
110
|
+
Returns a new instance generated from a dict returned by [`to_dict`](#instanceto_dict)
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
import memory
|
|
114
|
+
|
|
115
|
+
# Load a session
|
|
116
|
+
session = memory.load(somekey)
|
|
117
|
+
|
|
118
|
+
# Convert to a dict
|
|
119
|
+
d = session.to_dict()
|
|
120
|
+
|
|
121
|
+
# Revert to a session
|
|
122
|
+
session_copy = memory.from_dict(d)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
[ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
|
|
126
|
+
|
|
103
127
|
### load
|
|
104
128
|
Loads a session from a passed key.
|
|
105
129
|
|
|
106
|
-
#### example
|
|
107
130
|
```python
|
|
108
131
|
import memory
|
|
109
132
|
|
|
@@ -117,16 +140,15 @@ session['name'] = 'Samuel'
|
|
|
117
140
|
session.save()
|
|
118
141
|
```
|
|
119
142
|
|
|
120
|
-
[ [top](#memory_oc) / [contents](#contents) ]
|
|
143
|
+
[ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
|
|
121
144
|
|
|
122
145
|
## Class
|
|
123
|
-
The class is what is returned from the [create](#create)
|
|
124
|
-
[load](#load) methods
|
|
146
|
+
The class is what is returned from the [`create`](#create),
|
|
147
|
+
[`from_dict`](#from_dict), and [`load`](#load) methods
|
|
125
148
|
|
|
126
|
-
###
|
|
149
|
+
### close
|
|
127
150
|
Closes the session in Redis, making it no longer available to anyone.
|
|
128
151
|
|
|
129
|
-
#### example
|
|
130
152
|
```python
|
|
131
153
|
import memory
|
|
132
154
|
|
|
@@ -137,10 +159,11 @@ session = memory.load(somekey)
|
|
|
137
159
|
session.close()
|
|
138
160
|
```
|
|
139
161
|
|
|
140
|
-
|
|
162
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
163
|
+
|
|
164
|
+
### extend
|
|
141
165
|
Extends the TTL of the session by the initial TTL given.
|
|
142
166
|
|
|
143
|
-
#### exmaple
|
|
144
167
|
```python
|
|
145
168
|
import memory
|
|
146
169
|
|
|
@@ -151,10 +174,11 @@ session = memory.load(somekey)
|
|
|
151
174
|
session.extend()
|
|
152
175
|
```
|
|
153
176
|
|
|
154
|
-
|
|
177
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
178
|
+
|
|
179
|
+
### key
|
|
155
180
|
Returns the key of the session.
|
|
156
181
|
|
|
157
|
-
#### example
|
|
158
182
|
```python
|
|
159
183
|
import memory
|
|
160
184
|
|
|
@@ -165,10 +189,11 @@ session = memory.create()
|
|
|
165
189
|
print(session.key())
|
|
166
190
|
```
|
|
167
191
|
|
|
168
|
-
|
|
192
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
193
|
+
|
|
194
|
+
### save
|
|
169
195
|
Stores changes in Redis after updates.
|
|
170
196
|
|
|
171
|
-
#### example
|
|
172
197
|
```python
|
|
173
198
|
import memory
|
|
174
199
|
|
|
@@ -182,10 +207,47 @@ session['name'] = 'Samuel'
|
|
|
182
207
|
session.save()
|
|
183
208
|
```
|
|
184
209
|
|
|
185
|
-
|
|
210
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
211
|
+
|
|
212
|
+
### to_dict
|
|
213
|
+
Returns a `dict` representation of the instance which can be printed / converted
|
|
214
|
+
or converted to json. Can be turned back into an instance by passing it to
|
|
215
|
+
[from_dict](#instancefrom_dict)
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
import memory
|
|
219
|
+
|
|
220
|
+
# Load a session
|
|
221
|
+
session = memory.load(somekey)
|
|
222
|
+
|
|
223
|
+
# Print to console
|
|
224
|
+
print(session.to_dict())
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
228
|
+
|
|
229
|
+
### ttl
|
|
230
|
+
Getter / Setter for the TTL on the session.
|
|
231
|
+
|
|
232
|
+
Set
|
|
233
|
+
```python
|
|
234
|
+
import memory
|
|
235
|
+
|
|
236
|
+
# Load session
|
|
237
|
+
session = memory.load(somekey)
|
|
238
|
+
|
|
239
|
+
# Returns the current ttl
|
|
240
|
+
session.ttl()
|
|
241
|
+
|
|
242
|
+
# Updates the ttl to 5 minutes and refreshes the session
|
|
243
|
+
session.ttl(300)
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
247
|
+
|
|
248
|
+
### update
|
|
186
249
|
Works the same as dict update() so you can store multiple keys at once
|
|
187
250
|
|
|
188
|
-
#### example
|
|
189
251
|
```python
|
|
190
252
|
import memory
|
|
191
253
|
|
|
@@ -202,4 +264,4 @@ session.update({
|
|
|
202
264
|
session.save()
|
|
203
265
|
```
|
|
204
266
|
|
|
205
|
-
[ [top](#memory_oc) / [contents](#contents) ]
|
|
267
|
+
[ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
|
|
@@ -11,7 +11,7 @@ __email__ = "chris@ouroboroscoding.com"
|
|
|
11
11
|
__created__ = "2023-03-15"
|
|
12
12
|
|
|
13
13
|
# Limit exports
|
|
14
|
-
__all__ = ['
|
|
14
|
+
__all__ = [ 'close', 'create', 'load' ]
|
|
15
15
|
|
|
16
16
|
# Ouroboros imports
|
|
17
17
|
from config import config
|
|
@@ -21,8 +21,11 @@ from nredis import nr
|
|
|
21
21
|
from strings import random
|
|
22
22
|
from tools import combine
|
|
23
23
|
|
|
24
|
+
# Python imports
|
|
25
|
+
from copy import deepcopy
|
|
26
|
+
|
|
24
27
|
# Pip imports
|
|
25
|
-
import json_fix
|
|
28
|
+
import json_fix # needed for __json__, do not remove
|
|
26
29
|
|
|
27
30
|
# Open redis connection
|
|
28
31
|
_moRedis = nr(config.memory.redis('session'))
|
|
@@ -37,16 +40,22 @@ def close(key: str):
|
|
|
37
40
|
def create(key: str = None, ttl: int = 0, data = None) -> _Memory:
|
|
38
41
|
"""Create
|
|
39
42
|
|
|
40
|
-
Returns a brand new session using the key given, else a
|
|
43
|
+
Returns a brand new session using the key given, else a random key is
|
|
44
|
+
generated.
|
|
41
45
|
|
|
42
46
|
Arguments:
|
|
43
47
|
key (str): The key to use for the session
|
|
44
48
|
ttl (uint): Time to live, a specific expiry time in seconds
|
|
49
|
+
data (dict): Initial data in the session
|
|
45
50
|
|
|
46
51
|
Returns:
|
|
47
52
|
_Memory
|
|
48
53
|
"""
|
|
49
54
|
|
|
55
|
+
# If the ttl is not an unsigned int
|
|
56
|
+
if isinstance(ttl, bool) or not isinstance(ttl, int) or ttl < 0:
|
|
57
|
+
raise ValueError('ttl must be an unsigned int')
|
|
58
|
+
|
|
50
59
|
# Generate the data to store by adding the TTL
|
|
51
60
|
dData = (
|
|
52
61
|
data is None
|
|
@@ -59,7 +68,7 @@ def create(key: str = None, ttl: int = 0, data = None) -> _Memory:
|
|
|
59
68
|
|
|
60
69
|
# If it exists
|
|
61
70
|
if _moRedis.exists(key):
|
|
62
|
-
raise RuntimeError('memory_oc
|
|
71
|
+
raise RuntimeError(f'memory_oc: "{key}" exists')
|
|
63
72
|
|
|
64
73
|
# Set the key
|
|
65
74
|
sKey = key
|
|
@@ -82,16 +91,29 @@ def create(key: str = None, ttl: int = 0, data = None) -> _Memory:
|
|
|
82
91
|
i += 1
|
|
83
92
|
if i > 10:
|
|
84
93
|
raise RuntimeError(
|
|
85
|
-
'memory_oc
|
|
94
|
+
'memory_oc, potential infinite loop in create()'
|
|
86
95
|
)
|
|
87
96
|
|
|
88
|
-
# Create a new Memory using the passed key, or a new
|
|
97
|
+
# Create a new Memory using the passed key, or a new random string
|
|
89
98
|
return _Memory(sKey, dData)
|
|
90
99
|
|
|
91
|
-
def
|
|
100
|
+
def from_dict(value: dict) -> _Memory:
|
|
101
|
+
"""From Dict
|
|
102
|
+
|
|
103
|
+
Creates an instance from a dict representation.
|
|
104
|
+
|
|
105
|
+
Arguments:
|
|
106
|
+
value (dict): The value to convert to an instance
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
_Memory
|
|
110
|
+
"""
|
|
111
|
+
return _Memory(value['__key'], value['__store'])
|
|
112
|
+
|
|
113
|
+
def load(key: str) -> _Memory | None:
|
|
92
114
|
"""Load
|
|
93
115
|
|
|
94
|
-
Loads an existing session from the cache
|
|
116
|
+
Loads an existing session from the cache, or None if it doesn't exist.
|
|
95
117
|
|
|
96
118
|
Arguments:
|
|
97
119
|
key (str): The unique id of an existing session
|
|
@@ -116,16 +138,16 @@ def load(key: str) -> _Memory:
|
|
|
116
138
|
class _Memory(object):
|
|
117
139
|
"""Memory
|
|
118
140
|
|
|
119
|
-
A wrapper for the session data
|
|
141
|
+
A wrapper for the session data.
|
|
120
142
|
|
|
121
143
|
Extends:
|
|
122
144
|
object
|
|
123
145
|
"""
|
|
124
146
|
|
|
125
|
-
def __init__(self, key: str, data: dict = None):
|
|
147
|
+
def __init__(self, key: str, data: dict | None = None) -> _Memory:
|
|
126
148
|
"""Constructor
|
|
127
149
|
|
|
128
|
-
Intialises the instance
|
|
150
|
+
Intialises the instance with a given key and optional data.
|
|
129
151
|
|
|
130
152
|
Arguments:
|
|
131
153
|
key (str): The key used to access or store the session
|
|
@@ -146,7 +168,7 @@ class _Memory(object):
|
|
|
146
168
|
def __contains__(self, key: str):
|
|
147
169
|
"""__contains__
|
|
148
170
|
|
|
149
|
-
True if the key exists in the session
|
|
171
|
+
True if the key exists in the session.
|
|
150
172
|
|
|
151
173
|
Arguments:
|
|
152
174
|
key (str): The field to check for
|
|
@@ -156,20 +178,25 @@ class _Memory(object):
|
|
|
156
178
|
"""
|
|
157
179
|
return object.__getattribute__(self, '__store').__contains__(key)
|
|
158
180
|
|
|
159
|
-
def __delitem__(self, k):
|
|
181
|
+
def __delitem__(self, k: str):
|
|
160
182
|
"""__delete__
|
|
161
183
|
|
|
162
|
-
Removes a key from a session
|
|
184
|
+
Removes a key from a session.
|
|
163
185
|
|
|
164
186
|
Arguments:
|
|
165
187
|
k (str): The key to remove
|
|
166
188
|
"""
|
|
189
|
+
|
|
190
|
+
# Don't allow special keys to be changed
|
|
191
|
+
if isinstance(k, str) and k.startswith('__'):
|
|
192
|
+
raise KeyError(f'{k} is read-only')
|
|
193
|
+
|
|
167
194
|
del object.__getattribute__(self, '__store')[k]
|
|
168
195
|
|
|
169
196
|
def __getattr__(self, a: str) -> any:
|
|
170
197
|
"""__getattr__
|
|
171
198
|
|
|
172
|
-
Gives object notation access to get the internal dict keys
|
|
199
|
+
Gives object notation access to get the internal dict keys.
|
|
173
200
|
|
|
174
201
|
Arguments:
|
|
175
202
|
a (str): The attribute to get
|
|
@@ -180,15 +207,23 @@ class _Memory(object):
|
|
|
180
207
|
Returns:
|
|
181
208
|
any
|
|
182
209
|
"""
|
|
210
|
+
|
|
211
|
+
# Check for store, likely to exist, but necessary for some libraries
|
|
212
|
+
try:
|
|
213
|
+
dStore = object.__getattribute__(self, '__store')
|
|
214
|
+
except AttributeError:
|
|
215
|
+
raise AttributeError(a) from None
|
|
216
|
+
|
|
217
|
+
# Try the actual attribute
|
|
183
218
|
try:
|
|
184
|
-
return
|
|
219
|
+
return dStore[a]
|
|
185
220
|
except KeyError:
|
|
186
|
-
raise AttributeError(a
|
|
221
|
+
raise AttributeError(f'{a} not in Memory instance') from None
|
|
187
222
|
|
|
188
223
|
def __getitem__(self, k):
|
|
189
224
|
"""__getitem__
|
|
190
225
|
|
|
191
|
-
Returns the given key
|
|
226
|
+
Returns the given key.
|
|
192
227
|
|
|
193
228
|
Arguments:
|
|
194
229
|
k (str): The key to return
|
|
@@ -201,7 +236,7 @@ class _Memory(object):
|
|
|
201
236
|
def __iter__(self):
|
|
202
237
|
"""__iter__
|
|
203
238
|
|
|
204
|
-
Returns an iterator for the internal dict
|
|
239
|
+
Returns an iterator for the internal dict.
|
|
205
240
|
|
|
206
241
|
Returns:
|
|
207
242
|
iterator
|
|
@@ -211,20 +246,17 @@ class _Memory(object):
|
|
|
211
246
|
def __json__(self):
|
|
212
247
|
"""__json__
|
|
213
248
|
|
|
214
|
-
Returns a dict representation of the session
|
|
249
|
+
Returns a dict representation of the session.
|
|
215
250
|
|
|
216
251
|
Returns:
|
|
217
252
|
dict
|
|
218
253
|
"""
|
|
219
|
-
return
|
|
220
|
-
'__key': object.__getattribute__(self, '__key'),
|
|
221
|
-
'__store': object.__getattribute__(self, '__store')
|
|
222
|
-
}
|
|
254
|
+
return self.to_dict()
|
|
223
255
|
|
|
224
256
|
def __len__(self):
|
|
225
257
|
"""__len__
|
|
226
258
|
|
|
227
|
-
Return the length of the internal dict
|
|
259
|
+
Return the length of the internal dict.
|
|
228
260
|
|
|
229
261
|
Returns:
|
|
230
262
|
uint
|
|
@@ -234,29 +266,41 @@ class _Memory(object):
|
|
|
234
266
|
def __setattr__(self, a: str, v: any) -> None:
|
|
235
267
|
"""__setattr__
|
|
236
268
|
|
|
237
|
-
Gives object notation access to set the internal dict keys
|
|
269
|
+
Gives object notation access to set the internal dict keys.
|
|
238
270
|
|
|
239
271
|
Arguments:
|
|
240
272
|
a (str): The key in the dict to set
|
|
241
273
|
v (any): The value to set on the key
|
|
242
274
|
"""
|
|
275
|
+
|
|
276
|
+
# Don't allow special keys to be changed
|
|
277
|
+
if a.startswith('__'):
|
|
278
|
+
raise KeyError(f'{a} is read-only')
|
|
279
|
+
|
|
280
|
+
# Set the item
|
|
243
281
|
object.__getattribute__(self, '__store').__setitem__(a, v)
|
|
244
282
|
|
|
245
|
-
def __setitem__(self, k, v):
|
|
283
|
+
def __setitem__(self, k: str, v: any):
|
|
246
284
|
"""__setitem__
|
|
247
285
|
|
|
248
|
-
Sets the given key
|
|
286
|
+
Sets the given key.
|
|
249
287
|
|
|
250
288
|
Arguments:
|
|
251
289
|
k (str): The key to set
|
|
252
290
|
v (any): The value for the key
|
|
253
291
|
"""
|
|
292
|
+
|
|
293
|
+
# Don't allow special keys to be changed
|
|
294
|
+
if isinstance(k, str) and k.startswith('__'):
|
|
295
|
+
raise KeyError(f'{k} is read-only')
|
|
296
|
+
|
|
297
|
+
# Set the item
|
|
254
298
|
object.__getattribute__(self, '__store').__setitem__(k, v)
|
|
255
299
|
|
|
256
300
|
def __str__(self):
|
|
257
301
|
"""__str__
|
|
258
302
|
|
|
259
|
-
Returns a string representation of the internal dict
|
|
303
|
+
Returns a string representation of the internal dict.
|
|
260
304
|
|
|
261
305
|
Returns:
|
|
262
306
|
str
|
|
@@ -266,31 +310,31 @@ class _Memory(object):
|
|
|
266
310
|
def close(self):
|
|
267
311
|
"""Close
|
|
268
312
|
|
|
269
|
-
Deletes the session from the cache
|
|
313
|
+
Deletes the session from the cache.
|
|
270
314
|
"""
|
|
271
315
|
_moRedis.delete(object.__getattribute__(self, '__key'))
|
|
272
316
|
|
|
273
317
|
def extend(self):
|
|
274
318
|
"""Extend
|
|
275
319
|
|
|
276
|
-
Keep the session alive by extending it's expire time by the internally
|
|
277
|
-
set expire value, or else by the global one set for the module
|
|
320
|
+
Keep the session alive by extending it's expire time by the internally
|
|
321
|
+
set expire value, or else by the global one set for the module.
|
|
278
322
|
"""
|
|
279
323
|
|
|
324
|
+
# Get the store
|
|
325
|
+
dStore = object.__getattribute__(self, '__store')
|
|
326
|
+
|
|
280
327
|
# If the expire time is 0, do nothing
|
|
281
|
-
if
|
|
328
|
+
if dStore['__ttl'] == 0:
|
|
282
329
|
return
|
|
283
330
|
|
|
284
331
|
# Extend the session in Redis
|
|
285
|
-
_moRedis.expire(
|
|
286
|
-
object.__getattribute__(self, '__key'),
|
|
287
|
-
object.__getattribute__(self, '__store')['__ttl']
|
|
288
|
-
)
|
|
332
|
+
_moRedis.expire(object.__getattribute__(self, '__key'), dStore['__ttl'])
|
|
289
333
|
|
|
290
|
-
def key(self):
|
|
334
|
+
def key(self) -> str:
|
|
291
335
|
"""Key
|
|
292
336
|
|
|
293
|
-
Returns the key of the session
|
|
337
|
+
Returns the key of the session.
|
|
294
338
|
|
|
295
339
|
Returns:
|
|
296
340
|
str
|
|
@@ -300,31 +344,81 @@ class _Memory(object):
|
|
|
300
344
|
def save(self):
|
|
301
345
|
"""Save
|
|
302
346
|
|
|
303
|
-
Saves the current session data in the cache
|
|
347
|
+
Saves the current session data in the cache.
|
|
304
348
|
"""
|
|
305
349
|
|
|
350
|
+
# Get the store
|
|
351
|
+
dStore = object.__getattribute__(self, '__store')
|
|
352
|
+
|
|
353
|
+
# Encode it
|
|
354
|
+
dJSON = jsonb.encode(dStore)
|
|
355
|
+
|
|
306
356
|
# If we have no expire time, set forever
|
|
307
|
-
if
|
|
308
|
-
_moRedis.set(
|
|
309
|
-
object.__getattribute__(self, '__key'),
|
|
310
|
-
jsonb.encode(object.__getattribute__(self, '__store'))
|
|
311
|
-
)
|
|
357
|
+
if dStore['__ttl'] == 0:
|
|
358
|
+
_moRedis.set(object.__getattribute__(self, '__key'), dJSON)
|
|
312
359
|
|
|
313
360
|
# Else, set to expire
|
|
314
361
|
else:
|
|
315
362
|
_moRedis.setex(
|
|
316
363
|
object.__getattribute__(self, '__key'),
|
|
317
|
-
|
|
318
|
-
|
|
364
|
+
dStore['__ttl'],
|
|
365
|
+
dJSON
|
|
319
366
|
)
|
|
320
367
|
|
|
368
|
+
def to_dict(self) -> dict:
|
|
369
|
+
"""To Dict
|
|
370
|
+
|
|
371
|
+
Returns the instance represented as a dict which can be turned back
|
|
372
|
+
into an instance using from_dict().
|
|
373
|
+
|
|
374
|
+
Returns:
|
|
375
|
+
dict
|
|
376
|
+
"""
|
|
377
|
+
return {
|
|
378
|
+
'__key': object.__getattribute__(self, '__key'),
|
|
379
|
+
'__store': deepcopy(object.__getattribute__(self, '__store'))
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
def ttl(self, ttl: int | None = None) -> int | None:
|
|
383
|
+
"""TTL
|
|
384
|
+
|
|
385
|
+
Getter / Setter for the Time To Live in seconds of the session.
|
|
386
|
+
|
|
387
|
+
Arguments:
|
|
388
|
+
ttl (uint | None): Do not set to fetch the current value, set to 0
|
|
389
|
+
(zero) for a session that never expires, set to anything greater
|
|
390
|
+
than 0 (zero) to have it expired in `ttl` seconds
|
|
391
|
+
|
|
392
|
+
Returns:
|
|
393
|
+
uint | None
|
|
394
|
+
"""
|
|
395
|
+
|
|
396
|
+
# Get the store
|
|
397
|
+
dStore = object.__getattribute__(self, '__store')
|
|
398
|
+
|
|
399
|
+
# If we are requesting the data
|
|
400
|
+
if ttl is None:
|
|
401
|
+
return dStore['__ttl']
|
|
402
|
+
|
|
403
|
+
# If we didn't get a number, or it's below 0
|
|
404
|
+
if isinstance(ttl, bool) or not isinstance(ttl, int) or ttl < 0:
|
|
405
|
+
raise ValueError('ttl must be an unsigned int')
|
|
406
|
+
|
|
407
|
+
# If the value hasn't changed
|
|
408
|
+
if ttl != dStore['__ttl']:
|
|
409
|
+
dStore['__ttl'] = ttl
|
|
410
|
+
self.save()
|
|
411
|
+
|
|
321
412
|
def update(self, other = (), /, **kwargs):
|
|
322
413
|
"""Updated
|
|
323
414
|
|
|
324
415
|
Merge key/value pairs into the session store. Works exactly like dict
|
|
325
|
-
update()
|
|
416
|
+
update().
|
|
326
417
|
|
|
327
418
|
Arguments:
|
|
328
419
|
other (dict, list, **kwargs): The additional keys and values to add
|
|
329
420
|
"""
|
|
330
|
-
|
|
421
|
+
|
|
422
|
+
# Run through each to guard against __ keys being set
|
|
423
|
+
for k, v in dict(other, **kwargs).items():
|
|
424
|
+
self[k] = v
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "memory_oc"
|
|
7
|
-
version = "1.0.
|
|
7
|
+
version = "1.0.4"
|
|
8
8
|
description = "A session library using redis"
|
|
9
9
|
authors = [
|
|
10
10
|
{name = "Chris Nasr - Ouroboros Coding Inc.", email = "chris@ouroboroscoding.com"}
|
|
@@ -16,7 +16,7 @@ license = "MIT"
|
|
|
16
16
|
readme = "README.md"
|
|
17
17
|
requires-python = ">=3.10"
|
|
18
18
|
dependencies = [
|
|
19
|
-
'jobject>=1.0.
|
|
19
|
+
'jobject>=1.0.5,<1.1',
|
|
20
20
|
'jsonb>=1.0.0,<1.1',
|
|
21
21
|
'json_fix>=1.0.0,<1.1',
|
|
22
22
|
'namedredis>=1.0.2,<1.1',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|