Skip to content

Commit

Permalink
Version 2.2.0 (#12)
Browse files Browse the repository at this point in the history
* Adding update, set_default and dir support

* Adding tests, fixing up readme

* Python2.x compatibility

* Support for ruamel.yaml (#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
  • Loading branch information
cdgriffith authored Apr 2, 2017
1 parent a948c69 commit 52515ef
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 16 deletions.
17 changes: 17 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Box is written and maintained by Chris Griffith [email protected].

A big thank you to everyone that has helped! From PRs to suggestions and bug
reporting, all input is greatly appreciated!

Code contributions:

- Alexandre Decan (AlexandreDecan)

Suggestions and bug reporting:

- JiuLi Gao (gaojiuli)
- Jürgen Hermann (jhermann)
- /u/tilkau
- /u/Jumpy89
- /u/can_dry
- /u/spidyfan21
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version 2.2.0
=============

* Adding support for `ruamel.yaml` (Thanks to Alexandre Decan)
* Adding Contributing and Authors files

Version 2.1.0
=============

Expand Down
39 changes: 39 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Contributing to Box
===================

Thank you for looking into supporting Box. Any constructive input
is greatly appreciated!

Questions and Ideas
-------------------

Even if you don't have code contributions, but just an idea, or a question about
Box, please feel free to open an issue!

Reporting Bugs
--------------

- Please include sample code and traceback (or unexpected behavior)
of the error you are experiencing.

- Python version and Operating System.

Pull Requests
-------------

- Follow PEP8

- New features should have
- Reasoning for addition in pull request
- Docstring with code block example and parameters
- Tests with as much coverage as reasonable
- Tests should go through both sad and happy paths

- Bug fixes should include
- Explain under which circumstances the bug occurs in the pull request
- Tests for new happy and sad paths
- Test proving error without new code

- Update CHANGES.rst to include new feature or fix

- Add yourself to AUTHORS.rst!
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Available on all systems that support the default `json` library.::
**to_yaml**

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

to_yaml(filename=None, default_flow_style=False, **yaml_kwargs)
Transform the Box object into a YAML string.
Expand Down
24 changes: 13 additions & 11 deletions box.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
import yaml
yaml_support = True
except ImportError:
yaml = None
try:
import ruamel.yaml as yaml
yaml_support = True
except ImportError:
yaml = None

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

__all__ = ['Box', 'ConfigBox', 'LightBox', 'BoxList']
__author__ = "Chris Griffith"
__version__ = "2.1.0"
__version__ = "2.2.0"


class LightBox(dict):
Expand Down Expand Up @@ -163,8 +167,8 @@ def to_dict(self, in_dict=None):

def to_json(self, filename=None, indent=4, **json_kwargs):
"""
Transform the Box object into a JSON string.
Transform the Box object into a JSON string.
:param filename: If provided will save to file
:param indent: Automatic formatting by indent size in spaces
:param json_kwargs: additional arguments to pass to json.dump(s)
Expand All @@ -181,8 +185,8 @@ def to_json(self, filename=None, indent=4, **json_kwargs):
def to_yaml(self, filename=None, default_flow_style=False,
**yaml_kwargs):
"""
Transform the Box object into a YAML string.
Transform the Box object into a YAML string.
:param filename: If provided will save to file
:param default_flow_style: False will recursively dump dicts
:param yaml_kwargs: additional arguments to pass to yaml.dump
Expand Down Expand Up @@ -210,13 +214,12 @@ def _recursive_create(self, iterable, include_lists=False, box_class=LightBox):

class Box(LightBox):
"""
Same as LightBox,
Same as LightBox,
but also goes into lists and makes dicts within into Boxes.
The lists are turned into BoxLists
so that they can also intercept incoming items and turn
them into Boxes.
"""

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -295,7 +298,7 @@ def update(self, item=None, **kwargs):
def setdefault(self, item, default=None):
if item in self:
return self[item]

if isinstance(default, dict):
default = Box(default)
elif isinstance(default, list):
Expand All @@ -307,7 +310,7 @@ def setdefault(self, item, default=None):
class BoxList(list):
"""
Drop in replacement of list, that converts added objects to Box or BoxList
objects as necessary.
objects as necessary.
"""
__box_class__ = Box

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

_protected_keys = dir({}) + ['to_dict', 'bool', 'int', 'float',
Expand Down
8 changes: 5 additions & 3 deletions test/test_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import unittest
import json

import yaml
try:
import yaml
except ImportError:
import ruamel.yaml as yaml

from box import Box, ConfigBox, LightBox, BoxList

Expand Down Expand Up @@ -72,8 +75,7 @@ def test_box_from_dict(self):

def test_box_from_bad_dict(self):
try:
ns = Box('{"k1": "v1", '
'"k2": {"k3": "v2"}}')
Box('{"k1": "v1", "k2": {"k3": "v2"}}')
except ValueError:
assert True
else:
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ deps=
rarfile
scandir
pytest-cov
commands=py.test --cov=box test/
commands=py.test --cov=box test/

0 comments on commit 52515ef

Please sign in to comment.