Skip to content

Commit

Permalink
Works with Python 3 now
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-ziai-zefr committed Feb 1, 2017
1 parent f7bf468 commit 49e9cd2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 69 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ The final result as a Pandas dataframe:
0 1 2 3 4
1 0.5 NaN 3.2 NaN
2 0.8 1.8 NaN NaN
```
```

###
1 change: 0 additions & 1 deletion flatten_json/__init__.py

This file was deleted.

23 changes: 0 additions & 23 deletions flatten_json/flatten_json.py

This file was deleted.

33 changes: 10 additions & 23 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
from distutils.core import setup
#from Cython.Build import cythonize


# with open('README.md') as f:
# readme = f.read()


# with open('LICENSE') as f:
# license = f.read()

setup(
name = 'flatten_json',
packages = ['flatten_json'],
version = '0.1.2',
description = 'Flatten JSON objects',
# long_description = readme,
author = 'Amir Ziai',
author_email = '[email protected]',
url = 'https://github.com/amirziai/flatten',
# download_url = '...',
keywords = ['json', 'flatten', 'pandas'],
# license=license,
classifiers = [],
# ext_modules = cythonize("flatten_json/flatten_json.pyx")
)
name='flatten_json',
packages=[''],
version='0.1.3',
description='Flatten JSON objects',
author='Amir Ziai',
author_email='[email protected]',
url='https://github.com/amirziai/flatten',
keywords=['json', 'flatten', 'pandas'],
classifiers=[],
)
63 changes: 42 additions & 21 deletions test_flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,62 @@

from flatten_json import flatten_json


class UnitTests(unittest.TestCase):
def test_no_flatten(self):
dic = {'a': '1', 'b': '2', 'c': 3}
assert 3 == len(flatten_json(dic))

expected = dic
actual = flatten_json(dic)
self.assertEqual(actual, expected)

def test_one_flatten(self):
dic = {'a': '1',
'b': '2',
'c': {'c1': '3', 'c2': '4'}
}
assert 4 == len(flatten_json(dic))


def test_one_flatten_keys(self):
'b': '2',
'c': {'c1': '3', 'c2': '4'}
}
expected = {'a': '1', 'b': '2', 'c_c1': '3', 'c_c2': '4'}
actual = flatten_json(dic)
self.assertEqual(actual, expected)

def test_custom_separator(self):
dic = {'a': '1',
'b': '2',
'c': {'c1': '3', 'c2': '4'}
}
keys = ['a', 'b', 'c_c1', 'c_c2']
self.assertItemsEqual(keys, flatten_json(dic).keys())

'b': '2',
'c': {'c1': '3', 'c2': '4'}
}
expected = {'a': '1', 'b': '2', 'c*c1': '3', 'c*c2': '4'}
actual = flatten_json(dic, '*')
self.assertEqual(actual, expected)

def test_list(self):
dic = {
'a': 1,
'b': [{'c': [2, 3]}]
}
expected = {'a': 1, 'b_0_c_0': 2, 'b_0_c_1': 3}
actual = flatten_json(dic)
self.assertEqual(actual, expected)

def test_three_flatten(self):
def test_list_and_dict(self):
dic = {
'a': 1,
'b': 2,
'c': [{'d': [2, 3, 4], 'e': [{'f': 1, 'g': 2}]}]
}
flattened = flatten_json(dic)
keys = ['a', 'b', 'c_0_e_0_g', 'c_0_e_0_f', 'c_0_d_1', 'c_0_d_0', 'c_0_d_2']
expected = {'a': 1, 'b': 2, 'c_0_d_0': 2, 'c_0_d_1': 3, 'c_0_d_2': 4, 'c_0_e_0_f': 1, 'c_0_e_0_g': 2}
actual = flatten_json(dic)
self.assertEqual(actual, expected)

assert 7 == len(flattened)
self.assertItemsEqual(keys, flattened.keys())
def test_blog_example(self):
dic = {
"a": 1,
"b": 2,
"c": [{"d": ['2', 3, 4], "e": [{"f": 1, "g": 2}]}]
}
expected = {'a': 1, 'b': 2, 'c_0_d_0': '2', 'c_0_d_1': 3, 'c_0_d_2': 4, 'c_0_e_0_f': 1,
'c_0_e_0_g': 2}
actual = flatten_json(dic)
self.assertEqual(actual, expected)


if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 49e9cd2

Please sign in to comment.