memory-oc 1.0.1__tar.gz → 1.0.2__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.
@@ -0,0 +1,223 @@
1
+ Metadata-Version: 2.4
2
+ Name: memory_oc
3
+ Version: 1.0.2
4
+ Summary: A session library using redis
5
+ Project-URL: Source, https://github.com/ouroboroscoding/memory-python
6
+ Project-URL: Tracker, https://github.com/ouroboroscoding/memory-python/issues
7
+ Author-email: "Chris Nasr - Ouroboros Coding Inc." <chris@ouroboroscoding.com>
8
+ Maintainer-email: "Chris Nasr - Ouroboros Coding Inc." <chris@ouroboroscoding.com>
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Requires-Python: >=3.10
12
+ Requires-Dist: jobject<1.1,>=1.0.4
13
+ Requires-Dist: json-fix<1.1,>=1.0.0
14
+ Requires-Dist: jsonb<1.1,>=1.0.0
15
+ Requires-Dist: namedredis<1.1,>=1.0.2
16
+ Requires-Dist: strings-oc<1.1,>=1.0.7
17
+ Description-Content-Type: text/markdown
18
+
19
+ # memory_oc
20
+ [![pypi version](https://img.shields.io/pypi/v/memory_oc.svg)](https://pypi.org/project/memory_oc) ![Custom License](https://img.shields.io/pypi/l/memory_oc.svg)
21
+
22
+ Provides methods for creating and managing sessions in [body_oc](https://pypi.org/project/body-oc/)
23
+ projects.
24
+
25
+ Please see [LICENSE](https://github.com/ouroboroscoding/memory-python/blob/main/LICENSE)
26
+ for further information.
27
+
28
+ See [Releases](https://github.com/ouroboroscoding/memory-python/blob/main/releases.md)
29
+ for changes from release to release.
30
+
31
+ ## Contents
32
+ - [Module Install](#module-install)
33
+ - [Configuration](#configuration)
34
+ - [Methods](#methods)
35
+ - [close](#memory-close)
36
+ - [create](#memory-create)
37
+ - [load](#memory-load)
38
+ - [Class](#class)
39
+ - [close](#instance-close)
40
+ - [extend](#instance-extend)
41
+ - [key](#instance-key)
42
+ - [save](#instance-save)
43
+ - [update](#instance-update)
44
+
45
+ ## Module Install
46
+
47
+ ### Requires
48
+ memory_oc requires python 3.10 or higher
49
+
50
+ ### Install via pip
51
+ ```bash
52
+ pip install memory_oc
53
+ ```
54
+
55
+ [ [top](#memory_oc) / [contents](#contents) ]
56
+
57
+ ## Configuration
58
+ Memory requires the use of a config_oc config.json file. See [config_oc](https://pypi.org/project/config-oc/)
59
+ for more info.
60
+
61
+ ```json
62
+ {
63
+ "memory": {
64
+ "redis": "session"
65
+ },
66
+
67
+ "redis": {
68
+ "session": {
69
+ "host": "redis.mydomain.com",
70
+ "db": 1
71
+ }
72
+ }
73
+ }
74
+ ```
75
+
76
+ The `redis` section lists Redis connections by name and details to connect. Then
77
+ `memory.redis` specifies which connection to use to create the sessions on.
78
+
79
+ [ [top](#memory_oc) / [contents](#contents) ]
80
+
81
+ ## Methods
82
+ Memory comes with three methods, to [`close`](#close), [`create`](#create), and [`load`](#load) sessions.
83
+
84
+ ### close
85
+ Immediately closes and deletes a session from Redis.
86
+
87
+ #### example
88
+ ```python
89
+ import memory
90
+
91
+ # Closes the session by key
92
+ memory.close(somekey)
93
+ ```
94
+
95
+ ### create
96
+ Creates a new session. Takes 3 optional arguments
97
+
98
+ | Argument | Type | Description |
99
+ | -------- | ---- | ----------- |
100
+ | `key` | str | The key to use to create the session in Redis. |
101
+ | `ttl` | unsigned integer | The time to live in seconds for the session. |
102
+ | `data` | dict | The initial data to be stored in the session. |
103
+
104
+ #### example
105
+ ```python
106
+ import memory
107
+
108
+ # Creates a 10 minutes session
109
+ session = memory.create(ttl = 600, data = {
110
+ 'email': 'sam@somedomain.com',
111
+ 'name': 'Sam'
112
+ })
113
+
114
+ # Stores it in Redis
115
+ session.save()
116
+
117
+ # Returns the generated session key
118
+ return session.key()
119
+ ```
120
+
121
+ ### load
122
+ Loads a session from a passed key.
123
+
124
+ #### example
125
+ ```python
126
+ import memory
127
+
128
+ # Loads the session data from Redis
129
+ session = memory.load(somekey)
130
+
131
+ # Change data
132
+ session['name'] = 'Samuel'
133
+
134
+ # Store it in Redis
135
+ session.save()
136
+ ```
137
+
138
+ [ [top](#memory_oc) / [contents](#contents) ]
139
+
140
+ ## Class
141
+ The class is what is returned from the [create](#create) and
142
+ [load](#load) methods.
143
+
144
+ ### instance.close
145
+ Closes the session in Redis, making it no longer available to anyone.
146
+
147
+ #### example
148
+ ```python
149
+ import memory
150
+
151
+ # Load the session
152
+ session = memory.load(somekey)
153
+
154
+ # Close the session
155
+ session.close()
156
+ ```
157
+
158
+ ### instance.extend
159
+ Extends the TTL of the session by the initial TTL given.
160
+
161
+ #### exmaple
162
+ ```python
163
+ import memory
164
+
165
+ # Load the session
166
+ session = memory.load(somekey)
167
+
168
+ # Extend the session
169
+ session.extend()
170
+ ```
171
+
172
+ ### instance.key
173
+ Returns the key of the session.
174
+
175
+ #### example
176
+ ```python
177
+ import memory
178
+
179
+ # Create the session
180
+ session = memory.create()
181
+
182
+ # Print the key
183
+ print(session.key())
184
+ ```
185
+
186
+ ### instance.save
187
+ Stores changes in Redis after updates.
188
+
189
+ #### example
190
+ ```python
191
+ import memory
192
+
193
+ # Load a session
194
+ session = memory.load(somekey)
195
+
196
+ # Make changes
197
+ session['name'] = 'Samuel'
198
+
199
+ # Save changes
200
+ session.save()
201
+ ```
202
+
203
+ ### instance.update
204
+ Works the same as dict update() so you can store multiple keys at once
205
+
206
+ #### example
207
+ ```python
208
+ import memory
209
+
210
+ # Load session
211
+ session = memory.load(somekey)
212
+
213
+ # Update multiple fields
214
+ session.update({
215
+ 'name': 'Samuel',
216
+ 'phone': '(555) 123-4567'
217
+ })
218
+
219
+ # Save changes
220
+ session.save()
221
+ ```
222
+
223
+ [ [top](#memory_oc) / [contents](#contents) ]
@@ -0,0 +1,205 @@
1
+ # memory_oc
2
+ [![pypi version](https://img.shields.io/pypi/v/memory_oc.svg)](https://pypi.org/project/memory_oc) ![Custom License](https://img.shields.io/pypi/l/memory_oc.svg)
3
+
4
+ Provides methods for creating and managing sessions in [body_oc](https://pypi.org/project/body-oc/)
5
+ projects.
6
+
7
+ Please see [LICENSE](https://github.com/ouroboroscoding/memory-python/blob/main/LICENSE)
8
+ for further information.
9
+
10
+ See [Releases](https://github.com/ouroboroscoding/memory-python/blob/main/releases.md)
11
+ for changes from release to release.
12
+
13
+ ## Contents
14
+ - [Module Install](#module-install)
15
+ - [Configuration](#configuration)
16
+ - [Methods](#methods)
17
+ - [close](#memory-close)
18
+ - [create](#memory-create)
19
+ - [load](#memory-load)
20
+ - [Class](#class)
21
+ - [close](#instance-close)
22
+ - [extend](#instance-extend)
23
+ - [key](#instance-key)
24
+ - [save](#instance-save)
25
+ - [update](#instance-update)
26
+
27
+ ## Module Install
28
+
29
+ ### Requires
30
+ memory_oc requires python 3.10 or higher
31
+
32
+ ### Install via pip
33
+ ```bash
34
+ pip install memory_oc
35
+ ```
36
+
37
+ [ [top](#memory_oc) / [contents](#contents) ]
38
+
39
+ ## Configuration
40
+ Memory requires the use of a config_oc config.json file. See [config_oc](https://pypi.org/project/config-oc/)
41
+ for more info.
42
+
43
+ ```json
44
+ {
45
+ "memory": {
46
+ "redis": "session"
47
+ },
48
+
49
+ "redis": {
50
+ "session": {
51
+ "host": "redis.mydomain.com",
52
+ "db": 1
53
+ }
54
+ }
55
+ }
56
+ ```
57
+
58
+ The `redis` section lists Redis connections by name and details to connect. Then
59
+ `memory.redis` specifies which connection to use to create the sessions on.
60
+
61
+ [ [top](#memory_oc) / [contents](#contents) ]
62
+
63
+ ## Methods
64
+ Memory comes with three methods, to [`close`](#close), [`create`](#create), and [`load`](#load) sessions.
65
+
66
+ ### close
67
+ Immediately closes and deletes a session from Redis.
68
+
69
+ #### example
70
+ ```python
71
+ import memory
72
+
73
+ # Closes the session by key
74
+ memory.close(somekey)
75
+ ```
76
+
77
+ ### create
78
+ Creates a new session. Takes 3 optional arguments
79
+
80
+ | Argument | Type | Description |
81
+ | -------- | ---- | ----------- |
82
+ | `key` | str | The key to use to create the session in Redis. |
83
+ | `ttl` | unsigned integer | The time to live in seconds for the session. |
84
+ | `data` | dict | The initial data to be stored in the session. |
85
+
86
+ #### example
87
+ ```python
88
+ import memory
89
+
90
+ # Creates a 10 minutes session
91
+ session = memory.create(ttl = 600, data = {
92
+ 'email': 'sam@somedomain.com',
93
+ 'name': 'Sam'
94
+ })
95
+
96
+ # Stores it in Redis
97
+ session.save()
98
+
99
+ # Returns the generated session key
100
+ return session.key()
101
+ ```
102
+
103
+ ### load
104
+ Loads a session from a passed key.
105
+
106
+ #### example
107
+ ```python
108
+ import memory
109
+
110
+ # Loads the session data from Redis
111
+ session = memory.load(somekey)
112
+
113
+ # Change data
114
+ session['name'] = 'Samuel'
115
+
116
+ # Store it in Redis
117
+ session.save()
118
+ ```
119
+
120
+ [ [top](#memory_oc) / [contents](#contents) ]
121
+
122
+ ## Class
123
+ The class is what is returned from the [create](#create) and
124
+ [load](#load) methods.
125
+
126
+ ### instance.close
127
+ Closes the session in Redis, making it no longer available to anyone.
128
+
129
+ #### example
130
+ ```python
131
+ import memory
132
+
133
+ # Load the session
134
+ session = memory.load(somekey)
135
+
136
+ # Close the session
137
+ session.close()
138
+ ```
139
+
140
+ ### instance.extend
141
+ Extends the TTL of the session by the initial TTL given.
142
+
143
+ #### exmaple
144
+ ```python
145
+ import memory
146
+
147
+ # Load the session
148
+ session = memory.load(somekey)
149
+
150
+ # Extend the session
151
+ session.extend()
152
+ ```
153
+
154
+ ### instance.key
155
+ Returns the key of the session.
156
+
157
+ #### example
158
+ ```python
159
+ import memory
160
+
161
+ # Create the session
162
+ session = memory.create()
163
+
164
+ # Print the key
165
+ print(session.key())
166
+ ```
167
+
168
+ ### instance.save
169
+ Stores changes in Redis after updates.
170
+
171
+ #### example
172
+ ```python
173
+ import memory
174
+
175
+ # Load a session
176
+ session = memory.load(somekey)
177
+
178
+ # Make changes
179
+ session['name'] = 'Samuel'
180
+
181
+ # Save changes
182
+ session.save()
183
+ ```
184
+
185
+ ### instance.update
186
+ Works the same as dict update() so you can store multiple keys at once
187
+
188
+ #### example
189
+ ```python
190
+ import memory
191
+
192
+ # Load session
193
+ session = memory.load(somekey)
194
+
195
+ # Update multiple fields
196
+ session.update({
197
+ 'name': 'Samuel',
198
+ 'phone': '(555) 123-4567'
199
+ })
200
+
201
+ # Save changes
202
+ session.save()
203
+ ```
204
+
205
+ [ [top](#memory_oc) / [contents](#contents) ]
@@ -19,6 +19,7 @@ import jobject
19
19
  import jsonb
20
20
  from nredis import nr
21
21
  from strings import random
22
+ from tools import combine
22
23
 
23
24
  # Pip imports
24
25
  import json_fix
@@ -26,7 +27,14 @@ import json_fix
26
27
  # Open redis connection
27
28
  _moRedis = nr(config.memory.redis('session'))
28
29
 
29
- def create(key: str = None, ttl: int = 0) -> _Memory:
30
+ def close(key: str):
31
+ """Close
32
+
33
+ Deletes the session from the cache
34
+ """
35
+ _moRedis.delete(key)
36
+
37
+ def create(key: str = None, ttl: int = 0, data = None) -> _Memory:
30
38
  """Create
31
39
 
32
40
  Returns a brand new session using the key given, else a UUID is generated
@@ -39,8 +47,12 @@ def create(key: str = None, ttl: int = 0) -> _Memory:
39
47
  _Memory
40
48
  """
41
49
 
42
- # Init the data with the expires time
43
- dData = { '__ttl': ttl }
50
+ # Generate the data to store by adding the TTL
51
+ dData = (
52
+ data is None
53
+ and { '__ttl': ttl }
54
+ or combine(data, { '__ttl': ttl })
55
+ )
44
56
 
45
57
  # If we were passed a key
46
58
  if key:
@@ -55,22 +67,23 @@ def create(key: str = None, ttl: int = 0) -> _Memory:
55
67
  # Else, loop till we get a key that works, which theoretically is always the
56
68
  # first time, but on the off chance something breaks, let's have it fully
57
69
  # break instead of running forever eating up resources.
58
- i = 0
59
- while True:
70
+ else:
71
+ i = 0
72
+ while True:
60
73
 
61
- # Generate a random key
62
- sKey = 's:%s' % random(32, [ 'aZ', '10', '!*' ])
74
+ # Generate a random key
75
+ sKey = 's:%s' % random(32, [ 'aZ', '10', '!*' ])
63
76
 
64
- # If it doesn't exist, break out of the loop
65
- if not _moRedis.exists(sKey):
66
- break
77
+ # If it doesn't exist, break out of the loop
78
+ if not _moRedis.exists(sKey):
79
+ break
67
80
 
68
- # Increment the count
69
- i += 1
70
- if i > 10:
71
- raise RuntimeError(
72
- 'memory_oc', 'potential infinite loop in create()'
73
- )
81
+ # Increment the count
82
+ i += 1
83
+ if i > 10:
84
+ raise RuntimeError(
85
+ 'memory_oc', 'potential infinite loop in create()'
86
+ )
74
87
 
75
88
  # Create a new Memory using the passed key, or a new UUID
76
89
  return _Memory(sKey, dData)
@@ -109,7 +122,7 @@ class _Memory(object):
109
122
  object
110
123
  """
111
124
 
112
- def __init__(self, key: str, data: dict = {}):
125
+ def __init__(self, key: str, data: dict = None):
113
126
  """Constructor
114
127
 
115
128
  Intialises the instance, which is just setting up the dict
@@ -122,6 +135,10 @@ class _Memory(object):
122
135
  _Memory
123
136
  """
124
137
 
138
+ # If no data passed, init an empty dict
139
+ if data is None:
140
+ data = { }
141
+
125
142
  # Store the key and data
126
143
  object.__setattr__(self, '__key', key)
127
144
  object.__setattr__(self, '__store', jobject(data))
@@ -146,9 +163,6 @@ class _Memory(object):
146
163
 
147
164
  Arguments:
148
165
  k (str): The key to remove
149
-
150
- Returns:
151
- None
152
166
  """
153
167
  del object.__getattribute__(self, '__store')[k]
154
168
 
@@ -253,9 +267,6 @@ class _Memory(object):
253
267
  """Close
254
268
 
255
269
  Deletes the session from the cache
256
-
257
- Returns:
258
- None
259
270
  """
260
271
  _moRedis.delete(object.__getattribute__(self, '__key'))
261
272
 
@@ -264,9 +275,6 @@ class _Memory(object):
264
275
 
265
276
  Keep the session alive by extending it's expire time by the internally \
266
277
  set expire value, or else by the global one set for the module
267
-
268
- Returns:
269
- None
270
278
  """
271
279
 
272
280
  # If the expire time is 0, do nothing
@@ -293,9 +301,6 @@ class _Memory(object):
293
301
  """Save
294
302
 
295
303
  Saves the current session data in the cache
296
-
297
- Returns:
298
- None
299
304
  """
300
305
 
301
306
  # If we have no expire time, set forever
@@ -311,4 +316,15 @@ class _Memory(object):
311
316
  object.__getattribute__(self, '__key'),
312
317
  object.__getattribute__(self, '__store')['__ttl'],
313
318
  jsonb.encode(object.__getattribute__(self, '__store'))
314
- )
319
+ )
320
+
321
+ def update(self, other = (), /, **kwargs):
322
+ """Updated
323
+
324
+ Merge key/value pairs into the session store. Works exactly like dict
325
+ update()
326
+
327
+ Arguments:
328
+ other (dict, list, **kwargs): The additional keys and values to add
329
+ """
330
+ object.__getattribute__(self, '__store').update(other, **kwargs)
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "memory_oc"
7
- version = "1.0.1"
7
+ version = "1.0.2"
8
8
  description = "A session library using redis"
9
9
  authors = [
10
10
  {name = "Chris Nasr - Ouroboros Coding Inc.", email = "chris@ouroboroscoding.com"}
memory_oc-1.0.1/PKG-INFO DELETED
@@ -1,20 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: memory_oc
3
- Version: 1.0.1
4
- Summary: A session library using redis
5
- Project-URL: Source, https://github.com/ouroboroscoding/memory-python
6
- Project-URL: Tracker, https://github.com/ouroboroscoding/memory-python/issues
7
- Author-email: "Chris Nasr - Ouroboros Coding Inc." <chris@ouroboroscoding.com>
8
- Maintainer-email: "Chris Nasr - Ouroboros Coding Inc." <chris@ouroboroscoding.com>
9
- License-Expression: MIT
10
- License-File: LICENSE
11
- Requires-Python: >=3.10
12
- Requires-Dist: jobject<1.1,>=1.0.4
13
- Requires-Dist: json-fix<1.1,>=1.0.0
14
- Requires-Dist: jsonb<1.1,>=1.0.0
15
- Requires-Dist: namedredis<1.1,>=1.0.2
16
- Requires-Dist: strings-oc<1.1,>=1.0.7
17
- Description-Content-Type: text/markdown
18
-
19
- # memory-python
20
- A python session library using redis
memory_oc-1.0.1/README.md DELETED
@@ -1,2 +0,0 @@
1
- # memory-python
2
- A python session library using redis
File without changes
File without changes
File without changes