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]}

Lenss are composable via their .proxy() method, (this duplicates of the originally provided proxy) Lens(Lens(Proxy()['foo']).proxy()[1]) is equivalent to Lens(Proxy()['foo'][1]).

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.

Filename, size & hash SHA256 hash help File type Python version Upload date
immerframe-0.0.5.tar.gz (4.5 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page