Skip to main content

Immutable Collections

Project description

https://github.com/MagicStack/immutables/workflows/Tests/badge.svg?branch=master https://img.shields.io/pypi/v/immutables.svg

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

API

immutables.Map is an unordered immutable mapping. Map objects are hashable, comparable, and pickleable.

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

import immutables

map = immutables.Map(a=1, b=2)

print(map['a'])
# will print '1'

print(map.get('z', 100))
# will print '100'

print('z' in map)
# will print 'False'

Since Maps are immutable, there is a special API for mutations that allow apply changes to the Map object and create new (derived) Maps:

map2 = map.set('a', 10)
print(map, map2)
# will print:
#   <immutables.Map({'a': 1, 'b': 2})>
#   <immutables.Map({'a': 10, 'b': 2})>

map3 = map2.delete('b')
print(map, map2, map3)
# will print:
#   <immutables.Map({'a': 1, 'b': 2})>
#   <immutables.Map({'a': 10, 'b': 2})>
#   <immutables.Map({'a': 10})>

Maps also implement APIs for bulk updates: MapMutation objects:

map_mutation = map.mutate()
map_mutation['a'] = 100
del map_mutation['b']
map_mutation.set('y', 'y')

map2 = map_mutation.finish()

print(map, map2)
# will print:
#   <immutables.Map({'a': 1, 'b': 2})>
#   <immutables.Map({'a': 100, 'y': 'y'})>

MapMutation objects are context managers. Here’s the above example rewritten in a more idiomatic way:

with map.mutate() as mm:
    mm['a'] = 100
    del mm['b']
    mm.set('y', 'y')
    map2 = mm.finish()

print(map, map2)
# will print:
#   <immutables.Map({'a': 1, 'b': 2})>
#   <immutables.Map({'a': 100, 'y': 'y'})>

Further development

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

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.14.tar.gz (43.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

immutables-0.14-cp38-cp38-win_amd64.whl (54.1 kB view details)

Uploaded CPython 3.8Windows x86-64

immutables-0.14-cp38-cp38-manylinux1_x86_64.whl (102.6 kB view details)

Uploaded CPython 3.8

immutables-0.14-cp38-cp38-macosx_10_14_x86_64.whl (52.2 kB view details)

Uploaded CPython 3.8macOS 10.14+ x86-64

immutables-0.14-cp37-cp37m-win_amd64.whl (54.1 kB view details)

Uploaded CPython 3.7mWindows x86-64

immutables-0.14-cp37-cp37m-manylinux1_x86_64.whl (99.2 kB view details)

Uploaded CPython 3.7m

immutables-0.14-cp37-cp37m-macosx_10_14_x86_64.whl (52.1 kB view details)

Uploaded CPython 3.7mmacOS 10.14+ x86-64

immutables-0.14-cp36-cp36m-win_amd64.whl (54.1 kB view details)

Uploaded CPython 3.6mWindows x86-64

immutables-0.14-cp36-cp36m-manylinux1_x86_64.whl (98.3 kB view details)

Uploaded CPython 3.6m

immutables-0.14-cp36-cp36m-macosx_10_14_x86_64.whl (52.1 kB view details)

Uploaded CPython 3.6mmacOS 10.14+ x86-64

immutables-0.14-cp35-cp35m-manylinux1_x86_64.whl (98.2 kB view details)

Uploaded CPython 3.5m

immutables-0.14-cp35-cp35m-macosx_10_14_x86_64.whl (52.1 kB view details)

Uploaded CPython 3.5mmacOS 10.14+ x86-64

File details

Details for the file immutables-0.14.tar.gz.

File metadata

  • Download URL: immutables-0.14.tar.gz
  • Upload date:
  • Size: 43.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14.tar.gz
Algorithm Hash digest
SHA256 a0a1cc238b678455145bae291d8426f732f5255537ed6a5b7645949704c70a78
MD5 7f1667577ba7701fba503a59a25f2e5f
BLAKE2b-256 6b58c6be0577cccbe1658fcb1ec3673e9ac7508af26a2f5c814cc041c7d21212

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: immutables-0.14-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 54.1 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 ef9da20ec0f1c5853b5c8f8e3d9e1e15b8d98c259de4b7515d789a606af8745e
MD5 d58d50493e9c9d0ce01c1894252dd446
BLAKE2b-256 964a0e8fbc4a67b66b24e3a2bdcc7ab219df4f0d6dda174d74354b955a6b078e

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: immutables-0.14-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 102.6 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c453e12b95e1d6bb4909e8743f88b7f5c0c97b86a8bc0d73507091cb644e3c1e
MD5 072f629525e67c853640b30b3c40303f
BLAKE2b-256 ca46a8174fbbd3e05f97aec675e6c072d8b257bacecf097edd4f7a4e565669b7

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: immutables-0.14-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 52.2 kB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 1c11050c49e193a1ec9dda1747285333f6ba6a30bbeb2929000b9b1192097ec0
MD5 fbaf8a32836b6789f842b83b74013995
BLAKE2b-256 211637382487da5103eaedc8b5dbcd6ecb14c8ac06b467437f81414698a62d23

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: immutables-0.14-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 54.1 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 714aedbdeba4439d91cb5e5735cb10631fc47a7a69ea9cc8ecbac90322d50a4a
MD5 908cc3f8a9b490d958854546d0eb4e79
BLAKE2b-256 751c75e6da2100bfb078d32cdc501ed205b0686153c1fa9637522547a0d6d613

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: immutables-0.14-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 99.2 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c099212fd6504513a50e7369fe281007c820cf9d7bb22a336486c63d77d6f0b2
MD5 ddc1835ca3be1bf4e9f837ab5777ab21
BLAKE2b-256 a916696b75dfaca5d081d3d94d870d3b8f3e2aac1948dc6f53763aedf16f287c

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: immutables-0.14-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 52.1 kB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ab6c18b7b2b2abc83e0edc57b0a38bf0915b271582a1eb8c7bed1c20398f8040
MD5 f0e1b59c60952e93aabb1910661f1b95
BLAKE2b-256 d4cbd450dc6cd961c1504b5a1df37a0ae7a19f0769e7573e0d64ec38e4ac2111

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: immutables-0.14-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 54.1 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 6c8eace4d98988c72bcb37c05e79aae756832738305ae9497670482a82db08bc
MD5 a2f22fc11f6d9237dc60c5999b4eb259
BLAKE2b-256 cc20e86be7e828a12a0d8895fff21c71bb932521c1ffc5528d32a58d90541cbd

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: immutables-0.14-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 98.3 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 33ce2f977da7b5e0dddd93744862404bdb316ffe5853ec853e53141508fa2e6a
MD5 762d044fdda0199997ef1066142d9c78
BLAKE2b-256 99e0ea6fd4697120327d26773b5a84853f897a68e33d3f9376b00a8ff96e4f63

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp36-cp36m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: immutables-0.14-cp36-cp36m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 52.1 kB
  • Tags: CPython 3.6m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 8797eed4042f4626b0bc04d9cf134208918eb0c937a8193a2c66df5041e62d2e
MD5 8ae9a16a9af815d3e52c8e5f940f68b7
BLAKE2b-256 b4d7ce6edad6824b99c3ee0de2bb8f10f61608d62d139e9ff93e0b5835cd1083

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: immutables-0.14-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 98.2 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ce01788878827c3f0331c254a4ad8d9721489a5e65cc43e19c80040b46e0d297
MD5 3e4b0be8d84cba7c7aab30c0547a2c9d
BLAKE2b-256 6075f616eda3929dea18f9d68a96e076daf4945f0be67687f628fc35b34c87d8

See more details on using hashes here.

File details

Details for the file immutables-0.14-cp35-cp35m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: immutables-0.14-cp35-cp35m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 52.1 kB
  • Tags: CPython 3.5m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for immutables-0.14-cp35-cp35m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 860666fab142401a5535bf65cbd607b46bc5ed25b9d1eb053ca8ed9a1a1a80d6
MD5 d8150fd2307db56041dd4c33436555c7
BLAKE2b-256 d49b9d552a4046e1a23a96a9736d61d874adde41e872e1cdbe170a0bdaeb6baa

See more details on using hashes here.

Supported by

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