rediscache 0.3.1__py3-none-any.whl → 0.3.2__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.
rediscache/tools.py ADDED
@@ -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
@@ -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
@@ -0,0 +1,6 @@
1
+ rediscache/__init__.py,sha256=48eo_z_ohwxRYIeZxZTp373XxsCx6nEtC1EqE7FBfyk,14447
2
+ rediscache/tools.py,sha256=0I4l2f-SKZR6ec5y-GJOMhLdWimPjs6gGzHtPdPdYpg,995
3
+ rediscache-0.3.2.dist-info/LICENSE,sha256=glwtaJmUmkPhzLEfrVcc2JmLoCyTnHst84tadlSIK_8,1123
4
+ rediscache-0.3.2.dist-info/METADATA,sha256=1wOtgyEM_4R1xti4LSA5sczhI6N1uoySIIl4-zkBi3k,9975
5
+ rediscache-0.3.2.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
6
+ rediscache-0.3.2.dist-info/RECORD,,
@@ -1,5 +0,0 @@
1
- rediscache/__init__.py,sha256=48eo_z_ohwxRYIeZxZTp373XxsCx6nEtC1EqE7FBfyk,14447
2
- rediscache-0.3.1.dist-info/LICENSE,sha256=glwtaJmUmkPhzLEfrVcc2JmLoCyTnHst84tadlSIK_8,1123
3
- rediscache-0.3.1.dist-info/METADATA,sha256=44jsqPxY_QU5S1WgVzatYwe2sRHNVlj6FInv8R9RLxU,9012
4
- rediscache-0.3.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
5
- rediscache-0.3.1.dist-info/RECORD,,