creates the next immutable object by simply modifying the current one
Project description
immerframe
Intuitively perform deep updates on python objects
- without mutating them
- while being efficient via structural sharing - no
deepcopy
- while maintaining type correctness
This is (almost) a Python port of immer.
pip install immerframe
First, let's import some stuff
from dataclasses import dataclass
from immerframe import Proxy
Now, consider the data:
@dataclass
class Ant:
age: int
ant_10 = Ant(age=10)
ant_20 = Ant(age=20)
nested = {
"ants": [ant_10, ant_20, None],
}
let's pretend to mutate it
with Proxy(nested) as (p, new_nested):
p["ants"][0].age += 1
p["ants"].pop()
p["foo"] = 99
(note p
and new_nested
should have the correct types in mypy)
nested
will remain the same
assert nested == {
"ants": [ant_10, ant_20, None],
}
new_nested
will be nested
, but with the mutations with specified in the with Proxy(...)
block
assert new_nested == { "ants": [Ant(age=11), ant_20], "foo": 99, }
but it _won't_ be a deep copy
```python
assert new_nested["ants"][1] is ant_20
immerframe
supports most thing's that can be copy
ed
Things to remember
Mutating the same thing in a block twice may not do what you'd expect in the following type of case:
with Proxy([1]) as (l, new_l):
l[0] += 5
l[0] += 10
will give l == [11]
Use keys rather than references to loop over and mutate nested dict
s/list
s:
with Proxy(new_nested) as (p, new_nested):
for k in new_nested:
p[k] += 1
for i, n in enumerate(new_nested["ants"]):
if n.age < 15:
p["ants"][i].age + 10
TODO
- implement all dunder methods
- finish typing
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
immerframe-0.1.0.tar.gz
(5.0 kB
view details)
File details
Details for the file immerframe-0.1.0.tar.gz
.
File metadata
- Download URL: immerframe-0.1.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b683bcc7850195601fffdfd99798e4800277e948564eaadc46de3b5b6bac5a16 |
|
MD5 | 3ee433c5bcf9da9bc46fbc4ff4e065e1 |
|
BLAKE2b-256 | 3f999b7a59060843251b50dae37cdd11a462850a19393be4f4a0d09c418010b0 |