Skip to content

Commit 52515ef

Browse files
authored
Version 2.2.0 (cdgriffith#12)
* Adding update, set_default and dir support * Adding tests, fixing up readme * Python2.x compatibility * Support for ruamel.yaml (cdgriffith#8) * [ci skip] adding Authors and Contributing files. * [ci skip] Updating Authors * [ci skip] Updating Changes * Fixing tests when using ruamel.yaml instead of pyyaml * [ci skip] Update changes.rst * Fixing PEP8 issues
1 parent a948c69 commit 52515ef

7 files changed

Lines changed: 82 additions & 16 deletions

File tree

AUTHORS.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Box is written and maintained by Chris Griffith chris@cdgriffith.com.
2+
3+
A big thank you to everyone that has helped! From PRs to suggestions and bug
4+
reporting, all input is greatly appreciated!
5+
6+
Code contributions:
7+
8+
- Alexandre Decan (AlexandreDecan)
9+
10+
Suggestions and bug reporting:
11+
12+
- JiuLi Gao (gaojiuli)
13+
- Jürgen Hermann (jhermann)
14+
- /u/tilkau
15+
- /u/Jumpy89
16+
- /u/can_dry
17+
- /u/spidyfan21

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Version 2.2.0
2+
=============
3+
4+
* Adding support for `ruamel.yaml` (Thanks to Alexandre Decan)
5+
* Adding Contributing and Authors files
6+
17
Version 2.1.0
28
=============
39

CONTRIBUTING.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Contributing to Box
2+
===================
3+
4+
Thank you for looking into supporting Box. Any constructive input
5+
is greatly appreciated!
6+
7+
Questions and Ideas
8+
-------------------
9+
10+
Even if you don't have code contributions, but just an idea, or a question about
11+
Box, please feel free to open an issue!
12+
13+
Reporting Bugs
14+
--------------
15+
16+
- Please include sample code and traceback (or unexpected behavior)
17+
of the error you are experiencing.
18+
19+
- Python version and Operating System.
20+
21+
Pull Requests
22+
-------------
23+
24+
- Follow PEP8
25+
26+
- New features should have
27+
- Reasoning for addition in pull request
28+
- Docstring with code block example and parameters
29+
- Tests with as much coverage as reasonable
30+
- Tests should go through both sad and happy paths
31+
32+
- Bug fixes should include
33+
- Explain under which circumstances the bug occurs in the pull request
34+
- Tests for new happy and sad paths
35+
- Test proving error without new code
36+
37+
- Update CHANGES.rst to include new feature or fix
38+
39+
- Add yourself to AUTHORS.rst!

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ Available on all systems that support the default `json` library.::
185185
186186
**to_yaml**
187187

188-
Only available if `PyYAML` is installed (not automatically installed via pip or `setup.py`)::
188+
Only available if `PyYAML` or `ruamel.yaml` is installed (not automatically installed via pip or `setup.py`)::
189189

190190
to_yaml(filename=None, default_flow_style=False, **yaml_kwargs)
191191
Transform the Box object into a YAML string.

box.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@
2222
import yaml
2323
yaml_support = True
2424
except ImportError:
25-
yaml = None
25+
try:
26+
import ruamel.yaml as yaml
27+
yaml_support = True
28+
except ImportError:
29+
yaml = None
2630

2731
if sys.version_info >= (3, 0):
2832
basestring = str
2933

3034
__all__ = ['Box', 'ConfigBox', 'LightBox', 'BoxList']
3135
__author__ = "Chris Griffith"
32-
__version__ = "2.1.0"
36+
__version__ = "2.2.0"
3337

3438

3539
class LightBox(dict):
@@ -163,8 +167,8 @@ def to_dict(self, in_dict=None):
163167

164168
def to_json(self, filename=None, indent=4, **json_kwargs):
165169
"""
166-
Transform the Box object into a JSON string.
167-
170+
Transform the Box object into a JSON string.
171+
168172
:param filename: If provided will save to file
169173
:param indent: Automatic formatting by indent size in spaces
170174
:param json_kwargs: additional arguments to pass to json.dump(s)
@@ -181,8 +185,8 @@ def to_json(self, filename=None, indent=4, **json_kwargs):
181185
def to_yaml(self, filename=None, default_flow_style=False,
182186
**yaml_kwargs):
183187
"""
184-
Transform the Box object into a YAML string.
185-
188+
Transform the Box object into a YAML string.
189+
186190
:param filename: If provided will save to file
187191
:param default_flow_style: False will recursively dump dicts
188192
:param yaml_kwargs: additional arguments to pass to yaml.dump
@@ -210,13 +214,12 @@ def _recursive_create(self, iterable, include_lists=False, box_class=LightBox):
210214

211215
class Box(LightBox):
212216
"""
213-
Same as LightBox,
217+
Same as LightBox,
214218
but also goes into lists and makes dicts within into Boxes.
215219
216220
The lists are turned into BoxLists
217221
so that they can also intercept incoming items and turn
218222
them into Boxes.
219-
220223
"""
221224

222225
def __init__(self, *args, **kwargs):
@@ -295,7 +298,7 @@ def update(self, item=None, **kwargs):
295298
def setdefault(self, item, default=None):
296299
if item in self:
297300
return self[item]
298-
301+
299302
if isinstance(default, dict):
300303
default = Box(default)
301304
elif isinstance(default, list):
@@ -307,7 +310,7 @@ def setdefault(self, item, default=None):
307310
class BoxList(list):
308311
"""
309312
Drop in replacement of list, that converts added objects to Box or BoxList
310-
objects as necessary.
313+
objects as necessary.
311314
"""
312315
__box_class__ = Box
313316

@@ -363,7 +366,6 @@ class ConfigBox(LightBox):
363366
cns.bool('my_bool') # True
364367
cns.int('my_int') # 5
365368
cns.list('my_list', mod=lambda x: int(x)) # [5, 4, 3, 3, 2]
366-
367369
"""
368370

369371
_protected_keys = dir({}) + ['to_dict', 'bool', 'int', 'float',

test/test_box.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import unittest
44
import json
55

6-
import yaml
6+
try:
7+
import yaml
8+
except ImportError:
9+
import ruamel.yaml as yaml
710

811
from box import Box, ConfigBox, LightBox, BoxList
912

@@ -72,8 +75,7 @@ def test_box_from_dict(self):
7275

7376
def test_box_from_bad_dict(self):
7477
try:
75-
ns = Box('{"k1": "v1", '
76-
'"k2": {"k3": "v2"}}')
78+
Box('{"k1": "v1", "k2": {"k3": "v2"}}')
7779
except ValueError:
7880
assert True
7981
else:

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ deps=
77
rarfile
88
scandir
99
pytest-cov
10-
commands=py.test --cov=box test/
10+
commands=py.test --cov=box test/

0 commit comments

Comments
 (0)