美文网首页
python之munch使用

python之munch使用

作者: bonnie_xing | 来源:发表于2019-07-26 11:22 被阅读0次

    一、包安装

    1. 检查包是否已经安装

    [bonnie@gzqc249-null-8-nick ~]$ python
    Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import munch
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named munch
    >>> 
    

    说明munch包未安装

    2. 安装munch包

    [root@VM_249_182_centos ~]# pip install munch
    Looking in indexes: http://mirrors.tencentyun.com/pypi/simple
    Collecting munch
      Downloading http://mirrors.tencentyun.com/pypi/packages/68/f4/260ec98ea840757a0da09e0ed8135333d59b8dfebe9752a365b04857660a/munch-2.3.2.tar.gz
    Requirement already satisfied: six in ./.pyenv/versions/3.7.3/lib/python3.7/site-packages (from munch) (1.12.0)
    Installing collected packages: munch
      Running setup.py install for munch ... done
    Successfully installed munch-2.3.2
    

    3. 验证安装结果

    [root@VM_249_182_centos ~]# python
    Python 3.7.3 (default, Apr 10 2019, 16:42:27) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from munch import *
    >>> b = Munch()
    

    二、方法使用

    暂时增加初步使用的日志。
    对于munch的使用和性能分析,可参考https://zhuanlan.zhihu.com/p/36210263

    >>> from munch import *
    >>> b = Munch()
    >>> b.hellow='world'
    >>> b['hellow']
    'world'
    >>> 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
    >>> b
    Munch({'hellow': 'world', 'hello': 'world!', 'foo': Munch({'lol': True})})
    >>> b.keys()
    dict_keys(['hellow', 'hello', 'foo'])
    >>> b.update({'ponies':'are pretty!'}, hello = 43})
      File "<stdin>", line 1
        b.update({'ponies':'are pretty!'}, hello = 43})
                                                     ^
    SyntaxError: invalid syntax
    >>> b.update({'ponies':'are pretty!'}, hello = 43)
    >>> b
    Munch({'hellow': 'world', 'hello': 43, 'foo': Munch({'lol': True}), 'ponies': 'are pretty!'})
    >>> [(k, b[k]) for k in b]
    [('hellow', 'world'), ('hello', 43), ('foo', Munch({'lol': True})), ('ponies', 'are pretty!')]
    >>> import json
    >>> json.dumps(b)
    '{"hellow": "world", "hello": 43, "foo": {"lol": true}, "ponies": "are pretty!"}'
    >>> undefined = object()
    >>> b = DefaultMunch(undefined, {'hello':'world'})
    >>> b
    DefaultMunch(<object object at 0x7fbe44a141e0>, {'hello': 'world'})
    >>> b()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'DefaultMunch' object is not callable
    >>> b.hello
    'world'
    >>> b.foo
    <object object at 0x7fbe44a141e0>
    >>> b.foo.lol
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'object' object has no attribute 'lol'
    >>> b.foo()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'object' object is not callable
    >>> b.foo is undefined
    True
    >>> undefined = object()
    >>> b = DefaultMunch.fromDict( { 'recursively':{'nested':'value'} }, undefined )
    >>> b.recursively
    DefaultMunch(<object object at 0x7fbe44a141d0>, {'nested': 'value'})
    >>> b.recursively.nested
    'value'
    >>> b.recursively.foo
    <object object at 0x7fbe44a141d0>
    >>> import munch
    >>> b=Munch
    

    三、参考连接

    https://www.cnblogs.com/bawu/p/8127351.html

    相关文章

      网友评论

          本文标题:python之munch使用

          本文链接:https://www.haomeiwen.com/subject/ntjrrctx.html