Skip to content

Commit

Permalink
Adds support for + and += operators, merging a Bunch with a Mapping. …
Browse files Browse the repository at this point in the history
…Updates docs and tests.
  • Loading branch information
dsc committed Sep 17, 2024
1 parent c7ed586 commit 9646d9b
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 76 deletions.
23 changes: 22 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
*.pyc
tmp/

# Python
pip-log.txt
*.py[oc]
*.egg-info
*.un~
.env
.tox
.coverage
.coverage_html
docs/_build/*

# project files
.project
.settings
*.sublime-*
*.code-*

# junk
.DS_Store
Icon?
._*
*~
38 changes: 19 additions & 19 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
Copyright (c) 2010 David Schoonover
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Copyright (c) 2010-2012 David Schoonover <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include LICENSE.txt
include README.rst
include README.md
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ bunch

Bunch is a dictionary that supports attribute-style access, a la JavaScript.

````py
```py
>>> b = Bunch()
>>> b.hello = 'world'
>>> b.hello
Expand All @@ -16,66 +16,66 @@ Bunch is a dictionary that supports attribute-style access, a la JavaScript.
True
>>> b.foo is b['foo']
True
````
```


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

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

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

Including ``update()``:

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

As well as iteration:

````py
```py
>>> [ (k,b[k]) for k in b ]
[('ponies', 'are pretty!'), ('foo', Bunch(lol=True)), ('hello', 42)]
````
```

And "splats":

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


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

Bunches happily and transparently serialize to JSON and YAML.

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

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

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

````py
```py
>>> b = Bunch(foo=Bunch(lol=True), hello=42, ponies='are pretty!')
>>> import yaml
>>> yaml.dump(b)
'!bunch.Bunch\nfoo: !bunch.Bunch {lol: true}\nhello: 42\nponies: are pretty!\n'
>>> yaml.safe_dump(b)
'foo: {lol: true}\nhello: 42\nponies: are pretty!\n'
````
```

In addition, Bunch 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: ``Bunch.__str__ = Bunch.__repr__``. The Bunch class will also have a static method ``Bunch.fromYAML()``, which loads a Bunch out of a YAML string.

Expand All @@ -86,7 +86,7 @@ Miscellaneous
-------------

* It is safe to ``import *`` from this module. You'll get: ``Bunch``, ``bunchify``, and ``unbunchify``.
* Ample doctests
* Ample doctests:

$ python -m bunch.test
$ python -m bunch.test -v | tail -n22
Expand Down
Loading

0 comments on commit 9646d9b

Please sign in to comment.