Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/csernazs/cowdict
Copy-on-write dictionary
https://github.com/csernazs/cowdict
library python3
Last synced: 30 days ago
JSON representation
Copy-on-write dictionary
- Host: GitHub
- URL: https://github.com/csernazs/cowdict
- Owner: csernazs
- License: mit
- Created: 2017-12-31T15:20:53.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-06T10:53:26.000Z (almost 7 years ago)
- Last Synced: 2024-08-09T13:15:03.364Z (4 months ago)
- Topics: library, python3
- Language: Python
- Size: 9.77 KB
- Stars: 4
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
cowdict
~~~~~~~cowdict is a python module implementing copy-on-write pattern on dictionaries.
This means that if someone changes the dict-like object, the original object won't be changed but
the differences will be tracked instead.Nutshell
--------Here's a small example:
.. code-block:: python
base_dict = {"foo": "bar"}
cd = CowDict(base_dict)
cd["foo"] = "baz"print(base_dict["foo"]) # will still print 'bar'
print(cd["foo"]) # will print 'baz'CowDict object (`cd` in the code above) implements the `MutableMapping` interface, simply speaking it has the
same interface as the `dict` object.This means that it is guaranteed that changing keys/values on this object will never cause the change of the
underlying dictionary (`base_dict` in the example).Philosophy
----------
The idea behind this module is to avoid copying dictionary where it is possible and use this wrapper class instead.
While it has some penalties on the performance on key look-up and length calculation, the memory footprint can
be kept at minimum (compared to a full copy of the dictionary).Behind the scenes
-----------------
Behind the scenes, new items are added to a separate directory. Keys which exist on the base dictionary are
'deleted' by keeping a separate set object about the the keys deleted.
Every time when a key is accessed either by `__getitem__` or `items` or other methods,
these additional structures are involved to produce the correct result.While having an assumption of having the base dictionary read-only would make the world easier, especially
when calculating the length of the object, the library handles the situation when the base dictionary changes.