aboutsummaryrefslogtreecommitdiff
path: root/README.rst
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2018-11-20 13:26:15 -0500
committerYury Selivanov <yury@magic.io>2018-11-20 14:25:07 -0500
commit2ba4b491cffa5125b01ba9dae87b6c0760b68492 (patch)
tree3cbf43df25174beac5e41171c5d8cac3371e2284 /README.rst
parent2a14bc7bb3e523d74c9abe5246303976c013d91a (diff)
downloadimmutables-2ba4b491cffa5125b01ba9dae87b6c0760b68492.tar.gz
immutables-2ba4b491cffa5125b01ba9dae87b6c0760b68492.zip
Update readme
Diffstat (limited to 'README.rst')
-rw-r--r--README.rst73
1 files changed, 59 insertions, 14 deletions
diff --git a/README.rst b/README.rst
index fe5a120..4f3d789 100644
--- a/README.rst
+++ b/README.rst
@@ -37,22 +37,71 @@ Installation
$ pip install immutables
-immutables.Map
---------------
+API
+---
-The ``Map`` object implements ``collections.abc.Mapping`` ABC
+``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.
-The only exception are its ``Map.set()`` and ``Map.delete()`` methods
-which return a new instance of ``Map``:
+ 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})>
-.. code-block:: python
+ 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})>
- m1 = Map() # an empty Map
- m2 = m1.set('key1', 'val1') # m2 has a 'key1' key, m1 is still empty
+Maps also implement APIs for bulk updates: ``MapMutation`` objects::
- m3 = m2.set('key2', 'val2')
- m3 = m3.delete('key1') # m3 has only a 'key2' key
+ map_mutation = map.mutate()
+ map_mutation['a'] = 100
+ del map_mutation['b']
+ map_mutation.set('y', 'y')
+
+ map2 = map_mutation.finalize()
+
+ 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.finalize()
+
+ print(map, map2)
+ # will print:
+ # <immutables.Map({'a': 1, 'b': 2})>
+ # <immutables.Map({'a': 100, 'y': 'y'})>
Further development
@@ -61,10 +110,6 @@ Further development
* An immutable version of Python ``set`` type with efficient
``add()`` and ``discard()`` operations.
-* Add support for efficient ``Map.update()`` operation, allow to
- pass a set of key/values to ``Map()``, and add support for
- pickling.
-
License
-------