Skip to main content

frozndict is a memory efficient immutable dictionary.

Project description

Coverage Report License pypi version Circle ci Build Status Repo Size Banner

frozndict is a python package that acts as an alternative to frozenset, but for dictionaries.

🛠️ Requirements

frozndict requires Python 3.9 or above.

To install Python 3.9, I recommend using pyenv.

# install pyenv
git clone https://github.com/pyenv/pyenv ~/.pyenv

# setup pyenv (you should also put these three lines in .bashrc or similar)
# if you are using zsh
cat << EOF >> ~/.zshrc
# pyenv config
export PATH="${HOME}/.pyenv/bin:${PATH}"
export PYENV_ROOT="${HOME}/.pyenv"
eval "$(pyenv init -)"
EOF

# or if you using the default bash shell, do this instead:
cat << EOF >> ~/.bashrc
# pyenv config
export PATH="${HOME}/.pyenv/bin:${PATH}"
export PYENV_ROOT="${HOME}/.pyenv"
eval "$(pyenv init -)"
EOF
# Close and open a new shell session
# install Python 3.9.10
pyenv install 3.9.10

# make it available globally
pyenv global system 3.9.10

To manage the Python 3.9 virtualenv, I recommend using poetry.

# install poetry
curl -sSL https://install.python-poetry.org | python3 -
poetry --version
Poetry version 1.1.13

# Having the python executable in your PATH, you can use it:
poetry env use 3.9.10

# However, you are most likely to get the following issue:
Creating virtualenv frozndict-dxc671ba-py3.9 in ~/.cache/pypoetry/virtualenvs

ModuleNotFoundError

No module named 'virtualenv.seed.via_app_data'

at <frozen importlib._bootstrap>:973 in _find_and_load_unlocked

# To resolve it, you need to reinstall virtualenv through pip
sudo apt remove --purge python3-virtualenv virtualenv
python3 -m pip install -U virtualenv

# Now, you can just use the minor Python version in this case:
poetry env use 3.9.10
Using virtualenv: ~/.cache/pypoetry/virtualenvs/frozndict-dxc671ba-py3.9

🚨 Installation

With pip:

python3.9 -m pip install frozndict

With pipx:

python3.9 -m pip install --user pipx
pipx install --python python3.9 frozndict

🚸 Usage

>>> from frozndict import frozendict

# Empty immutable immutable dictionary.
>>> frozen_dict = frozendict({})
frozendict({})

# Non empty immutable immutable dictionary.
>>> frozen_dict = frozendict({"Greetings": "Hello World!"})
>>> frozen_dict
frozendict({'Greetings': 'Hello World!'})

# Get an item.
>>> frozen_dict["Greetings"]
'Hello World!'

# Copy a dictionary.
>>> frozen_dict_copy = frozen_dict.copy()
>>> frozen_dict_copy
{'Greetings': 'Hello World!'}

# Nested dictionary.
>>> frozen_dict_copy = frozendict({'x': 3, 'y': 4, 'z': {'a': 0, 'b': [3,1,{4,1},[5,9]]}}, c= 1)
>>> print(a.pretty_repr())
frozendict({
    x: 3,
    y: 4,
    z: {
        a: 0,
        b: [3, 1, {1, 4}, [5, 9]],
    },
    c: 1,
})

# Create an immutable dictionary using `fromkeys` method.
>>> frozen_dict = frozendict.fromkeys(["x", "y"], "5")
>>> frozen_dict
frozendict({'x': '5', 'y': '5'})

# Test uniqueness: frozendict(a=1,b=2) == frozendict(b=2,a=1)
>>> set([frozendict(a=1,b=2), frozendict(a=5), frozendict(b=2,a=1)])
{frozendict({'a': 5}), frozendict({'a': 1, 'b': 2})}

🚀 Similar Projects Comparaison

This project is similar to frozendict created by Marco Sulla.

>>> from frozndict import frozendict as myfrozendict
>>> from frozendict import frozendict

# create instances
>>> my_frozen_dict = myfrozendict({'x': 3, 'y': 4, 'z': {'a': 0, 'b': [3,1,{4,1},[5,9]]}}, c= 1)
>>> frozen_dict = frozendict({'x': 3, 'y': 4, 'z': {'a': 0, 'b': [3,1,{4,1},[5,9]]}}, c= 1)
>>> dict = dict({'x': 3, 'y': 4, 'z': {'a': 0, 'b': [3,1,{4,1},[5,9]]}}, c= 1)

# comparaison
>>> import sys
>>> tuple(map(sys.getsizeof, [frozen_dict, my_frozen_dict, dict]))
(248, 240, 232)

Notice my_frozen_dict takes less space in memory than frozen_dict!

🎉 Credits

These following projects were used to build and test frozndict.

👋 Contribute

If you are looking for a way to contribute to the project, please refer to the Guideline.

📝 License

This program and the accompanying materials are made available under the terms and conditions of the GNU GENERAL PUBLIC LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

frozndict-1.0.5.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

frozndict-1.0.5-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file frozndict-1.0.5.tar.gz.

File metadata

  • Download URL: frozndict-1.0.5.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.10 Linux/5.13.0-1017-aws

File hashes

Hashes for frozndict-1.0.5.tar.gz
Algorithm Hash digest
SHA256 94383af49705cdb57839a0074d7082bab8e04a7ee720a10227f495ebc6c07c20
MD5 668d8012046f1a7f05e4b63e77074485
BLAKE2b-256 f76f4510d2eb2ff3f10eaedd97ae9c4cc529c9747a45d97d9124a70ea2f7edb9

See more details on using hashes here.

File details

Details for the file frozndict-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: frozndict-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.10 Linux/5.13.0-1017-aws

File hashes

Hashes for frozndict-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 27cd2c2c0dfa8c841a8f8536ed6ea1b2e68238fb56e53cfebd621a6bb09786ac
MD5 0a2724717ca87216a31b646d84519b21
BLAKE2b-256 e1cfcf77106699a01c63aa4a602776f48c7b5809655e5e8a1cd748485cfb4b39

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page