SolidJS style updating of nested python objects
Project description
Mutate nested Python objects using a method inspired by SolidJS’s setState function for updating stores.
https://www.solidjs.com/docs/latest/api#updating-stores
Examples
update lists by index
>>> from pymutator import mutate >>> mutate([1, 2, 3, 4, 5], 3, 'D') [1, 2, 3, 'D', 5]
update lists by list of indices
>>> mutate([1, 2, 3, 4, 5], [1, 3], 'D') [1, 'D', 3, 'D', 5]
update lists by filter predicate
>>> mutate([1, 2, 3, 4, 5], lambda i: i%2, 'odd') ['odd', 2, 'odd', 4, 'odd']
update list with computed value
>>> mutate([1, 2, 3, 4, 5], 3, lambda i: f'D{i}') [1, 2, 3, 'D4', 5]
update dict
>>> mutate({'a': 1, 'b': 2, 'c': 3}, 'b', 'B') {'a': 1, 'b': 'B', 'c': 3}>>> mutate({'a': 1, 'b': 2, 'c': 3}, b='B') {'a': 1, 'b': 'B', 'c': 3}>>> mutate({'a': 1, 'b': 2, 'c': 3}, {'b': 'B'}) {'a': 1, 'b': 'B', 'c': 3}>>> mutate({'a': 1, 'b': 2, 'c': 3}, ['b', 'x'], 'B') {'a': 1, 'b': 'B', 'c': 3, 'x': 'B'}
update dict with filter predicate
>>> mutate({'a': 1, 'b': 2, 'c': 3}, lambda i: i%2, 'odd') {'a': 'odd', 'b': 2, 'c': 'odd'}
update dict with computed value
>>> mutate({'a': 1, 'b': 2, 'c': 3}, 'b', lambda i: f'B{i}') {'a': 1, 'b': 'B2', 'c': 3}
update object
>>> class O: ... def __init__(self): self.a = 1 >>> o = O() >>> mutate(o, 'a', 2) and o.a 2>>> mutate(o, a=3) and o.a 3>>> mutate(o, {'a': 4}) and o.a 4>>> mutate(o, 'a', lambda i: i+1) and o.a 5
updating nested objects
>>> store = { ... 'todos': [ ... {'task': 'Finish work', 'completed': False}, ... {'task': 'Go grocery shopping', 'completed': False}, ... {'task': 'Make dinner', 'completed': False}, ... ] ... }>>> mutate(store, 'todos', [0, 2], 'completed', True) {'todos': [{'task': 'Finish work', 'completed': True}, {'task': 'Go grocery shopping', 'completed': False}, {'task': 'Make dinner', 'completed': True}]}>>> mutate(store, 'todos', lambda todo: todo['completed'], ... 'task', lambda t: t + '!') {'todos': [{'task': 'Finish work!', 'completed': True}, {'task': 'Go grocery shopping', 'completed': False}, {'task': 'Make dinner!', 'completed': True}]}
Running tests
$ pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
PyMutator-0.1.tar.gz
(4.0 kB
view details)
Built Distribution
File details
Details for the file PyMutator-0.1.tar.gz
.
File metadata
- Download URL: PyMutator-0.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
c3e0d673589c28748287e45866462cdd784619cd6472fa51547d4463e511e9b3
|
|
MD5 |
ae297236f84d328a4e475f807512db5a
|
|
BLAKE2b-256 |
ce751987b6c5310b225defaa06d5d86e809ab2fa6402da1f6ca0922d5576aea0
|
File details
Details for the file PyMutator-0.1-py3-none-any.whl
.
File metadata
- Download URL: PyMutator-0.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
e30cea78e7c317a0305027be16a34877321c3d74d568d3cc6e2542e99fb42c9d
|
|
MD5 |
c07d11e56b191f631e5602da9652a239
|
|
BLAKE2b-256 |
b7796ce836cf7f074e15d5701c4ea66eab6a3e16027e981e4d0399af4d41e0ca
|