cachier 2.1.1__py2.py3-none-any.whl → 2.2.1__py2.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.
cachier/_version.py CHANGED
@@ -11,8 +11,8 @@ version_json = '''
11
11
  {
12
12
  "dirty": false,
13
13
  "error": null,
14
- "full-revisionid": "7bd2c7797bad62a6fc4ba4ddc04289426ea1115c",
15
- "version": "2.1.1"
14
+ "full-revisionid": "5776ee8d63c8a5719ccd3737f0f504bc1a7eb679",
15
+ "version": "2.2.1"
16
16
  }
17
17
  ''' # END VERSION_JSON
18
18
 
cachier/core.py CHANGED
@@ -8,24 +8,25 @@
8
8
  # Copyright (c) 2016, Shay Palachy <shaypal5@gmail.com>
9
9
 
10
10
  # python 2 compatibility
11
- from __future__ import absolute_import
12
- from __future__ import division
13
- from __future__ import print_function
14
-
15
- import os
16
- from functools import wraps
17
- from warnings import warn
11
+ from __future__ import absolute_import, division, print_function
18
12
 
19
13
  import datetime
20
14
  import functools
21
15
  import hashlib
16
+ import os
22
17
  import pickle
23
18
  from concurrent.futures import ThreadPoolExecutor
19
+ from functools import wraps
20
+ from typing import TYPE_CHECKING, Callable, Literal, Optional, TypedDict, Union
21
+ from warnings import warn
24
22
 
25
- from .base_core import RecalculationNeeded
26
- from .pickle_core import _PickleCore
27
- from .mongo_core import _MongoCore
23
+ from .base_core import RecalculationNeeded, _BaseCore
28
24
  from .memory_core import _MemoryCore
25
+ from .mongo_core import _MongoCore
26
+ from .pickle_core import _PickleCore
27
+
28
+ if TYPE_CHECKING:
29
+ import pymongo.collection
29
30
 
30
31
 
31
32
  MAX_WORKERS_ENVAR_NAME = 'CACHIER_MAX_WORKERS'
@@ -92,7 +93,26 @@ class MissingMongetter(ValueError):
92
93
  """Thrown when the mongetter keyword argument is missing."""
93
94
 
94
95
 
95
- _default_params = {
96
+ HashFunc = Callable[..., str]
97
+ Mongetter = Callable[[], "pymongo.collection.Collection"]
98
+ Backend = Literal["pickle", "mongo", "memory"]
99
+
100
+
101
+ class Params(TypedDict):
102
+ caching_enabled: bool
103
+ hash_func: HashFunc
104
+ backend: Backend
105
+ mongetter: Optional[Mongetter]
106
+ stale_after: datetime.timedelta
107
+ next_time: bool
108
+ cache_dir: Union[str, os.PathLike]
109
+ pickle_reload: bool
110
+ separate_files: bool
111
+ wait_for_calc_timeout: int
112
+ allow_none: bool
113
+
114
+
115
+ _default_params: Params = {
96
116
  'caching_enabled': True,
97
117
  'hash_func': _default_hash_func,
98
118
  'backend': 'pickle',
@@ -103,20 +123,22 @@ _default_params = {
103
123
  'pickle_reload': True,
104
124
  'separate_files': False,
105
125
  'wait_for_calc_timeout': 0,
126
+ 'allow_none': False,
106
127
  }
107
128
 
108
129
 
109
130
  def cachier(
110
- hash_func=None,
111
- hash_params=None,
112
- backend=None,
113
- mongetter=None,
114
- stale_after=None,
115
- next_time=None,
116
- cache_dir=None,
117
- pickle_reload=None,
118
- separate_files=None,
119
- wait_for_calc_timeout=None,
131
+ hash_func: Optional[HashFunc] = None,
132
+ hash_params: Optional[HashFunc] = None,
133
+ backend: Optional[Backend] = None,
134
+ mongetter: Optional[Mongetter] = None,
135
+ stale_after: Optional[datetime.timedelta] = None,
136
+ next_time: Optional[bool] = None,
137
+ cache_dir: Optional[Union[str, os.PathLike]] = None,
138
+ pickle_reload: Optional[bool] = None,
139
+ separate_files: Optional[bool] = None,
140
+ wait_for_calc_timeout: Optional[int] = None,
141
+ allow_none: Optional[bool] = None,
120
142
  ):
121
143
  """A persistent, stale-free memoization decorator.
122
144
 
@@ -170,6 +192,9 @@ def cachier(
170
192
  True, any process trying to read the same entry will wait a maximum of
171
193
  seconds specified in this parameter. 0 means wait forever.
172
194
  Once the timeout expires the calculation will be triggered.
195
+ allow_none: bool, optional
196
+ Allows storing None values in the cache. If False, functions returning
197
+ None will not be cached and are recalculated every call.
173
198
  """
174
199
  # Check for deprecated parameters
175
200
  if hash_params is not None:
@@ -184,6 +209,7 @@ def cachier(
184
209
  backend = 'mongo'
185
210
  if backend is None:
186
211
  backend = _default_params['backend']
212
+ core: _BaseCore
187
213
  if backend == 'pickle':
188
214
  core = _PickleCore( # pylint: disable=R0204
189
215
  hash_func=hash_func,
@@ -208,11 +234,6 @@ def cachier(
208
234
  hash_func=hash_func,
209
235
  default_params=_default_params,
210
236
  )
211
- elif backend == 'redis':
212
- raise NotImplementedError(
213
- 'A Redis backend has not yet been implemented. '
214
- 'Please see https://github.com/python-cachier/cachier/issues/4'
215
- )
216
237
  else:
217
238
  raise ValueError('specified an invalid core: {}'.format(backend))
218
239
 
@@ -221,6 +242,12 @@ def cachier(
221
242
 
222
243
  @wraps(func)
223
244
  def func_wrapper(*args, **kwds): # pylint: disable=C0111,R0911
245
+ nonlocal allow_none
246
+ _allow_none = (
247
+ allow_none
248
+ if allow_none is not None else
249
+ _default_params['allow_none']
250
+ )
224
251
  # print('Inside general wrapper for {}.'.format(func.__name__))
225
252
  ignore_cache = kwds.pop('ignore_cache', False)
226
253
  overwrite_cache = kwds.pop('overwrite_cache', False)
@@ -238,7 +265,7 @@ def cachier(
238
265
  return _calc_entry(core, key, func, args, kwds)
239
266
  if entry is not None: # pylint: disable=R0101
240
267
  _print('Entry found.')
241
- if entry.get('value', None) is not None:
268
+ if (_allow_none or entry.get('value', None) is not None):
242
269
  _print('Cached result found.')
243
270
  local_stale_after = stale_after if stale_after is not None else _default_params['stale_after'] # noqa: E501
244
271
  local_next_time = next_time if next_time is not None else _default_params['next_time'] # noqa: E501
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cachier
3
- Version: 2.1.1
3
+ Version: 2.2.1
4
4
  Summary: Persistent, stale-free, local and cross-machine caching for Python functions.
5
5
  Home-page: https://github.com/python-cachier/cachier
6
6
  Author: Shay Palachy
@@ -14,10 +14,10 @@ Classifier: Development Status :: 4 - Beta
14
14
  Classifier: License :: OSI Approved :: MIT License
15
15
  Classifier: Programming Language :: Python
16
16
  Classifier: Programming Language :: Python :: 3
17
- Classifier: Programming Language :: Python :: 3.7
18
17
  Classifier: Programming Language :: Python :: 3.8
19
18
  Classifier: Programming Language :: Python :: 3.9
20
19
  Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
21
  Classifier: Topic :: Software Development :: Libraries
22
22
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
23
  Classifier: Topic :: Utilities
@@ -36,6 +36,9 @@ Requires-Dist: bandit ; extra == 'test'
36
36
  Requires-Dist: flake8 ; extra == 'test'
37
37
  Requires-Dist: pylint ; extra == 'test'
38
38
  Requires-Dist: safety ; extra == 'test'
39
+ Requires-Dist: mypy ; extra == 'test'
40
+ Requires-Dist: types-setuptools ; extra == 'test'
41
+ Requires-Dist: pandas-stubs ; extra == 'test'
39
42
  Requires-Dist: pymongo ; extra == 'test'
40
43
  Requires-Dist: dnspython ; extra == 'test'
41
44
  Requires-Dist: pymongo-inmemory ; extra == 'test'
@@ -89,7 +92,7 @@ Features
89
92
  ========
90
93
 
91
94
  * Pure Python.
92
- * Compatible with Python 3.5+ (and Python 2.7 up until version 1.2.8).
95
+ * Compatible with Python 3.8+ (Python 2.7 was discontinued in version 1.2.8).
93
96
  * Supported and `tested on Linux, OS X and Windows <https://travis-ci.org/shaypal5/cachier>`_.
94
97
  * A simple interface.
95
98
  * Defining "shelf life" for cached values.
@@ -184,6 +187,7 @@ The following parameters will only be applied to decorators defined after `set_d
184
187
 
185
188
  These parameters can be changed at any time and they will apply to all decorators:
186
189
 
190
+ * `allow_none`
187
191
  * `caching_enabled`
188
192
  * `stale_after`
189
193
  * `next_time`
@@ -301,6 +305,10 @@ Verbose Cache Call
301
305
 
302
306
  You can have ``cachier`` print out a detailed explanation of the logic of a specific call by passing ``verbose_cache=True`` to the function call. This can be useful if you are not sure why a certain function result is, or is not, returned.
303
307
 
308
+ Cache `None` Values
309
+ ~~~~~~~~~~~~~~~~~~~
310
+
311
+ By default, ``cachier`` does not cache ``None`` values. You can override this behaviour by passing ``allow_none=True`` to the function call.
304
312
 
305
313
 
306
314
  Cachier Cores
@@ -509,5 +517,3 @@ Notable bugfixers:
509
517
  .. links:
510
518
  .. _pymongo: https://api.mongodb.com/python/current/
511
519
  .. _watchdog: https://github.com/gorakhargosh/watchdog
512
-
513
-
@@ -1,15 +1,15 @@
1
1
  cachier/__init__.py,sha256=CQhNw1mPDRcR8x5E-rJX0shNQ_j-1MHkY4VdHI0PWSo,232
2
- cachier/_version.py,sha256=EMoBzs1Tjbo8HerKxp1Oc4S68qE-T_A5YNaLdjma_wU,471
2
+ cachier/_version.py,sha256=53-w9yvhnJKzEivGwM-rvu3FtmFtPgfHpCpO_S55v2c,471
3
3
  cachier/base_core.py,sha256=D0czuggU8oKUyTKx3R7dwhEDeOSlvnkojYfHPMpN_dI,3062
4
- cachier/core.py,sha256=83cm3uRcVppHY-yfkbISqxUU1whHIvTcEjKM5o6Euso,13492
4
+ cachier/core.py,sha256=m8F5GdgukhLDzmsZb-13cC-M_eadh_GkQ_doSHi8038,14548
5
5
  cachier/memory_core.py,sha256=u19-9IIYL8Y_9bDZ9cC3k9bE_-QezzLA1qrd_abpIes,2846
6
6
  cachier/mongo_core.py,sha256=eur37BnMg8aO_-UA26jDEj2VNSZl352B0U2IWIf72Hc,5022
7
7
  cachier/pickle_core.py,sha256=pteQZ1IYaEL7tKagaxt3eeueGXy58weAi4hmMfmbM5A,10068
8
8
  cachier/scripts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
9
  cachier/scripts/cli.py,sha256=tMwq1IaD_lxhcc8WMgWh4P0gpi5l6-twCS2YeAKAM98,429
10
- cachier-2.1.1.dist-info/LICENSE,sha256=-2WrMJkIa0gVP6YQHXXDT7ws-S3M2NEVEF4XF3K8qrY,1069
11
- cachier-2.1.1.dist-info/METADATA,sha256=8RFpbcb3vVc_V6uUbC1xjlmtWubzBdEXW5iKA-ZG_4M,17955
12
- cachier-2.1.1.dist-info/WHEEL,sha256=z9j0xAa_JmUKMpmz72K0ZGALSM_n-wQVmGbleXx2VHg,110
13
- cachier-2.1.1.dist-info/entry_points.txt,sha256=HZPL3TEoT-aieCOvQvsLEDBAuPBSbDNrIjMiDA8h6zE,71
14
- cachier-2.1.1.dist-info/top_level.txt,sha256=_rW_HiJumDCch67YT-WAgzcyvKg5RiYDMZq9d-0ZpaE,8
15
- cachier-2.1.1.dist-info/RECORD,,
10
+ cachier-2.2.1.dist-info/LICENSE,sha256=-2WrMJkIa0gVP6YQHXXDT7ws-S3M2NEVEF4XF3K8qrY,1069
11
+ cachier-2.2.1.dist-info/METADATA,sha256=aTiRMVMZ1L5YGl_6Lqwj1tyiDhl-v2TiPRs-YTQenJk,18293
12
+ cachier-2.2.1.dist-info/WHEEL,sha256=z9j0xAa_JmUKMpmz72K0ZGALSM_n-wQVmGbleXx2VHg,110
13
+ cachier-2.2.1.dist-info/entry_points.txt,sha256=lX3uoizHLFaUndwv_d_B-feK3mI2Z3uejU9kufZLcW0,52
14
+ cachier-2.2.1.dist-info/top_level.txt,sha256=_rW_HiJumDCch67YT-WAgzcyvKg5RiYDMZq9d-0ZpaE,8
15
+ cachier-2.2.1.dist-info/RECORD,,
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ cachier = cachier.scripts.cli:cli
@@ -1,4 +0,0 @@
1
-
2
- [console_scripts]
3
- cachier=cachier.scripts.cli:cli
4
-