Skip to content

Commit

Permalink
#1; Added doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanBalfanz committed Jun 12, 2013
1 parent 10d7f25 commit 1f3861f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -8,7 +8,7 @@ upload: sdist
python setup.py upload

test:
PYTHONPATH=`pwd`/example_project/ DJANGO_SETTINGS_MODULE=example_project.settings.local nosetests
PYTHONPATH=`pwd`/example_project/ DJANGO_SETTINGS_MODULE=example_project.settings.local nosetests --with-doctest

clean:
find ./ -name "*.pyc" -delete
Expand Down
21 changes: 10 additions & 11 deletions admin_exporter/utils.py
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

import json
from contextlib import closing

Expand All @@ -13,6 +15,14 @@
def json_to_csv(jsonData, fieldnames=None):
"""
Returns a CSV string from the given JSON data string.
>>> import json
>>> data = [
... {"col1": "foo", "col2": "Ȓȳǡɴ"},
... {"col1": "foo", "col2": "bar"},
... {"col1": "foo", "col2": "bar"},
... ]
>>> csvData = json_to_csv(json.dumps(data))
"""
d = json.loads(jsonData)
fieldnames = fieldnames or d[0].keys()
Expand All @@ -33,14 +43,3 @@ def dict_to_csv(d, fieldnames):
buf = f.getvalue()

return buf


if __name__ == "__main__":
data = [
{"col2": "foo", "col1": "foo"},
{"col2": "bar", "col1": "bar"},
{"col2": "baz", "col1": "baz"},
]

jsonString = json.dumps(data)
print json_to_csv(json.dumps(data), data[0].keys())

0 comments on commit 1f3861f

Please sign in to comment.