Skip to main content

PyMutator

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

Metadata

Release files for PyMutator 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyMutator 0.1
File Size Uploaded Provenance Actions
PyMutator-0.1.tar.gz 4.0 kB Without provenance Details for PyMutator-0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for PyMutator 0.1
File Interpreter ABI Platform Actions
PyMutator-0.1-py3-none-any.whl Python 3 none any Details for PyMutator-0.1-py3-none-any.whl

Total release size: 8.3 kB

Release files / PyMutator-0.1.tar.gz

Download URL PyMutator-0.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c3e0d673589c28748287e45866462cdd784619cd6472fa51547d4463e511e9b3
BLAKE2b-256 checksum
How to use checksums
ce751987b6c5310b225defaa06d5d86e809ab2fa6402da1f6ca0922d5576aea0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / PyMutator-0.1-py3-none-any.whl

Download URL PyMutator-0.1-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e30cea78e7c317a0305027be16a34877321c3d74d568d3cc6e2542e99fb42c9d
BLAKE2b-256 checksum
How to use checksums
b7796ce836cf7f074e15d5701c4ea66eab6a3e16027e981e4d0399af4d41e0ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Security for PyMutator 0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page