Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
.. 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
immutables
requires Python 3.6+ and is available on PyPI::
$ pip install immutables
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'})>
set
type with efficient
add()
and discard()
operations.Apache 2.0
FAQs
Immutable Collections
We found that immutables demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.