Skip to main content

Immutable Collections

Project description

https://travis-ci.org/MagicStack/immutables.svg?branch=master https://ci.appveyor.com/api/projects/status/tgbc6tq56u63qqhf?svg=true

An immutable mapping type for Python.

The underlying datastructure is a Hash Array Mapped Trie (HAMT) used in Clojure, Scala, Haskell, and other functional languages. This implementation is used in CPython 3.7 in the contextvars module (see PEP 550 and PEP 567 for more details).

Immutable mappings based on HAMT have O(log N) performance for both set() and get() operations, which is essentially O(1) for relatively small mappings.

Below is a visualization of a simple get/set benchmark comparing HAMT to an immutable mapping implemented with a Python dict copy-on-write approach (the benchmark code is available here):

bench.png

Installation

immutables requires Python 3.5+ and is available on PyPI:

$ pip install immutables

immutables.Map

The Map object implements collections.abc.Mapping ABC so working with it is very similar to working with Python dicts.

The only exception are its Map.set() and Map.delete() methods which return a new instance of Map:

m1 = Map()  # an empty Map
m2 = m1.set('key1', 'val1')  # m2 has a 'key1' key, m1 is still empty

m3 = m2.set('key2', 'val2')
m3 = m3.delete('key1')  # m3 has only a 'key2' key

Further development

  • An immutable version of Python set type with efficient add() and discard() operations.

  • Add support for efficient Map.update() operation, allow to pass a set of key/values to Map(), and add support for pickling.

License

Apache 2.0

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

immutables-0.6.tar.gz (31.3 kB view hashes)

Uploaded Source

Built Distributions

immutables-0.6-cp36-cp36m-win_amd64.whl (38.9 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

immutables-0.6-cp36-cp36m-win32.whl (35.3 kB view hashes)

Uploaded CPython 3.6m Windows x86

immutables-0.6-cp36-cp36m-manylinux1_x86_64.whl (70.5 kB view hashes)

Uploaded CPython 3.6m

immutables-0.6-cp36-cp36m-manylinux1_i686.whl (67.0 kB view hashes)

Uploaded CPython 3.6m

immutables-0.6-cp36-cp36m-macosx_10_12_x86_64.whl (36.4 kB view hashes)

Uploaded CPython 3.6m macOS 10.12+ x86-64

immutables-0.6-cp35-cp35m-win_amd64.whl (38.9 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

immutables-0.6-cp35-cp35m-win32.whl (35.3 kB view hashes)

Uploaded CPython 3.5m Windows x86

immutables-0.6-cp35-cp35m-manylinux1_x86_64.whl (70.3 kB view hashes)

Uploaded CPython 3.5m

immutables-0.6-cp35-cp35m-manylinux1_i686.whl (66.8 kB view hashes)

Uploaded CPython 3.5m

immutables-0.6-cp35-cp35m-macosx_10_12_x86_64.whl (36.4 kB view hashes)

Uploaded CPython 3.5m macOS 10.12+ x86-64

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