memory-oc 1.0.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: memory_oc
3
- Version: 1.0.3
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.4
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,16 +32,18 @@ for changes from release to release.
32
32
  - [Module Install](#module-install)
33
33
  - [Configuration](#configuration)
34
34
  - [Methods](#methods)
35
- - [close](#memory-close)
36
- - [create](#memory-create)
37
- - [load](#memory-load)
35
+ - [close](#close)
36
+ - [create](#create)
37
+ - [from_dict](#from_dict)
38
+ - [load](#load)
38
39
  - [Class](#class)
39
- - [close](#instance-close)
40
- - [extend](#instance-extend)
41
- - [key](#instance-key)
42
- - [save](#instance-save)
43
- - [ttl](#instance-ttl)
44
- - [update](#instance-update)
40
+ - [close](#close-1)
41
+ - [extend](#extend)
42
+ - [key](#key)
43
+ - [save](#save)
44
+ - [to_dict](#to_dict)
45
+ - [ttl](#ttl)
46
+ - [update](#update)
45
47
 
46
48
  ## Module Install
47
49
 
@@ -80,12 +82,12 @@ The `redis` section lists Redis connections by name and details to connect. Then
80
82
  [ [top](#memory_oc) / [contents](#contents) ]
81
83
 
82
84
  ## Methods
83
- Memory comes with three methods, to [`close`](#close), [`create`](#create), and [`load`](#load) sessions.
85
+ Memory comes with four methods, to [`close`](#close), [`create`](#create),
86
+ [`from_dict`](#from_dict) and [`load`](#load) sessions.
84
87
 
85
88
  ### close
86
89
  Immediately closes and deletes a session from Redis.
87
90
 
88
- #### example
89
91
  ```python
90
92
  import memory
91
93
 
@@ -93,6 +95,8 @@ import memory
93
95
  memory.close(somekey)
94
96
  ```
95
97
 
98
+ [ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
99
+
96
100
  ### create
97
101
  Creates a new session. Takes 3 optional arguments
98
102
 
@@ -102,7 +106,6 @@ Creates a new session. Takes 3 optional arguments
102
106
  | `ttl` | unsigned integer | The time to live in seconds for the session. |
103
107
  | `data` | dict | The initial data to be stored in the session. |
104
108
 
105
- #### example
106
109
  ```python
107
110
  import memory
108
111
 
@@ -119,10 +122,29 @@ session.save()
119
122
  return session.key()
120
123
  ```
121
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
+
122
145
  ### load
123
146
  Loads a session from a passed key.
124
147
 
125
- #### example
126
148
  ```python
127
149
  import memory
128
150
 
@@ -136,16 +158,15 @@ session['name'] = 'Samuel'
136
158
  session.save()
137
159
  ```
138
160
 
139
- [ [top](#memory_oc) / [contents](#contents) ]
161
+ [ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
140
162
 
141
163
  ## Class
142
- The class is what is returned from the [create](#create) and
143
- [load](#load) methods.
164
+ The class is what is returned from the [`create`](#create),
165
+ [`from_dict`](#from_dict), and [`load`](#load) methods
144
166
 
145
- ### instance.close
167
+ ### close
146
168
  Closes the session in Redis, making it no longer available to anyone.
147
169
 
148
- #### example
149
170
  ```python
150
171
  import memory
151
172
 
@@ -156,10 +177,11 @@ session = memory.load(somekey)
156
177
  session.close()
157
178
  ```
158
179
 
159
- ### instance.extend
180
+ [ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
181
+
182
+ ### extend
160
183
  Extends the TTL of the session by the initial TTL given.
161
184
 
162
- #### exmaple
163
185
  ```python
164
186
  import memory
165
187
 
@@ -170,10 +192,11 @@ session = memory.load(somekey)
170
192
  session.extend()
171
193
  ```
172
194
 
173
- ### instance.key
195
+ [ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
196
+
197
+ ### key
174
198
  Returns the key of the session.
175
199
 
176
- #### example
177
200
  ```python
178
201
  import memory
179
202
 
@@ -184,10 +207,11 @@ session = memory.create()
184
207
  print(session.key())
185
208
  ```
186
209
 
187
- ### instance.save
210
+ [ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
211
+
212
+ ### save
188
213
  Stores changes in Redis after updates.
189
214
 
190
- #### example
191
215
  ```python
192
216
  import memory
193
217
 
@@ -201,10 +225,28 @@ session['name'] = 'Samuel'
201
225
  session.save()
202
226
  ```
203
227
 
204
- ### instance.ttl
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
205
248
  Getter / Setter for the TTL on the session.
206
249
 
207
- #### example
208
250
  Set
209
251
  ```python
210
252
  import memory
@@ -219,10 +261,11 @@ session.ttl()
219
261
  session.ttl(300)
220
262
  ```
221
263
 
222
- ### instance.update
264
+ [ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
265
+
266
+ ### update
223
267
  Works the same as dict update() so you can store multiple keys at once
224
268
 
225
- #### example
226
269
  ```python
227
270
  import memory
228
271
 
@@ -239,4 +282,4 @@ session.update({
239
282
  session.save()
240
283
  ```
241
284
 
242
- [ [top](#memory_oc) / [contents](#contents) ]
285
+ [ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
@@ -14,16 +14,18 @@ for changes from release to release.
14
14
  - [Module Install](#module-install)
15
15
  - [Configuration](#configuration)
16
16
  - [Methods](#methods)
17
- - [close](#memory-close)
18
- - [create](#memory-create)
19
- - [load](#memory-load)
17
+ - [close](#close)
18
+ - [create](#create)
19
+ - [from_dict](#from_dict)
20
+ - [load](#load)
20
21
  - [Class](#class)
21
- - [close](#instance-close)
22
- - [extend](#instance-extend)
23
- - [key](#instance-key)
24
- - [save](#instance-save)
25
- - [ttl](#instance-ttl)
26
- - [update](#instance-update)
22
+ - [close](#close-1)
23
+ - [extend](#extend)
24
+ - [key](#key)
25
+ - [save](#save)
26
+ - [to_dict](#to_dict)
27
+ - [ttl](#ttl)
28
+ - [update](#update)
27
29
 
28
30
  ## Module Install
29
31
 
@@ -62,12 +64,12 @@ The `redis` section lists Redis connections by name and details to connect. Then
62
64
  [ [top](#memory_oc) / [contents](#contents) ]
63
65
 
64
66
  ## Methods
65
- Memory comes with three methods, to [`close`](#close), [`create`](#create), and [`load`](#load) sessions.
67
+ Memory comes with four methods, to [`close`](#close), [`create`](#create),
68
+ [`from_dict`](#from_dict) and [`load`](#load) sessions.
66
69
 
67
70
  ### close
68
71
  Immediately closes and deletes a session from Redis.
69
72
 
70
- #### example
71
73
  ```python
72
74
  import memory
73
75
 
@@ -75,6 +77,8 @@ import memory
75
77
  memory.close(somekey)
76
78
  ```
77
79
 
80
+ [ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
81
+
78
82
  ### create
79
83
  Creates a new session. Takes 3 optional arguments
80
84
 
@@ -84,7 +88,6 @@ Creates a new session. Takes 3 optional arguments
84
88
  | `ttl` | unsigned integer | The time to live in seconds for the session. |
85
89
  | `data` | dict | The initial data to be stored in the session. |
86
90
 
87
- #### example
88
91
  ```python
89
92
  import memory
90
93
 
@@ -101,10 +104,29 @@ session.save()
101
104
  return session.key()
102
105
  ```
103
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
+
104
127
  ### load
105
128
  Loads a session from a passed key.
106
129
 
107
- #### example
108
130
  ```python
109
131
  import memory
110
132
 
@@ -118,16 +140,15 @@ session['name'] = 'Samuel'
118
140
  session.save()
119
141
  ```
120
142
 
121
- [ [top](#memory_oc) / [contents](#contents) ]
143
+ [ [top](#memory_oc) / [contents](#contents) / [methods](#methods) ]
122
144
 
123
145
  ## Class
124
- The class is what is returned from the [create](#create) and
125
- [load](#load) methods.
146
+ The class is what is returned from the [`create`](#create),
147
+ [`from_dict`](#from_dict), and [`load`](#load) methods
126
148
 
127
- ### instance.close
149
+ ### close
128
150
  Closes the session in Redis, making it no longer available to anyone.
129
151
 
130
- #### example
131
152
  ```python
132
153
  import memory
133
154
 
@@ -138,10 +159,11 @@ session = memory.load(somekey)
138
159
  session.close()
139
160
  ```
140
161
 
141
- ### instance.extend
162
+ [ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
163
+
164
+ ### extend
142
165
  Extends the TTL of the session by the initial TTL given.
143
166
 
144
- #### exmaple
145
167
  ```python
146
168
  import memory
147
169
 
@@ -152,10 +174,11 @@ session = memory.load(somekey)
152
174
  session.extend()
153
175
  ```
154
176
 
155
- ### instance.key
177
+ [ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
178
+
179
+ ### key
156
180
  Returns the key of the session.
157
181
 
158
- #### example
159
182
  ```python
160
183
  import memory
161
184
 
@@ -166,10 +189,11 @@ session = memory.create()
166
189
  print(session.key())
167
190
  ```
168
191
 
169
- ### instance.save
192
+ [ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
193
+
194
+ ### save
170
195
  Stores changes in Redis after updates.
171
196
 
172
- #### example
173
197
  ```python
174
198
  import memory
175
199
 
@@ -183,10 +207,28 @@ session['name'] = 'Samuel'
183
207
  session.save()
184
208
  ```
185
209
 
186
- ### instance.ttl
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
187
230
  Getter / Setter for the TTL on the session.
188
231
 
189
- #### example
190
232
  Set
191
233
  ```python
192
234
  import memory
@@ -201,10 +243,11 @@ session.ttl()
201
243
  session.ttl(300)
202
244
  ```
203
245
 
204
- ### instance.update
246
+ [ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
247
+
248
+ ### update
205
249
  Works the same as dict update() so you can store multiple keys at once
206
250
 
207
- #### example
208
251
  ```python
209
252
  import memory
210
253
 
@@ -221,4 +264,4 @@ session.update({
221
264
  session.save()
222
265
  ```
223
266
 
224
- [ [top](#memory_oc) / [contents](#contents) ]
267
+ [ [top](#memory_oc) / [contents](#contents) / [class](#class) ]
@@ -25,7 +25,7 @@ from tools import combine
25
25
  from copy import deepcopy
26
26
 
27
27
  # Pip imports
28
- import json_fix
28
+ import json_fix # needed for __json__, do not remove
29
29
 
30
30
  # Open redis connection
31
31
  _moRedis = nr(config.memory.redis('session'))
@@ -97,6 +97,19 @@ def create(key: str = None, ttl: int = 0, data = None) -> _Memory:
97
97
  # Create a new Memory using the passed key, or a new random string
98
98
  return _Memory(sKey, dData)
99
99
 
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
+
100
113
  def load(key: str) -> _Memory | None:
101
114
  """Load
102
115
 
@@ -134,7 +147,7 @@ class _Memory(object):
134
147
  def __init__(self, key: str, data: dict | None = None) -> _Memory:
135
148
  """Constructor
136
149
 
137
- Intialises the instance, which is just setting up the dict
150
+ Intialises the instance with a given key and optional data.
138
151
 
139
152
  Arguments:
140
153
  key (str): The key used to access or store the session
@@ -238,10 +251,7 @@ class _Memory(object):
238
251
  Returns:
239
252
  dict
240
253
  """
241
- return {
242
- '__key': object.__getattribute__(self, '__key'),
243
- '__store': deepcopy(object.__getattribute__(self, '__store'))
244
- }
254
+ return self.to_dict()
245
255
 
246
256
  def __len__(self):
247
257
  """__len__
@@ -355,6 +365,20 @@ class _Memory(object):
355
365
  dJSON
356
366
  )
357
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
+
358
382
  def ttl(self, ttl: int | None = None) -> int | None:
359
383
  """TTL
360
384
 
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "memory_oc"
7
- version = "1.0.3"
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.4,<1.1',
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