sample_python_munch.py 644 B

1234567891011121314151617181920
  1. from munch import Munch
  2. b = Munch()
  3. b.hello = 'world'
  4. assert b.hello == 'world'
  5. b['hello'] += "!"
  6. assert b.hello == 'world!'
  7. b.foo = Munch(lol=True)
  8. assert b.foo.lol is True
  9. assert b.foo is b['foo']
  10. assert sorted(b.keys()) == ['foo', 'hello']
  11. b.update({'ponies': 'are pretty!'}, hello=42)
  12. assert b == Munch({'ponies': 'are pretty!', 'foo': Munch({'lol': True}), 'hello': 42})
  13. assert sorted([(k, b[k]) for k in b]) == [('foo', Munch({'lol': True})), ('hello', 42), ('ponies', 'are pretty!')]
  14. format_munch = Munch(knights='lolcats', ni='can haz')
  15. assert "The {knights} who say {ni}!".format(**format_munch) == 'The lolcats who say can haz!'