Immutable Collections
Project description
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):
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file immutables-0.11.tar.gz
.
File metadata
- Download URL: immutables-0.11.tar.gz
- Upload date:
- Size: 38.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6850578a0dc6530ac19113cfe4ddc13903df635212d498f176fe601a8a5a4a3 |
|
MD5 | 2478a4009ed371232fc9e4d00fac1159 |
|
BLAKE2b-256 | 3934d5f6797f9b835d755d047d7a592a0f2f0044f82b5c9a184a5fbe7488808f |
File details
Details for the file immutables-0.11-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: immutables-0.11-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 53.4 kB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b6d8d035e5888baad3db61dfb167476838a63afccecd927c365f228bb55754c |
|
MD5 | 2899c68510cd1e02a5c40e9856c88cce |
|
BLAKE2b-256 | c41a181fb128db26a443510c3af85c834f5e9ce583e8493b8967546c109c6c43 |
File details
Details for the file immutables-0.11-cp37-cp37m-win32.whl
.
File metadata
- Download URL: immutables-0.11-cp37-cp37m-win32.whl
- Upload date:
- Size: 48.8 kB
- Tags: CPython 3.7m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 545186faab9237c102b8bcffd36d71f0b382174c93c501e061de239753cff694 |
|
MD5 | 81ecb3675e2a8e929ec1c5257b17ec39 |
|
BLAKE2b-256 | a33ba773834eec3d14b58826fa24434052411d80355dcfc8d512cd42adce1544 |
File details
Details for the file immutables-0.11-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: immutables-0.11-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 93.7 kB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c2e729aab250be0de0c13fa833241a778b51390ee2650e0457d1e45b318c441 |
|
MD5 | 2a1d3887bd297bf84ffac7a8c5853cac |
|
BLAKE2b-256 | b2bd5d3476a4f3483b4802b48f2040e62c23e43f2315a74977769b76293f754f |
File details
Details for the file immutables-0.11-cp37-cp37m-manylinux1_i686.whl
.
File metadata
- Download URL: immutables-0.11-cp37-cp37m-manylinux1_i686.whl
- Upload date:
- Size: 89.0 kB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c536ff2bafeeff9a7865ea10a17a50f90b80b585e31396c349e8f57b0075bd4 |
|
MD5 | a9f8e6eea50f6b22fed342897aac88b7 |
|
BLAKE2b-256 | 9860d32c5e9c7edaafbaa8e105d687b16dcda1030acc97447386ae9cb16b7fd3 |
File details
Details for the file immutables-0.11-cp37-cp37m-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: immutables-0.11-cp37-cp37m-macosx_10_13_x86_64.whl
- Upload date:
- Size: 50.6 kB
- Tags: CPython 3.7m, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 518f20945c1f600b618fb691922c2ab43b193f04dd2d4d2823220d0202014670 |
|
MD5 | 9b925408d188c92c91fdaadb7c128c89 |
|
BLAKE2b-256 | 5f5b53e1b2a9329fcaa385428bf666c998686f50ff606ae5eb0270254fed2032 |
File details
Details for the file immutables-0.11-cp36-cp36m-win_amd64.whl
.
File metadata
- Download URL: immutables-0.11-cp36-cp36m-win_amd64.whl
- Upload date:
- Size: 53.4 kB
- Tags: CPython 3.6m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8fed714f1c84a3242c7184838f5e9889139a22bbdd701a182b7fdc237ca3cbb |
|
MD5 | 97132564000c046bb7589b2517ca2201 |
|
BLAKE2b-256 | 9f5051289aef23562beef9005220658725b236ff3f0fe6bfa36246fbc6a95a9a |
File details
Details for the file immutables-0.11-cp36-cp36m-win32.whl
.
File metadata
- Download URL: immutables-0.11-cp36-cp36m-win32.whl
- Upload date:
- Size: 48.8 kB
- Tags: CPython 3.6m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f07c58122e1ce70a7165e68e18e795ac5fe94d7fee3e045ffcf6432602026df |
|
MD5 | 936255e9d521bd529541c1d420bfdd60 |
|
BLAKE2b-256 | 7f915ae4e8b87d0c930512aae39bc1b41aed1c7a7ebfcf73a597b0311d1ae0b5 |
File details
Details for the file immutables-0.11-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: immutables-0.11-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 92.7 kB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b268422a5802fbf934152b835329ac0d23b80b558eaee68034d45718edab4a11 |
|
MD5 | 036ded24f2d67ac28a393045c28f2216 |
|
BLAKE2b-256 | 62cc3961b18a1a689a7e3232d923a8546cb901c49b4a33be6987c03a89175c4f |
File details
Details for the file immutables-0.11-cp36-cp36m-manylinux1_i686.whl
.
File metadata
- Download URL: immutables-0.11-cp36-cp36m-manylinux1_i686.whl
- Upload date:
- Size: 88.0 kB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5b93248552c9e7198558776da21c9157d3f70649905d7fdc083c2ab2fbc6088 |
|
MD5 | be63cc670b07ac72e743137db6a21504 |
|
BLAKE2b-256 | fdb74cfbaac47fde635b93bfb14c3232e695f9793abfc83e0a47a64be3383bc1 |
File details
Details for the file immutables-0.11-cp36-cp36m-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: immutables-0.11-cp36-cp36m-macosx_10_13_x86_64.whl
- Upload date:
- Size: 50.6 kB
- Tags: CPython 3.6m, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 422c7d4c75c88057c625e32992248329507bca180b48cfb702b4ef608f581b50 |
|
MD5 | c6e1dd0dde5c3eb40f12a3eeb4af1ec5 |
|
BLAKE2b-256 | 97904ad8d6f5c63281004a871b6fbc8ca1507f86d681b4aec41fac415da45ee6 |
File details
Details for the file immutables-0.11-cp35-cp35m-win_amd64.whl
.
File metadata
- Download URL: immutables-0.11-cp35-cp35m-win_amd64.whl
- Upload date:
- Size: 53.4 kB
- Tags: CPython 3.5m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0aa055c745510238cbad2f1f709a37a1c9e30a38594de3b385e9876c48a25633 |
|
MD5 | 2c3537a09ce0e1eab5d9ce0665c2548d |
|
BLAKE2b-256 | fdf5ee265d5976498e0505b056cdf57006b17bca36185f24f85130d18a38e0eb |
File details
Details for the file immutables-0.11-cp35-cp35m-win32.whl
.
File metadata
- Download URL: immutables-0.11-cp35-cp35m-win32.whl
- Upload date:
- Size: 48.8 kB
- Tags: CPython 3.5m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e87bd941cb4dfa35f16e1ff4b2d99a2931452dcc9cfd788dc8fe513f3d38551e |
|
MD5 | 725bdcd6558cc84780487df25c5ba583 |
|
BLAKE2b-256 | 40f57e611152c0beacbff9b48562cab2ead878977a9fee514013c63a7970b861 |
File details
Details for the file immutables-0.11-cp35-cp35m-manylinux1_x86_64.whl
.
File metadata
- Download URL: immutables-0.11-cp35-cp35m-manylinux1_x86_64.whl
- Upload date:
- Size: 92.6 kB
- Tags: CPython 3.5m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2de2ec8dde1ca154f811776a8cbbeaea515c3b226c26036eab6484530eea28e0 |
|
MD5 | 0570080f991d51a9823b0fc635937e2f |
|
BLAKE2b-256 | 481d9e457d5cccafe976e68c80b161538a8bee8ca1f407be5627877b3123a6c4 |
File details
Details for the file immutables-0.11-cp35-cp35m-manylinux1_i686.whl
.
File metadata
- Download URL: immutables-0.11-cp35-cp35m-manylinux1_i686.whl
- Upload date:
- Size: 87.9 kB
- Tags: CPython 3.5m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7eb2d15c35c73bb168c002c6ea145b65f40131e10dede54b39db0b72849b280 |
|
MD5 | 0d113d57f181b16f1d9097140353569e |
|
BLAKE2b-256 | 3c2d4b248b111e9e2d6a210a709bd09936932bb72dc35b2b9550befe7740e568 |
File details
Details for the file immutables-0.11-cp35-cp35m-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: immutables-0.11-cp35-cp35m-macosx_10_13_x86_64.whl
- Upload date:
- Size: 50.6 kB
- Tags: CPython 3.5m, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bce27277a2fe91509cca69181971ab509c2ee862e8b37b09f26b64f90e8fe8fb |
|
MD5 | 750fa5b448aab5878ed9b9177886c6e7 |
|
BLAKE2b-256 | 477412cbd7dac0e45896b1711533fc4e53728d664367eaffe159241c004e0522 |