Skip to main content

creates the next immutable object by simply modifying the current one

Project description

immerframe

Create the next immutable object by simply modifying the current one

This is a Python port of immer.

pip install immerframe

Want to do a deep update on a Python data structure without mutating it? No problem:

from immerframe import Proxy, produce

Ant = namedtuple('Ant', 'age')
nested = {
    'foo': [
        Ant(age=2),
        'bar',
    ],
}

proxy = Proxy()
proxy['foo'][0].age += 1
proxy['foo'].pop()
proxy['qux'] = 99

new_nested = produce(proxy, nested)

new_nested will now equal

{
    'foo': [
        Ant(age=3),
    ],
    'qux': 99,
}

while nested will remain unchanged.

"What about my typing?"

from typing import cast

Cat = namedtuple('Cat', 'name')

proxy = cast(Cat, Proxy())
# continue as before but with autocomplete and type checking!
proxy.name = 'Felix'

immerframe uses structural sharing, so should be efficient in most cases:

d = {'foo': 1}
l = [d]

proxy = Proxy()
proxy.append(100)
new_l = produce(proxy, l)
assert new_l == [d, 100]
assert new_l[0] is d

immerframe supports:

  • dicts
  • lists
  • sets
  • tupless
  • namedtupless
  • attrss

Lens

immerframe comes with a Lens class to help with path reuse, it has .get, .set, .modify:

d = {'foo': [1, 2, 3, 4]}

lens = Lens(Proxy()['foo'][1])

new_d = lens.set(d, 100)
assert new_d == {'foo': [1, 100, 3, 4]}
assert d == {'foo': [1, 2, 3, 4]}
assert lens.get(d) == 2

another_d = lens.modify(d, lambda n: n + 1000)
assert another_d == {'foo': [1, 1002, 3, 4]}

Plugins:

immerframe currently has an attrs plugin, registering plugins is pretty easy, just mutate the immerframe.plugins list (see here for the structure of the existing attr plugin).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for immerframe, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size immerframe-0.0.4.tar.gz (4.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page