Skip to content

Commit

Permalink
Added ability to add multiple arguments to dumps()
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Richardson committed Feb 5, 2019
1 parent 492d4d9 commit 7391be6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ useful logging.

CHANGELOG:

1.2.0: Added multi-argument support in dumps()
1.1.0: Added more supported versions of python and a test framework.
1.0.4: Fixed problem in Python 2 when using io.StringIO with dumper.
1.0.3: Fixed problems in Python 3 related to trying to use decode as member of str.
Expand Down
7 changes: 5 additions & 2 deletions dumper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,13 @@ def dump(val, output=None):
else:
Dumper(output=output).dump(val)

def dumps(val):
def dumps(val, *argv):
from io import StringIO
out = StringIO()
Dumper(output=out).dump(val)
dumper = Dumper(output=out)
dumper.dump(val)
for val in argv:
dumper.dump(val)
return out.getvalue()


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# http://packaging.python.org/en/latest/tutorial.html#version
version='1.1.0',
version='1.2.0',

description='Tool to conveniently describe any Python datastructure',
long_description=long_description,
Expand Down
39 changes: 37 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,45 @@ def do_dump_scalars():
def test_do_dump_scalars():
assert_output_as_expected(do_dump_scalars)

def do_dumps_multi_values():
s = dumps(1, " is less than ", 10) # returns unicode string in py2
if sys.version < (3, 0):
s = s.encode('ascii', 'replace') # convert back to regular string
dump(s)
return "\"1' is less than '10\""

def test_dumps_multi_values():
assert_output_as_expected(do_dumps_multi_values)

def do_dump_json():
obj = {
"httpCode": 200,
"extensionData": [
{
"extensionValue": "egg"
}
]
}
dump(obj)
return '''
<dict at {WORD}>:
httpCode: 200
extensionData: <list at {WORD}>
0: <dict at {WORD}>:
extensionValue: 'egg'
'''

def test_do_dump_json():
assert_output_matches_template(do_dump_json)

# END TEST CASES

def text_type(val):
if sys.version < '3':
return unicode(val)
else:
return str(val)

def assertMatching(a, b):
''' Asserts that the lines from string, a, match the lines in the string, b.
a is the expected string / pattern
Expand All @@ -40,7 +71,11 @@ def assertMatching(a, b):
raise AssertionError("a has " + text_type(len(a_lines)) + ", but b has " + text_type(len(b_lines)) + " lines: a={" + a + "}, b={" + b + "}")
for i in range(0, len(a_lines)):
assert a_lines[i] == b_lines[i]


def assert_output_matches_template(func):
# TODO: implement this
pass

def assert_output_as_expected(func):
# buffer stdout
try:
Expand Down

0 comments on commit 7391be6

Please sign in to comment.