All Projects → MagicStack → Immutables

MagicStack / Immutables

Licence: apache-2.0
A high-performance immutable mapping type for Python.

Programming Languages

python
139335 projects - #7 most used programming language
c
50402 projects - #5 most used programming language
python3
1442 projects

Labels

Projects that are alternatives of or similar to Immutables

Qframe
Immutable data frame for Go
Stars: ✭ 282 (-65.74%)
Mutual labels:  immutable
Immutable
Immutable collections for Go
Stars: ✭ 392 (-52.37%)
Mutual labels:  immutable
Kotlinx.collections.immutable
Immutable persistent collections for Kotlin
Stars: ✭ 465 (-43.5%)
Mutual labels:  immutable
Observable Membrane
A Javascript Membrane implementation using Proxies to observe mutation on an object graph
Stars: ✭ 315 (-61.73%)
Mutual labels:  immutable
Capsule
The Capsule Hash Trie Collections Library
Stars: ✭ 350 (-57.47%)
Mutual labels:  immutable
Ewig
The eternal text editor — Didactic Ersatz Emacs to show immutable data-structures and the single-atom architecture
Stars: ✭ 422 (-48.72%)
Mutual labels:  immutable
Halt
OS where everything is immutable! (Experimental)
Stars: ✭ 265 (-67.8%)
Mutual labels:  immutable
Rust Ipfs
The InterPlanetary File System (IPFS), implemented in Rust.
Stars: ✭ 739 (-10.21%)
Mutual labels:  immutable
Immer
Create the next immutable state by mutating the current one
Stars: ✭ 21,756 (+2543.5%)
Mutual labels:  immutable
Koazee
A StreamLike, Immutable, Lazy Loading and smart Golang Library to deal with slices.
Stars: ✭ 446 (-45.81%)
Mutual labels:  immutable
React Antd
基于react + redux + immutable + less + ES6/7 + webpack2.0 + fetch + react-router + antd实现的SPA后台管理系统模板
Stars: ✭ 321 (-61%)
Mutual labels:  immutable
Data Structures
Go datastructures.
Stars: ✭ 336 (-59.17%)
Mutual labels:  immutable
Dinero.js
Create, calculate, and format money in JavaScript and TypeScript.
Stars: ✭ 5,286 (+542.28%)
Mutual labels:  immutable
Polychrome
🎨 Easy color manipulation in ~2kb (gzipped)
Stars: ✭ 286 (-65.25%)
Mutual labels:  immutable
Pepperoni App Kit
Pepperoni - React Native App Starter Kit for Android and iOS
Stars: ✭ 4,657 (+465.86%)
Mutual labels:  immutable
Fpp
Functional PHP Preprocessor - Generate Immutable Data Types
Stars: ✭ 282 (-65.74%)
Mutual labels:  immutable
Mail
Library to send e-mails over different transports and protocols (like SMTP and IMAP) using immutable messages and streams. Also includes SMTP server.
Stars: ✭ 399 (-51.52%)
Mutual labels:  immutable
React Tetris
Use React, Redux, Immutable to code Tetris. 🎮
Stars: ✭ 6,770 (+722.6%)
Mutual labels:  immutable
Js Ipfs
IPFS implementation in JavaScript
Stars: ✭ 6,129 (+644.71%)
Mutual labels:  immutable
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+449.82%)
Mutual labels:  immutable

immutables

.. image:: https://github.com/MagicStack/immutables/workflows/Tests/badge.svg?branch=master :target: https://github.com/MagicStack/immutables/actions?query=workflow%3ATests+branch%3Amaster+event%3Apush

.. image:: https://img.shields.io/pypi/v/immutables.svg :target: https://pypi.python.org/pypi/immutables

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 <https://www.python.org/dev/peps/pep-0550/>_ and PEP 567 <https://www.python.org/dev/peps/pep-0567/>_ 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 <https://gist.github.com/1st1/292e3f0bbe43bd65ff3256f80aa2637d>_):

.. image:: 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:

.. code-block:: python

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:

.. code-block:: python

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:

.. code-block:: python

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:

.. code-block:: python

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].