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

Uploaded Source

Built Distributions

immutables-0.15-cp39-cp39-win_amd64.whl (55.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

immutables-0.15-cp39-cp39-manylinux1_x86_64.whl (102.7 kB view details)

Uploaded CPython 3.9

immutables-0.15-cp39-cp39-macosx_10_14_x86_64.whl (53.9 kB view details)

Uploaded CPython 3.9 macOS 10.14+ x86-64

immutables-0.15-cp38-cp38-win_amd64.whl (56.0 kB view details)

Uploaded CPython 3.8 Windows x86-64

immutables-0.15-cp38-cp38-manylinux1_x86_64.whl (105.2 kB view details)

Uploaded CPython 3.8

immutables-0.15-cp38-cp38-macosx_10_14_x86_64.whl (53.9 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

immutables-0.15-cp37-cp37m-win_amd64.whl (56.0 kB view details)

Uploaded CPython 3.7m Windows x86-64

immutables-0.15-cp37-cp37m-manylinux1_x86_64.whl (101.6 kB view details)

Uploaded CPython 3.7m

immutables-0.15-cp37-cp37m-macosx_10_14_x86_64.whl (53.9 kB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

immutables-0.15-cp36-cp36m-win_amd64.whl (56.0 kB view details)

Uploaded CPython 3.6m Windows x86-64

immutables-0.15-cp36-cp36m-manylinux1_x86_64.whl (100.6 kB view details)

Uploaded CPython 3.6m

immutables-0.15-cp36-cp36m-macosx_10_14_x86_64.whl (53.9 kB view details)

Uploaded CPython 3.6m macOS 10.14+ x86-64

immutables-0.15-cp35-cp35m-manylinux1_x86_64.whl (100.5 kB view details)

Uploaded CPython 3.5m

immutables-0.15-cp35-cp35m-macosx_10_14_x86_64.whl (53.9 kB view details)

Uploaded CPython 3.5m macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: immutables-0.15.tar.gz
  • Upload date:
  • Size: 44.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15.tar.gz
Algorithm Hash digest
SHA256 3713ab1ebbb6946b7ce1387bb9d1d7f5e09c45add58c2a2ee65f963c171e746b
MD5 64628c8d813a4b3e954ef2af531eddc6
BLAKE2b-256 d5331187e0fcc0a521a72234b011e06cff99f8a204e1125ea791c190bd780de7

See more details on using hashes here.

File details

Details for the file immutables-0.15-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: immutables-0.15-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 55.9 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 2283a93c151566e6830aee0e5bee55fc273455503b43aa004356b50f9182092b
MD5 fe4139291f52363f780ce16f19d10c28
BLAKE2b-256 8725037f3b10e392d7b14da307d28b718476a2d02d6ebf6c357d0b0b839689f1

See more details on using hashes here.

File details

Details for the file immutables-0.15-cp39-cp39-manylinux1_x86_64.whl.

File metadata

  • Download URL: immutables-0.15-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 102.7 kB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3b15c08c71c59e5b7c2470ef949d49ff9f4263bb77f488422eaa157da84d6999
MD5 37ebd5566cf8efd41d9d3181e52a1913
BLAKE2b-256 a52f6a4d19a4cf91fe35b45a95394868f0076c702d03c14f8b9a8c9b125ee43a

See more details on using hashes here.

File details

Details for the file immutables-0.15-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: immutables-0.15-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 53.9 kB
  • Tags: CPython 3.9, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 a0a4e4417d5ef4812d7f99470cd39347b58cb927365dd2b8da9161040d260db0
MD5 b5a5997cbbe24338e34b948b3043b5fa
BLAKE2b-256 07c259cbda9df8c32e3d7f94a37877d33408df872158a4e87fbd81691ae74913

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 56.0 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 cbe8c64640637faa5535d539421b293327f119c31507c33ca880bd4f16035eb6
MD5 96088724af0c76a5b1883902954c93af
BLAKE2b-256 a30e5281336e67c0c4a556a87b26f5389a76eabd8213debac4aef9024d2fde0d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 105.2 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 141c2e9ea515a3a815007a429f0b47a578ebeb42c831edaec882a245a35fffca
MD5 72b87794e2901e4f067c23ce6efe43db
BLAKE2b-256 729a1b813d91638861777d4b87d0f0664094af4bf6d31c0bf71ed1373eab3f5e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 53.9 kB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b04fa69174e0c8f815f9c55f2a43fc9e5a68452fab459a08e904a74e8471639f
MD5 852271727dfd6f7d8d020c7f71b2378d
BLAKE2b-256 a1760ffd1d2c874b1edf4eb69caebc7f65d37c1a405261bffd0e35e5e9e40089

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 56.0 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 3035849accee4f4e510ed7c94366a40e0f5fef9069fbe04a35f4787b13610a4a
MD5 ff1ef509006bcf7392352db53f3f25e8
BLAKE2b-256 86ddf618e6de176586dabcffa6e41df2db761d6c086d07f54d13bc5d3e2b5f9c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 101.6 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b7e13c061785e34f73c4f659861f1b3e4a5fd918e4395c84b21c4e3d449ebe27
MD5 06d16fefcc6939230aa85f52aec7707c
BLAKE2b-256 ab46d2feb89eb17c981208037cfeeadb854ce066a5a23d0aba616afd9dfa0c05

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 53.9 kB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b75ade826920c4e490b1bb14cf967ac14e61eb7c5562161c5d7337d61962c226
MD5 9b2f5b645ec49c8bf3aed8cb39d2bac0
BLAKE2b-256 551afdd45a7f06a3baa1125c2774a284c04772d3d03ced5b45c9bd3a5947d445

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 56.0 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 6f117d9206165b9dab8fd81c5129db757d1a044953f438654236ed9a7a4224ae
MD5 1f30c509631f9725edf8fd83550a3d1c
BLAKE2b-256 e7383d27860c43cf8ee8a395158dae06c3c1f45a19b4bce7f0cd360eff029c28

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 100.6 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b8ad986f9b532c026f19585289384b0769188fcb68b37c7f0bd0df9092a6ca54
MD5 4c3ae7b126fd6597622750afa29eb1f9
BLAKE2b-256 4a52e64a14a99c509cbdfe0405e9f076aef0331cb9548a3efa1d5bacd524978a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp36-cp36m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 53.9 kB
  • Tags: CPython 3.6m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 8703d8abfd8687932f2a05f38e7de270c3a6ca3bd1c1efb3c938656b3f2f985a
MD5 a101ae8fa0a9b46af2e4a563a61c9642
BLAKE2b-256 d788487f1661c6556d385ec1d5d42964e33e736c7a624fda5237407bdf456a40

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 100.5 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f0836cd3bdc37c8a77b192bbe5f41dbcc3ce654db048ebbba89bdfe6db7a1c7a
MD5 165912a056b85aec56f2f0671ab330f4
BLAKE2b-256 d8b897c6e23aa267287aa382dd6fe4358550a8b6f38bcc48bcde7d82e300d5d1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: immutables-0.15-cp35-cp35m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 53.9 kB
  • Tags: CPython 3.5m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for immutables-0.15-cp35-cp35m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 6728f4392e3e8e64b593a5a0cd910a1278f07f879795517e09f308daed138631
MD5 4d3f7d6aecd11b1a7edd91173927b378
BLAKE2b-256 c7ac162c7556ec00b777458b3d93d2d03a3fd0971911ceda8a3c3fda5e3233d6

See more details on using hashes here.

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