Skip to content

Commit fcc6d6e

Browse files
committed
docs: update path of demos migrated to tests in README.md
refactor: remove `set_customer_serializer` in favor of overridable `serialize_value`
1 parent 9de215e commit fcc6d6e

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Version 0.12.2
4+
5+
- docs: update path of demos migrated to tests in `README.md`
6+
- refactor: remove `set_customer_serializer` in favor of overridable `serialize_value`
7+
38
## Version 0.12.1
49

510
- refactor: move store serializer from test framework to code `Store` class

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ side effects.
8181
This concept fills the gap in handling side effects within Redux's ecosystem, offering
8282
a more nuanced and integrated approach to state and side effect management.
8383

84-
See todo sample below or check the [demo](demo.py) to see it in action.
84+
See todo sample below or check the [todo demo](/tests/test_todo.py) or
85+
[features demo](/tests/test_features.py) to see it in action.
8586

8687
### Autorun Decorator
8788

@@ -97,7 +98,8 @@ selector's return value. This mechanism ensures that the decorated function runs
9798
in response to relevant state changes, enhancing efficiency and responsiveness in
9899
the application.
99100

100-
See todo sample below or check the [demo](demo.py) to see it in action.
101+
See todo sample below or check the [todo demo](/tests/test_todo.py) or
102+
[features demo](/tests/test_features.py) to see it in action.
101103

102104
### Combining reducers - `combine_reducers`
103105

@@ -241,7 +243,7 @@ store.dispatch(FinishAction())
241243

242244
## 🎉 Demo
243245

244-
For a detailed example, see `demo.py` in the [GitHub repository](https://github.com/sassanh/python-redux).
246+
For a detailed example, see [features demo](/tests/test_features.py).
245247

246248
## 🤝 Contributing
247249

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-redux"
3-
version = "0.12.1"
3+
version = "0.12.2"
44
description = "Redux implementation for Python"
55
authors = ["Sassan Haradji <[email protected]>"]
66
license = "Apache-2.0"

redux/main.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ruff: noqa: D100, D101, D102, D103, D104, D105, D107
1+
"""Redux store for managing state and side effects."""
22
from __future__ import annotations
33

44
import dataclasses
@@ -74,13 +74,14 @@ def run(self: _SideEffectRunnerThread[Event]) -> None:
7474

7575

7676
class Store(Generic[State, Action, Event]):
77-
custom_serializer = None
77+
"""Redux store for managing state and side effects."""
7878

7979
def __init__(
8080
self: Store[State, Action, Event],
8181
reducer: ReducerType[State, Action, Event],
8282
options: CreateStoreOptions | None = None,
8383
) -> None:
84+
"""Create a new store."""
8485
self.store_options = options or CreateStoreOptions()
8586
self.reducer = reducer
8687
self._create_task: Callable[[Coroutine], Any] = (
@@ -175,6 +176,7 @@ def _run_event_handlers(self: Store[State, Action, Event]) -> None:
175176
cast(Callable[[], Any], event_handler)()
176177

177178
def run(self: Store[State, Action, Event]) -> None:
179+
"""Run the store."""
178180
with self._is_running:
179181
while len(self._actions) > 0 or len(self._events) > 0:
180182
if len(self._actions) > 0:
@@ -189,6 +191,7 @@ def dispatch(
189191
with_state: Callable[[State | None], DispatchParameters[Action, Event]]
190192
| None = None,
191193
) -> None:
194+
"""Dispatch actions and/or events."""
192195
if with_state is not None:
193196
self.dispatch(with_state(self._state))
194197

@@ -217,6 +220,7 @@ def subscribe(
217220
*,
218221
keep_ref: bool = True,
219222
) -> Callable[[], None]:
223+
"""Subscribe to state changes."""
220224
if keep_ref:
221225
listener_ref = listener
222226
elif inspect.ismethod(listener):
@@ -234,6 +238,7 @@ def subscribe_event(
234238
*,
235239
options: EventSubscriptionOptions | None = None,
236240
) -> Callable[[], None]:
241+
"""Subscribe to events."""
237242
subscription_options = (
238243
EventSubscriptionOptions() if options is None else options
239244
)
@@ -271,6 +276,8 @@ def autorun(
271276
SelectorOutput,
272277
AutorunOriginalReturnType,
273278
]:
279+
"""Create a new autorun, reflecting on state changes."""
280+
274281
def decorator(
275282
func: Callable[[SelectorOutput], AutorunOriginalReturnType]
276283
| Callable[[SelectorOutput, SelectorOutput], AutorunOriginalReturnType],
@@ -285,26 +292,19 @@ def decorator(
285292

286293
return decorator
287294

288-
def set_custom_serializer(
289-
self: Store,
290-
serializer: Callable[[object | type], SnapshotAtom],
291-
) -> None:
292-
"""Set a custom serializer for the store snapshot."""
293-
self.custom_serializer = serializer
294-
295295
@property
296296
def snapshot(self: Store[State, Action, Event]) -> SnapshotAtom:
297-
return self._serialize_value(self._state)
297+
"""Return a snapshot of the current state of the store."""
298+
return self.serialize_value(self._state)
298299

299-
def _serialize_value(self: Store, obj: object | type) -> SnapshotAtom:
300-
if self.custom_serializer:
301-
return self.custom_serializer(obj)
300+
def serialize_value(self: Store, obj: object | type) -> SnapshotAtom:
301+
"""Serialize a value to a snapshot atom."""
302302
if is_immutable(obj):
303303
return self._serialize_dataclass_to_dict(obj)
304304
if isinstance(obj, (list, tuple)):
305-
return [self._serialize_value(i) for i in obj]
305+
return [self.serialize_value(i) for i in obj]
306306
if callable(obj):
307-
return self._serialize_value(obj())
307+
return self.serialize_value(obj())
308308
if isinstance(obj, StrEnum):
309309
return str(obj)
310310
if isinstance(obj, IntEnum):
@@ -320,6 +320,6 @@ def _serialize_dataclass_to_dict(
320320
) -> dict[str, Any]:
321321
result = {}
322322
for field in dataclasses.fields(obj):
323-
value = self._serialize_value(getattr(obj, field.name))
323+
value = self.serialize_value(getattr(obj, field.name))
324324
result[field.name] = value
325325
return result

0 commit comments

Comments
 (0)