rediscache 0.3.1__tar.gz → 0.3.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: rediscache
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Redis caching of functions evolving over time
5
5
  Home-page: https://github.com/AmadeusITGroup/RedisCache
6
6
  License: MIT
@@ -158,6 +158,41 @@ def myfunc():
158
158
  print(myfunc.function())
159
159
  ```
160
160
 
161
+ ## The `decorate` decorator
162
+
163
+ In the `tools` submodule, the `decorate` decorator is a little helper to transform a serializer or deserializer function into a decorator.
164
+ The transformation function is expected to take a single argument of a certain type and transform it into another type.
165
+ For example, a typical serializer would transform a dictionary into a string.
166
+
167
+ ```python
168
+ from json import dumps
169
+
170
+ from rediscache.tools import decorate
171
+
172
+ @decorate(dumps)
173
+ def myfunc():
174
+ return {"toto": 42}
175
+
176
+ assert isinstance(myfunc(), str)
177
+ ```
178
+
179
+ You may use partial functions if your transformation function requires extra parameters.
180
+
181
+ ```python
182
+ from datetime import date
183
+ from functools import partial
184
+ from json import dumps
185
+
186
+ from rediscache.tools import decorate
187
+
188
+ @decorate(partial(dumps, skipkeys=True))
189
+ def func_with_id():
190
+ """My func_with_id"""
191
+ return {"name": "Toto", "age": 25, date.today(): "today"}
192
+
193
+ assert func_with_id() == '{"name": "Toto", "age": 25}'
194
+ ```
195
+
161
196
  ## Development
162
197
 
163
198
  ### Poetry
@@ -130,6 +130,41 @@ def myfunc():
130
130
  print(myfunc.function())
131
131
  ```
132
132
 
133
+ ## The `decorate` decorator
134
+
135
+ In the `tools` submodule, the `decorate` decorator is a little helper to transform a serializer or deserializer function into a decorator.
136
+ The transformation function is expected to take a single argument of a certain type and transform it into another type.
137
+ For example, a typical serializer would transform a dictionary into a string.
138
+
139
+ ```python
140
+ from json import dumps
141
+
142
+ from rediscache.tools import decorate
143
+
144
+ @decorate(dumps)
145
+ def myfunc():
146
+ return {"toto": 42}
147
+
148
+ assert isinstance(myfunc(), str)
149
+ ```
150
+
151
+ You may use partial functions if your transformation function requires extra parameters.
152
+
153
+ ```python
154
+ from datetime import date
155
+ from functools import partial
156
+ from json import dumps
157
+
158
+ from rediscache.tools import decorate
159
+
160
+ @decorate(partial(dumps, skipkeys=True))
161
+ def func_with_id():
162
+ """My func_with_id"""
163
+ return {"name": "Toto", "age": 25, date.today(): "today"}
164
+
165
+ assert func_with_id() == '{"name": "Toto", "age": 25}'
166
+ ```
167
+
133
168
  ## Development
134
169
 
135
170
  ### Poetry
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "rediscache"
3
- version = "0.3.1"
3
+ version = "0.3.2"
4
4
  description = "Redis caching of functions evolving over time"
5
5
  authors = ["Pierre Cart-Grandjean <pcart-grandjean@amadeus.com>"]
6
6
  license = "MIT"
@@ -0,0 +1,30 @@
1
+ """
2
+ Extra tools provided to help with the cache main purpose.
3
+ """
4
+
5
+ from functools import wraps
6
+ from typing import Callable, ParamSpec, TypeVar
7
+
8
+ P = ParamSpec("P")
9
+ InT = TypeVar("InT")
10
+ OutT = TypeVar("OutT")
11
+
12
+
13
+ def decorate(transform: Callable[[InT], OutT]) -> Callable[[Callable[P, InT]], Callable[P, OutT]]:
14
+ """
15
+ This decorator helps to transform the output of a function from type InT to type OuT providing the
16
+ appropriate function.
17
+ It is especially meant to be used to serialize the output of a function to be cached.
18
+ It can also be used to deserialize the cached value, but this should be used with great caution
19
+ since it could be worse than not caching the function at all.
20
+ """
21
+
22
+ def decorator(function: Callable[P, InT]) -> Callable[P, OutT]:
23
+ @wraps(function)
24
+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> OutT:
25
+ value = function(*args, **kwargs)
26
+ return transform(value)
27
+
28
+ return wrapper
29
+
30
+ return decorator
File without changes