pickle-function-cache 0.1.6__tar.gz → 0.1.7__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,8 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pickle-function-cache
3
- Version: 0.1.6
3
+ Version: 0.1.7
4
4
  Summary: Caching of function results in a pickle file.
5
- License-Expression: MIT
6
5
  License-File: LICENSE.txt
7
6
  Author: Oleksandr Zelentsov
8
7
  Author-email: oleksandrzelentsov@gmail.com
@@ -14,7 +14,8 @@ OMIT_CACHE = (
14
14
 
15
15
  @dataclass(frozen=True)
16
16
  class ArgsContainer:
17
- """Represents args and kwargs sent to a function."""
17
+ """Represents args and kwargs sent to a function.
18
+ Frozen to be able to serialize easily."""
18
19
 
19
20
  args: tuple[Any]
20
21
  kwargs: tuple[tuple[str, Any]]
@@ -27,7 +28,10 @@ class ArgsContainer:
27
28
  )
28
29
 
29
30
 
30
- def find_cache_file(filename: str, start_path: str | None = None) -> str:
31
+ def find_file_in_parents(filename: str, start_path: str | None = None) -> str:
32
+ """
33
+ Find a file by specified name in a directory or any of its parent directories.
34
+ """
31
35
  cwd = Path(start_path or os.getcwd()).resolve()
32
36
  if cwd.is_file():
33
37
  cwd = cwd.parent
@@ -66,7 +70,7 @@ class Cache:
66
70
  return {}
67
71
 
68
72
  def get_instance(self) -> dict:
69
- """Returns the cache."""
73
+ """Returns the cached values, deserialized."""
70
74
  if self._cache is None:
71
75
  if self.filename in self._filename_caches:
72
76
  self._cache = self._filename_caches[self.filename]
@@ -90,28 +94,17 @@ class Cache:
90
94
  pass
91
95
 
92
96
  def sync_global_cache(self):
97
+ """Record the cached values into the class variable."""
93
98
  cache = self.get_instance()
94
99
  self._filename_caches[self.filename] = cache
95
100
 
96
101
  @classmethod
97
102
  def invalidate_cache(cls, filename):
103
+ """Remove the file and remove the recorded data."""
98
104
  del cls._filename_caches[filename]
99
105
  os.unlink(filename)
100
106
 
101
107
 
102
- def set_cached_response(cache_filename, function_name, value, *args, **kwargs):
103
- cache_instance = Cache(filename=cache_filename)
104
- cache = cache_instance.get_instance()
105
- if not isinstance(function_name, str):
106
- function_name = function_name.__name__
107
- if function_name not in cache:
108
- cache[function_name] = {}
109
-
110
- arguments = ArgsContainer.from_args_kwargs(*args, **kwargs)
111
- cache[function_name][arguments] = value
112
- cache_instance.save_instance()
113
-
114
-
115
108
  if OMIT_CACHE:
116
109
 
117
110
  def cache_responses(cache_filename):
@@ -121,6 +114,7 @@ if OMIT_CACHE:
121
114
  return decor
122
115
 
123
116
  else:
117
+
124
118
  def cache_responses(cache_filename):
125
119
  """Decorate a function with this and the function will get its result cached in a file."""
126
120
 
@@ -1,12 +1,11 @@
1
1
  [project]
2
2
  name = "pickle-function-cache"
3
- version = "0.1.6"
3
+ version = "0.1.7"
4
4
  description = "Caching of function results in a pickle file."
5
5
  authors = [
6
6
  {name = "Oleksandr Zelentsov", email = "oleksandrzelentsov@gmail.com"}
7
7
  ]
8
8
  readme = "README.md"
9
- license = "MIT"
10
9
  requires-python = ">=3.10"
11
10
  dependencies = []
12
11