Infinidat / munch

A Munch is a Python dictionary that provides attribute-style access (a la JavaScript objects).
http://github.com/Infinidat/munch
MIT License
761 stars 84 forks source link

Immutable/frozen munch #43

Open worldmind opened 5 years ago

worldmind commented 5 years ago

Hi, I am searching a best type for config data, usually it is nested dict, but on my opinion config must:

  1. have dot notation for readability,
  2. be immutable for safe using (config is global variable). Can Munch be immutable?

P.S. I found https://github.com/cdgriffith/Box/ but not sure that it popular = good tested.

jaraco commented 3 years ago

You might also consider jaraco.collections, which adds attribute access for items on any mapping:

$ pip-run --use-pep517 jaraco.collections frozendict
Collecting frozendict
  Using cached frozendict-1.2.tar.gz (2.6 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Collecting jaraco.collections
  Using cached jaraco.collections-3.0.0-py2.py3-none-any.whl (9.4 kB)
Collecting jaraco.classes
  Using cached jaraco.classes-3.1.0-py2.py3-none-any.whl (5.7 kB)
Collecting six>=1.7.0
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting jaraco.text
  Using cached jaraco.text-3.2.0-py2.py3-none-any.whl (8.1 kB)
Collecting jaraco.functools
  Using cached jaraco.functools-3.0.1-py3-none-any.whl (6.7 kB)
Collecting more-itertools
  Using cached more_itertools-8.5.0-py3-none-any.whl (44 kB)
Building wheels for collected packages: frozendict
  Building wheel for frozendict (PEP 517) ... done
  Created wheel for frozendict: filename=frozendict-1.2-py3-none-any.whl size=3148 sha256=b127942c3dbb2d8e958462f10dbae7e2e67c45b6bd21f9d6edaa81aa76d1bd5c
  Stored in directory: /Users/jaraco/Library/Caches/pip/wheels/5b/fa/ab/0a80360debb57b95f092356ee3a075bbbffc631b9813136599
Successfully built frozendict
Installing collected packages: more-itertools, six, jaraco.functools, jaraco.text, jaraco.classes, jaraco.collections, frozendict
Successfully installed frozendict-1.2 jaraco.classes-3.1.0 jaraco.collections-3.0.0 jaraco.functools-3.0.1 jaraco.text-3.2.0 more-itertools-8.5.0 six-1.15.0
Python 3.9.0 (v3.9.0:9cf6752276, Oct  5 2020, 11:29:23) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import frozendict
>>> import jaraco.collections
>>> class FrozenDictAttrs(frozendict.frozendict, jaraco.collections.ItemsAsAttributes):
...     pass
... 
>>> fd = FrozenDictAttrs(a=1, b=2)
>>> fd.a
1
>>> fd['a'] = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'FrozenDictAttrs' object does not support item assignment