|
1 | 1 | immutables |
2 | 2 | ========== |
3 | 3 |
|
| 4 | +.. image:: https://travis-ci.org/MagicStack/immutables.svg?branch=master |
| 5 | + :target: https://travis-ci.org/MagicStack/immutables |
| 6 | + |
| 7 | +.. image:: https://ci.appveyor.com/api/projects/status/tgbc6tq56u63qqhf?svg=true |
| 8 | + :target: https://ci.appveyor.com/project/MagicStack/immutables |
| 9 | + |
4 | 10 | An immutable mapping type for Python. |
5 | 11 |
|
6 | 12 | The underlying datastructure is a Hash Array Mapped Trie (HAMT) |
7 | | -used in Clojure and other functional languages. The actual |
8 | | -implementation is used in CPython 3.7 (see PEP 567 and |
9 | | -the contextvars module.) |
| 13 | +used in Clojure, Scala, Haskell, and other functional languages. |
| 14 | +This implementation is used in CPython 3.7 in the ``contextvars`` |
| 15 | +module (see PEP 550 and PEP 567 for more details). |
| 16 | + |
| 17 | +Immutable mappings based on HAMT have O(log\ :sub:`32`\ N) |
| 18 | +performance for both ``set()`` and ``get()`` operations, which is |
| 19 | +essentially O(1) for relatively small mappings. |
| 20 | + |
| 21 | +Below is a visualization of a simple get/set benchmark comparing |
| 22 | +HAMT to an immutable mapping implemented with a Python dict |
| 23 | +copy-on-write approach (the benchmark code is available |
| 24 | +`here <https://gist.github.com/1st1/9004813d5576c96529527d44c5457dcd>`_): |
| 25 | + |
| 26 | +.. image:: bench.png |
| 27 | + |
| 28 | + |
| 29 | +Installation |
| 30 | +------------ |
| 31 | + |
| 32 | +``immutables`` requires Python 3.5+ and is available on PyPI:: |
| 33 | + |
| 34 | + $ pip install immutables |
| 35 | + |
| 36 | + |
| 37 | +immutables.Map |
| 38 | +-------------- |
| 39 | + |
| 40 | +The ``Map`` object implements ``collections.abc.Mapping`` ABC |
| 41 | +so working with it is very similar to working with Python dicts. |
| 42 | + |
| 43 | +The only exception is its ``Map.set()`` and ``Map.delete()`` methods |
| 44 | +which return a new instance of ``Map``: |
| 45 | + |
| 46 | +.. code-block:: python |
| 47 | +
|
| 48 | + m1 = Map() # an empty Map |
| 49 | + m2 = m1.set('key1', 'val1') # m2 has a 'key1' key, m1 is still empty |
| 50 | +
|
| 51 | + m3 = m2.set('key2', 'val2') |
| 52 | + m3 = m3.delete('key1') # m3 has only a 'key2' key |
| 53 | +
|
| 54 | +
|
| 55 | +Further development |
| 56 | +------------------- |
| 57 | + |
| 58 | +* An immutable version of Python ``set`` type with efficient |
| 59 | + ``add()`` and ``discard()`` operations. |
| 60 | + |
| 61 | +* Add support for efficient ``Map.update()`` operation, allow to |
| 62 | + pass a set of key/values to ``Map()``, and add support for |
| 63 | + pickling. |
10 | 64 |
|
11 | 65 |
|
12 | 66 | License |
13 | | -======= |
| 67 | +------- |
14 | 68 |
|
15 | 69 | Apache 2.0 |
0 commit comments