Skip to content

Commit

Permalink
Updates tests for python3 and fixes yaml.load() deprecation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsc committed Sep 17, 2024
1 parent c52f4c1 commit 2badfdb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,30 @@ Miscellaneous
* Ample doctests:

$ python -m bunch.test
$ python -m bunch.test -v | tail -n22
1 items had no tests:
bunch.fromYAML
16 items passed all tests:
$ python -m bunch.test -v | tail -n24
20 items passed all tests:
8 tests in bunch
13 tests in bunch.Bunch
7 tests in bunch.Bunch.__contains__
7 tests in bunch.Bunch.__add__
11 tests in bunch.Bunch.__contains__
4 tests in bunch.Bunch.__delattr__
7 tests in bunch.Bunch.__getattr__
5 tests in bunch.Bunch.__iadd__
3 tests in bunch.Bunch.__repr__
5 tests in bunch.Bunch.__setattr__
5 tests in bunch.Bunch.copy
2 tests in bunch.Bunch.fromDict
4 tests in bunch.Bunch.fromYAML
2 tests in bunch.Bunch.toDict
3 tests in bunch.Bunch.toJSON
6 tests in bunch.Bunch.toYAML
5 tests in bunch.bunchify
2 tests in bunch.from_yaml
3 tests in bunch.toJSON
6 tests in bunch.toYAML
3 tests in bunch.to_yaml
3 tests in bunch.to_yaml_safe
4 tests in bunch.unbunchify
77 tests in 17 items.
77 passed and 0 failed.
102 tests in 20 items.
102 passed and 0 failed.
Test passed.


Expand Down
38 changes: 23 additions & 15 deletions bunch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class Bunch(dict):
As well as iteration...
>>> [ (k,b[k]) for k in b ]
[('ponies', 'are pretty!'), ('foo', Bunch(lol=True)), ('hello', 42)]
>>> sorted([ (k,b[k]) for k in b ])
[('foo', Bunch(lol=True)), ('hello', 42), ('ponies', 'are pretty!')]
And "splats".
Expand Down Expand Up @@ -156,10 +156,10 @@ def __delattr__(self, k):
propagate as an AttributeError instead.
>>> b = Bunch(lol=42)
>>> del b.values
>>> del b.values # doctest: +ELLIPSIS
Traceback (most recent call last):
...
AttributeError: 'Bunch' object attribute 'values' is read-only
AttributeError: ...values...
>>> del b.lol
>>> b.lol
Traceback (most recent call last):
Expand Down Expand Up @@ -311,7 +311,7 @@ def bunchify(it, BunchClass=Bunch):
the second parameter.
"""
if isinstance(it, Mapping):
return BunchClass( (k, bunchify(v, BunchClass)) for k, v in it.iteritems() )
return BunchClass( (k, bunchify(it[k], BunchClass)) for k in iter(it) )
elif isinstance(it, (list, tuple)):
return type(it)( (bunchify(v, BunchClass) for v in it) )
else:
Expand Down Expand Up @@ -339,7 +339,7 @@ def unbunchify(it, DictClass=dict):
the second parameter.
"""
if isinstance(it, Mapping):
return DictClass( (k, unbunchify(v, DictClass)) for k, v in it.iteritems() )
return DictClass( (k, unbunchify(it[k], DictClass)) for k in iter(it) )
elif isinstance(it, (list, tuple)):
return type(it)( (unbunchify(v, DictClass) for v in it) )
else:
Expand All @@ -359,9 +359,9 @@ def toJSON(self, **options):
>>> b = Bunch(foo=Bunch(lol=True), hello=42, ponies='are pretty!')
>>> json.dumps(b)
'{"ponies": "are pretty!", "foo": {"lol": true}, "hello": 42}'
'{"foo": {"lol": true}, "hello": 42, "ponies": "are pretty!"}'
>>> b.toJSON()
'{"ponies": "are pretty!", "foo": {"lol": true}, "hello": 42}'
'{"foo": {"lol": true}, "hello": 42, "ponies": "are pretty!"}'
"""
return json.dumps(self, **options)

Expand All @@ -382,7 +382,7 @@ def from_yaml(loader, node):
""" PyYAML support for Bunches using the tag ``!bunch`` and ``!bunch.Bunch``.
>>> import yaml
>>> yaml.load('''
>>> yaml.full_load('''
... Flow style: !bunch.Bunch { Clark: Evans, Brian: Ingerson, Oren: Ben-Kiki }
... Block style: !bunch
... Clark : Evans
Expand Down Expand Up @@ -470,8 +470,11 @@ def fromYAML(cls, *args, **kwargs):
>>> Bunch.fromYAML(document)
Bunch(foo=['bar', Bunch(lol=True)], hello=42)
Uses ``yaml.load()`` by default; pass ``safe=True`` to use the SafeLoader,
and/or ``all=True`` to load all documents (returning a list).
Uses ``yaml.load()`` by default, but accepts the following for convenience:
- ``safe=True`` for SafeLoader
- ``full=True`` for FullLoader (default)
- ``unsafe=True`` for UnsafeLoader
- ``all=True`` to load all documents (returning a list)
>>> documents = '''
... ---
Expand All @@ -491,11 +494,16 @@ def fromYAML(cls, *args, **kwargs):
[Bunch(hp=12, level=2, name='Orc')]]
All other options are passed to PyYAML, so you can still specify a
custom loader with ``Bunch.fromYAML(data, Loader=CustomLoader)``.
custom loader with ``Bunch.fromYAML(data, Loader=CustomLoader)``. (Note that
supplying a kw argument ``Loader`` overrides ``safe``, ``full``, and ``unsafe``.)
See https://msg.pyyaml.org/load for more info.
"""
method_name = 'load'
if kwargs.pop('safe', False) and 'Loader' not in kwargs:
method_name = 'safe_load'
method_name = 'full_load'
for prefix in ('safe', 'full', 'unsafe'):
# we want to pop all the prefix keys anyway, so put the test for Loader last
if kwargs.pop(prefix, False) and 'Loader' not in kwargs:
method_name = prefix+'_load'

if kwargs.pop('all', False):
data = list(getattr(yaml, method_name+'_all')(*args, **kwargs))
Expand Down

0 comments on commit 2badfdb

Please sign in to comment.