munch


Namemunch JSON
Version 4.0.0 PyPI version JSON
download
home_pagehttps://github.com/Infinidat/munch
SummaryA dot-accessible dictionary (a la JavaScript objects)
upload_time2023-07-01 09:49:35
maintainer
docs_urlNone
authorRotem Yaari
requires_python>=3.6
licenseMIT
keywords munch dict mapping container collection
VCS
bugtrack_url
requirements importlib_metadata
Travis-CI
coveralls test coverage No coveralls.
            [![Latest Version](https://img.shields.io/pypi/v/munch.svg)](https://pypi.python.org/pypi/munch/)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/munch.svg)](https://pypi.python.org/pypi/munch/)
[![Downloads](https://img.shields.io/pypi/dm/munch.svg)](https://pypi.python.org/pypi/munch/)

munch
==========

Installation
-------------

```
pip install munch
```

Usage
-----

munch is a fork of David Schoonover's **Bunch** package, providing similar functionality. 99% of the work was done by him, and the fork was made mainly for lack of responsiveness for fixes and maintenance on the original code.

Munch is a dictionary that supports attribute-style access, a la JavaScript:

```python

>>> from munch import Munch
>>> b = Munch()
>>> b.hello = 'world'
>>> b.hello
'world'
>>> b['hello'] += "!"
>>> b.hello
'world!'
>>> b.foo = Munch(lol=True)
>>> b.foo.lol
True
>>> b.foo is b['foo']
True

```


Dictionary Methods
------------------

A Munch is a subclass of ``dict``; it supports all the methods a ``dict`` does:

```python

>>> list(b.keys())
['hello', 'foo']

```

Including ``update()``:

```python

>>> b.update({ 'ponies': 'are pretty!' }, hello=42)
>>> print(repr(b))
Munch({'hello': 42, 'foo': Munch({'lol': True}), 'ponies': 'are pretty!'})

```

As well as iteration:

```python

>>> [ (k,b[k]) for k in b ]
[('hello', 42), ('foo', Munch({'lol': True})), ('ponies', 'are pretty!')]

```

And "splats":

```python

>>> "The {knights} who say {ni}!".format(**Munch(knights='lolcats', ni='can haz'))
'The lolcats who say can haz!'

```


Serialization
-------------

Munches happily and transparently serialize to JSON and YAML.

```python

>>> b = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!')
>>> import json
>>> json.dumps(b)
'{"foo": {"lol": true}, "hello": 42, "ponies": "are pretty!"}'

```

If JSON support is present (``json`` or ``simplejson``), ``Munch`` will have a ``toJSON()`` method which returns the object as a JSON string.

If you have [PyYAML](http://pyyaml.org/wiki/PyYAML) installed, Munch attempts to register itself with the various YAML Representers so that Munches can be transparently dumped and loaded.

```python

>>> b = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!')
>>> import yaml
>>> yaml.dump(b)
'!munch.Munch\nfoo: !munch.Munch\n  lol: true\nhello: 42\nponies: are pretty!\n'
>>> yaml.safe_dump(b)
'foo:\n  lol: true\nhello: 42\nponies: are pretty!\n'

```

In addition, Munch instances will have a ``toYAML()`` method that returns the YAML string using ``yaml.safe_dump()``. This method also replaces ``__str__`` if present, as I find it far more readable. You can revert back to Python's default use of ``__repr__`` with a simple assignment: ``Munch.__str__ = Munch.__repr__``. The Munch class will also have a static method ``Munch.fromYAML()``, which loads a Munch out of a YAML string.

Finally, Munch converts easily and recursively to (``unmunchify()``, ``Munch.toDict()``) and from (``munchify()``, ``Munch.fromDict()``) a normal ``dict``, making it easy to cleanly serialize them in other formats.


Default Values
--------------

``DefaultMunch`` instances return a specific default value when an attribute is missing from the collection. Like ``collections.defaultdict``, the first argument is the value to use for missing keys:

```python

>>> from munch import DefaultMunch
>>> undefined = object()
>>> b = DefaultMunch(undefined, {'hello': 'world!'})
>>> b.hello
'world!'
>>> b.foo is undefined
True

```

``DefaultMunch.fromDict()`` also takes the ``default`` argument:

```python

>>> undefined = object()
>>> b = DefaultMunch.fromDict({'recursively': {'nested': 'value'}}, undefined)
>>> b.recursively.nested == 'value'
True
>>> b.recursively.foo is undefined
True

```

Or you can use ``DefaultFactoryMunch`` to specify a factory for generating missing attributes. The first argument is the factory:

```python

>>> from munch import DefaultFactoryMunch
>>> b = DefaultFactoryMunch(list, {'hello': 'world!'})
>>> b.hello
'world!'
>>> b.foo
[]
>>> b.bar.append('hello')
>>> b.bar
['hello']

```


Miscellaneous
-------------

* It is safe to ``import *`` from this module. You'll get: ``Munch``, ``DefaultMunch``, ``DefaultFactoryMunch``, ``munchify`` and ``unmunchify``.
* Ample Tests. Just run ``pip install tox && tox`` from the project root.

Feedback
--------

Open a ticket / fork the project on [GitHub](http://github.com/Infinidat/munch).


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Infinidat/munch",
    "name": "munch",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "munch,dict,mapping,container,collection",
    "author": "Rotem Yaari",
    "author_email": "vmalloc@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e7/2b/45098135b5f9f13221820d90f9e0516e11a2a0f55012c13b081d202b782a/munch-4.0.0.tar.gz",
    "platform": null,
    "description": "[![Latest Version](https://img.shields.io/pypi/v/munch.svg)](https://pypi.python.org/pypi/munch/)\n[![Supported Python versions](https://img.shields.io/pypi/pyversions/munch.svg)](https://pypi.python.org/pypi/munch/)\n[![Downloads](https://img.shields.io/pypi/dm/munch.svg)](https://pypi.python.org/pypi/munch/)\n\nmunch\n==========\n\nInstallation\n-------------\n\n```\npip install munch\n```\n\nUsage\n-----\n\nmunch is a fork of David Schoonover's **Bunch** package, providing similar functionality. 99% of the work was done by him, and the fork was made mainly for lack of responsiveness for fixes and maintenance on the original code.\n\nMunch is a dictionary that supports attribute-style access, a la JavaScript:\n\n```python\n\n>>> from munch import Munch\n>>> b = Munch()\n>>> b.hello = 'world'\n>>> b.hello\n'world'\n>>> b['hello'] += \"!\"\n>>> b.hello\n'world!'\n>>> b.foo = Munch(lol=True)\n>>> b.foo.lol\nTrue\n>>> b.foo is b['foo']\nTrue\n\n```\n\n\nDictionary Methods\n------------------\n\nA Munch is a subclass of ``dict``; it supports all the methods a ``dict`` does:\n\n```python\n\n>>> list(b.keys())\n['hello', 'foo']\n\n```\n\nIncluding ``update()``:\n\n```python\n\n>>> b.update({ 'ponies': 'are pretty!' }, hello=42)\n>>> print(repr(b))\nMunch({'hello': 42, 'foo': Munch({'lol': True}), 'ponies': 'are pretty!'})\n\n```\n\nAs well as iteration:\n\n```python\n\n>>> [ (k,b[k]) for k in b ]\n[('hello', 42), ('foo', Munch({'lol': True})), ('ponies', 'are pretty!')]\n\n```\n\nAnd \"splats\":\n\n```python\n\n>>> \"The {knights} who say {ni}!\".format(**Munch(knights='lolcats', ni='can haz'))\n'The lolcats who say can haz!'\n\n```\n\n\nSerialization\n-------------\n\nMunches happily and transparently serialize to JSON and YAML.\n\n```python\n\n>>> b = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!')\n>>> import json\n>>> json.dumps(b)\n'{\"foo\": {\"lol\": true}, \"hello\": 42, \"ponies\": \"are pretty!\"}'\n\n```\n\nIf JSON support is present (``json`` or ``simplejson``), ``Munch`` will have a ``toJSON()`` method which returns the object as a JSON string.\n\nIf you have [PyYAML](http://pyyaml.org/wiki/PyYAML) installed, Munch attempts to register itself with the various YAML Representers so that Munches can be transparently dumped and loaded.\n\n```python\n\n>>> b = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!')\n>>> import yaml\n>>> yaml.dump(b)\n'!munch.Munch\\nfoo: !munch.Munch\\n  lol: true\\nhello: 42\\nponies: are pretty!\\n'\n>>> yaml.safe_dump(b)\n'foo:\\n  lol: true\\nhello: 42\\nponies: are pretty!\\n'\n\n```\n\nIn addition, Munch instances will have a ``toYAML()`` method that returns the YAML string using ``yaml.safe_dump()``. This method also replaces ``__str__`` if present, as I find it far more readable. You can revert back to Python's default use of ``__repr__`` with a simple assignment: ``Munch.__str__ = Munch.__repr__``. The Munch class will also have a static method ``Munch.fromYAML()``, which loads a Munch out of a YAML string.\n\nFinally, Munch converts easily and recursively to (``unmunchify()``, ``Munch.toDict()``) and from (``munchify()``, ``Munch.fromDict()``) a normal ``dict``, making it easy to cleanly serialize them in other formats.\n\n\nDefault Values\n--------------\n\n``DefaultMunch`` instances return a specific default value when an attribute is missing from the collection. Like ``collections.defaultdict``, the first argument is the value to use for missing keys:\n\n```python\n\n>>> from munch import DefaultMunch\n>>> undefined = object()\n>>> b = DefaultMunch(undefined, {'hello': 'world!'})\n>>> b.hello\n'world!'\n>>> b.foo is undefined\nTrue\n\n```\n\n``DefaultMunch.fromDict()`` also takes the ``default`` argument:\n\n```python\n\n>>> undefined = object()\n>>> b = DefaultMunch.fromDict({'recursively': {'nested': 'value'}}, undefined)\n>>> b.recursively.nested == 'value'\nTrue\n>>> b.recursively.foo is undefined\nTrue\n\n```\n\nOr you can use ``DefaultFactoryMunch`` to specify a factory for generating missing attributes. The first argument is the factory:\n\n```python\n\n>>> from munch import DefaultFactoryMunch\n>>> b = DefaultFactoryMunch(list, {'hello': 'world!'})\n>>> b.hello\n'world!'\n>>> b.foo\n[]\n>>> b.bar.append('hello')\n>>> b.bar\n['hello']\n\n```\n\n\nMiscellaneous\n-------------\n\n* It is safe to ``import *`` from this module. You'll get: ``Munch``, ``DefaultMunch``, ``DefaultFactoryMunch``, ``munchify`` and ``unmunchify``.\n* Ample Tests. Just run ``pip install tox && tox`` from the project root.\n\nFeedback\n--------\n\nOpen a ticket / fork the project on [GitHub](http://github.com/Infinidat/munch).\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A dot-accessible dictionary (a la JavaScript objects)",
    "version": "4.0.0",
    "project_urls": {
        "Homepage": "https://github.com/Infinidat/munch"
    },
    "split_keywords": [
        "munch",
        "dict",
        "mapping",
        "container",
        "collection"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "56b37c69b37f03260a061883bec0e7b05be7117c1b1c85f5212c72c8c2bc3c8c",
                "md5": "0b5887c3f1571ecf550aa3e9bbf24e90",
                "sha256": "71033c45db9fb677a0b7eb517a4ce70ae09258490e419b0e7f00d1e386ecb1b4"
            },
            "downloads": -1,
            "filename": "munch-4.0.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0b5887c3f1571ecf550aa3e9bbf24e90",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.6",
            "size": 9950,
            "upload_time": "2023-07-01T09:49:34",
            "upload_time_iso_8601": "2023-07-01T09:49:34.472765Z",
            "url": "https://files.pythonhosted.org/packages/56/b3/7c69b37f03260a061883bec0e7b05be7117c1b1c85f5212c72c8c2bc3c8c/munch-4.0.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e72b45098135b5f9f13221820d90f9e0516e11a2a0f55012c13b081d202b782a",
                "md5": "4e70cf760e3b81dcaa6050803c1dbd72",
                "sha256": "542cb151461263216a4e37c3fd9afc425feeaf38aaa3025cd2a981fadb422235"
            },
            "downloads": -1,
            "filename": "munch-4.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4e70cf760e3b81dcaa6050803c1dbd72",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 19089,
            "upload_time": "2023-07-01T09:49:35",
            "upload_time_iso_8601": "2023-07-01T09:49:35.980393Z",
            "url": "https://files.pythonhosted.org/packages/e7/2b/45098135b5f9f13221820d90f9e0516e11a2a0f55012c13b081d202b782a/munch-4.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-01 09:49:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Infinidat",
    "github_project": "munch",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "importlib_metadata",
            "specs": [
                [
                    ">=",
                    "1.7.0"
                ]
            ]
        }
    ],
    "tox": true,
    "lcname": "munch"
}
        
Elapsed time: 0.09088s