ddutils 0.4.1__tar.gz → 0.4.3__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.4
2
2
  Name: ddutils
3
- Version: 0.4.1
3
+ Version: 0.4.3
4
4
  Summary: Shared utilities of the dd libraries: annotation and class helpers, convertors, scoped registries, connection factories and feature flags
5
5
  Keywords: python,ddutils,utils,helpers,type hints,type annotations,introspection,generics,scoped registry,context scope,connection manager,connection factory,feature flags,feature toggle,retry,safe call,decorators,convertors,asyncio
6
6
  Author: davyddd
@@ -11,8 +11,9 @@ Classifier: Programming Language :: Python :: 3.11
11
11
  Classifier: Programming Language :: Python :: 3.12
12
12
  Classifier: Programming Language :: Python :: 3.13
13
13
  Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Programming Language :: Python :: 3.15
14
15
  Classifier: Typing :: Typed
15
- Requires-Python: >=3.11, <3.15
16
+ Requires-Python: >=3.11, <3.16
16
17
  Project-URL: Repository, https://github.com/davyddd/ddutils
17
18
  Description-Content-Type: text/markdown
18
19
 
@@ -50,7 +51,7 @@ run `uvx copier update` to pull in template updates.
50
51
 
51
52
  ## Compatibility
52
53
 
53
- Python 3.11 to 3.14. What changed in each release, breaking changes included,
54
+ Python 3.11 to 3.15. What changed in each release, breaking changes included,
54
55
  is in the [GitHub releases](https://github.com/davyddd/ddutils/releases).
55
56
 
56
57
  <!-- The sections below are the library's own; copier update leaves them alone -->
@@ -667,9 +668,10 @@ if await feature_flags.is_enabled('email_provider_block'):
667
668
 
668
669
  `FeatureFlags(storage, base_ttl=BASE_TTL)` caches every flag it reads for `base_ttl` (15) plus 0 to `JITTER` (5)
669
670
  seconds, so the readers of a process do not re-read at once. The cache belongs to the instance: keep one per
670
- process, and expect a `put` to take up to a TTL to be seen, also from the process that wrote it. While the
671
- storage fails, the last known record (or the defaults) is served and logged, and the re-read is retried with a
672
- backoff from `ERROR_TTL` (1 second) doubling on every failure up to `base_ttl`.
671
+ process, and expect a `put` to take up to a TTL to be seen, also from the process that wrote it. Assigning another
672
+ storage to `feature_flags.storage` drops the cache. While the storage fails, the last known record (or the
673
+ defaults) is served and logged, and the re-read is retried with a backoff from `ERROR_TTL` (1 second) doubling on
674
+ every failure up to `base_ttl`.
673
675
 
674
676
  A storage is a `FeatureFlagStorage` with `get(name) -> FeatureFlagRecord | None`, `get_map() -> dict[str, FeatureFlagRecord]`,
675
677
  `put(name, record) -> None` (create or overwrite; there is no create-only) and `delete(name) -> bool`;
@@ -678,6 +680,21 @@ atomically. A storage builds records with `from_dict` and, when what it read is
678
680
  reads it as the defaults. Only a failure of the storage itself raises, as `FeatureFlagStorageError` with the cause
679
681
  chained.
680
682
 
683
+ `InMemoryFeatureFlagStorage()` is a storage in a dict, for a process with no storage of its own, a test process:
684
+ what is put is what is read, and a new instance starts empty. A test setup puts a new one behind the application's
685
+ `FeatureFlags`, which also drops the cache, so nothing one test set is read by the next, and a test sets a flag the
686
+ way an admin panel writes one:
687
+
688
+ ```python
689
+ from ddutils.feature_flags import FeatureFlagRecord, InMemoryFeatureFlagStorage
690
+
691
+ # before every test
692
+ feature_flags.storage = InMemoryFeatureFlagStorage()
693
+
694
+ # in a test
695
+ await feature_flags.storage.put('new_checkout', FeatureFlagRecord(enabled=True))
696
+ ```
697
+
681
698
  ## Context Helpers
682
699
 
683
700
  ### async_suppress
@@ -32,7 +32,7 @@ run `uvx copier update` to pull in template updates.
32
32
 
33
33
  ## Compatibility
34
34
 
35
- Python 3.11 to 3.14. What changed in each release, breaking changes included,
35
+ Python 3.11 to 3.15. What changed in each release, breaking changes included,
36
36
  is in the [GitHub releases](https://github.com/davyddd/ddutils/releases).
37
37
 
38
38
  <!-- The sections below are the library's own; copier update leaves them alone -->
@@ -649,9 +649,10 @@ if await feature_flags.is_enabled('email_provider_block'):
649
649
 
650
650
  `FeatureFlags(storage, base_ttl=BASE_TTL)` caches every flag it reads for `base_ttl` (15) plus 0 to `JITTER` (5)
651
651
  seconds, so the readers of a process do not re-read at once. The cache belongs to the instance: keep one per
652
- process, and expect a `put` to take up to a TTL to be seen, also from the process that wrote it. While the
653
- storage fails, the last known record (or the defaults) is served and logged, and the re-read is retried with a
654
- backoff from `ERROR_TTL` (1 second) doubling on every failure up to `base_ttl`.
652
+ process, and expect a `put` to take up to a TTL to be seen, also from the process that wrote it. Assigning another
653
+ storage to `feature_flags.storage` drops the cache. While the storage fails, the last known record (or the
654
+ defaults) is served and logged, and the re-read is retried with a backoff from `ERROR_TTL` (1 second) doubling on
655
+ every failure up to `base_ttl`.
655
656
 
656
657
  A storage is a `FeatureFlagStorage` with `get(name) -> FeatureFlagRecord | None`, `get_map() -> dict[str, FeatureFlagRecord]`,
657
658
  `put(name, record) -> None` (create or overwrite; there is no create-only) and `delete(name) -> bool`;
@@ -660,6 +661,21 @@ atomically. A storage builds records with `from_dict` and, when what it read is
660
661
  reads it as the defaults. Only a failure of the storage itself raises, as `FeatureFlagStorageError` with the cause
661
662
  chained.
662
663
 
664
+ `InMemoryFeatureFlagStorage()` is a storage in a dict, for a process with no storage of its own, a test process:
665
+ what is put is what is read, and a new instance starts empty. A test setup puts a new one behind the application's
666
+ `FeatureFlags`, which also drops the cache, so nothing one test set is read by the next, and a test sets a flag the
667
+ way an admin panel writes one:
668
+
669
+ ```python
670
+ from ddutils.feature_flags import FeatureFlagRecord, InMemoryFeatureFlagStorage
671
+
672
+ # before every test
673
+ feature_flags.storage = InMemoryFeatureFlagStorage()
674
+
675
+ # in a test
676
+ await feature_flags.storage.put('new_checkout', FeatureFlagRecord(enabled=True))
677
+ ```
678
+
663
679
  ## Context Helpers
664
680
 
665
681
  ### async_suppress
@@ -101,7 +101,8 @@ class FeatureFlagStorage(ABC):
101
101
 
102
102
  A missing flag is `None`, a malformed one is read as the defaults: only a failure of the storage itself
103
103
  raises, as `FeatureFlagStorageError`, so that `FeatureFlags` can keep serving the last known values.
104
- Implementations: `ddredis.feature_flags.RedisFeatureFlagStorage`, `ddaerospike.feature_flags.AerospikeFeatureFlagStorage`.
104
+ Implementations: `ddredis.feature_flags.RedisFeatureFlagStorage`, `ddaerospike.feature_flags.AerospikeFeatureFlagStorage`,
105
+ and `InMemoryFeatureFlagStorage` below for a process with no storage of its own.
105
106
  """
106
107
 
107
108
  @abstractmethod
@@ -125,6 +126,32 @@ class FeatureFlagStorage(ABC):
125
126
  await self.delete(old_name)
126
127
 
127
128
 
129
+ class InMemoryFeatureFlagStorage(FeatureFlagStorage):
130
+ """
131
+ A `FeatureFlagStorage` in a dict, for a process with no storage of its own, such as a test process: what is
132
+ put is what is read. The records belong to the instance, so a new instance starts empty.
133
+
134
+ Example:
135
+ feature_flags.storage = InMemoryFeatureFlagStorage() # before the test
136
+ await feature_flags.storage.put('new_checkout', FeatureFlagRecord(enabled=True))
137
+ """
138
+
139
+ def __init__(self) -> None:
140
+ self._records: dict[str, FeatureFlagRecord] = {}
141
+
142
+ async def get(self, name: str) -> FeatureFlagRecord | None:
143
+ return self._records.get(name)
144
+
145
+ async def get_map(self) -> dict[str, FeatureFlagRecord]:
146
+ return dict(self._records)
147
+
148
+ async def put(self, name: str, record: FeatureFlagRecord) -> None:
149
+ self._records[name] = record
150
+
151
+ async def delete(self, name: str) -> bool:
152
+ return self._records.pop(name, None) is not None
153
+
154
+
128
155
  class _CacheEntry(NamedTuple):
129
156
  record: FeatureFlagRecord
130
157
  expiration_time: float
@@ -158,10 +185,23 @@ class FeatureFlags:
158
185
  """
159
186
 
160
187
  def __init__(self, storage: FeatureFlagStorage, base_ttl: int = BASE_TTL) -> None:
161
- self.storage = storage
188
+ self._storage = storage
162
189
  self.base_ttl = base_ttl
163
190
  self._cache: dict[str, _CacheEntry] = {}
164
191
 
192
+ @property
193
+ def storage(self) -> FeatureFlagStorage:
194
+ """
195
+ The storage the flags are read from. Assigning another one drops the cache, so no record read from the
196
+ old storage is served after the swap.
197
+ """
198
+ return self._storage
199
+
200
+ @storage.setter
201
+ def storage(self, storage: FeatureFlagStorage) -> None:
202
+ self._storage = storage
203
+ self._cache.clear()
204
+
165
205
  async def is_enabled(self, name: str, idempotent_key: Stringable | None = None) -> bool:
166
206
  """
167
207
  Whether the flag is on for this reader: the stored switch is on and the reader falls into the
@@ -223,5 +263,6 @@ __all__ = (
223
263
  'FeatureFlagStorage',
224
264
  'FeatureFlagStorageError',
225
265
  'FeatureFlags',
266
+ 'InMemoryFeatureFlagStorage',
226
267
  'Stringable',
227
268
  )
@@ -1,11 +1,11 @@
1
1
  [project]
2
2
  name = "ddutils"
3
3
  description = "Shared utilities of the dd libraries: annotation and class helpers, convertors, scoped registries, connection factories and feature flags"
4
- version = "0.4.1"
4
+ version = "0.4.3"
5
5
  license = "MIT"
6
6
  license-files = ["LICENSE"]
7
7
  readme = "README.md"
8
- requires-python = ">=3.11,<3.15"
8
+ requires-python = ">=3.11,<3.16"
9
9
  keywords = [
10
10
  "python",
11
11
  "ddutils",
@@ -33,6 +33,7 @@ classifiers = [
33
33
  "Programming Language :: Python :: 3.12",
34
34
  "Programming Language :: Python :: 3.13",
35
35
  "Programming Language :: Python :: 3.14",
36
+ "Programming Language :: Python :: 3.15",
36
37
  "Typing :: Typed",
37
38
  ]
38
39
  dependencies = []
@@ -63,5 +64,5 @@ linter = [
63
64
  module-root = ""
64
65
 
65
66
  [build-system]
66
- requires = ["uv_build>=0.12.0,<0.13.0"]
67
+ requires = ["uv_build>=0.13.0,<0.14.0"]
67
68
  build-backend = "uv_build"
@@ -1,12 +1,12 @@
1
1
  [project]
2
2
  name = "ddutils"
3
3
  description = "Shared utilities of the dd libraries: annotation and class helpers, convertors, scoped registries, connection factories and feature flags"
4
- version = "0.4.1"
4
+ version = "0.4.3"
5
5
  authors = [{ name = "davyddd" }]
6
6
  license = "MIT"
7
7
  license-files = ["LICENSE"]
8
8
  readme = "README.md"
9
- requires-python = ">=3.11,<3.15"
9
+ requires-python = ">=3.11,<3.16"
10
10
  keywords = ["python", "ddutils", "utils", "helpers", "type hints", "type annotations", "introspection", "generics", "scoped registry", "context scope", "connection manager", "connection factory", "feature flags", "feature toggle", "retry", "safe call", "decorators", "convertors", "asyncio"]
11
11
  # uv_build does not derive these from requires-python (poetry did); PyPI and the pyversions badge read them
12
12
  classifiers = [
@@ -15,6 +15,7 @@ classifiers = [
15
15
  "Programming Language :: Python :: 3.12",
16
16
  "Programming Language :: Python :: 3.13",
17
17
  "Programming Language :: Python :: 3.14",
18
+ "Programming Language :: Python :: 3.15",
18
19
  "Typing :: Typed",
19
20
  ]
20
21
  dependencies = []
@@ -42,5 +43,5 @@ linter = [
42
43
  module-root = ""
43
44
 
44
45
  [build-system]
45
- requires = ["uv_build>=0.12.0,<0.13.0"]
46
+ requires = ["uv_build>=0.13.0,<0.14.0"]
46
47
  build-backend = "uv_build"
File without changes
File without changes
File without changes
File without changes
File without changes