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:

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

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.

Source Distribution

immerframe-0.0.3.tar.gz (4.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page