Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring util away #71

Merged
merged 2 commits into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: python
python:
- "3.7"
- "3.8"
- "pypy3"
install:
- pip install -r requirements.txt
Expand Down
43 changes: 27 additions & 16 deletions flatten_json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,26 @@

import six

from flatten_json.util import check_if_numbers_are_consecutive

try:
# 3.8 and up
from collections.abc import Iterable
except ImportError:
from collections import Iterable


def check_if_numbers_are_consecutive(list_):
"""
Returns True if numbers in the list are consecutive

:param list_: list of integers
:return: Boolean
"""
return all(
True if second - first == 1 else False
for first, second in zip(list_[:-1], list_[1:])
)


def _construct_key(previous_key, separator, new_key, replace_separators=None):
"""
Returns the new_key if no previous key exists, otherwise concatenates
Expand All @@ -40,7 +51,7 @@ def _construct_key(previous_key, separator, new_key, replace_separators=None):
def flatten(
nested_dict,
separator="_",
root_keys_to_ignore=set(),
root_keys_to_ignore=None,
replace_separators=None):
"""
Flattens a dictionary with nested structure to a dictionary with no
Expand All @@ -58,6 +69,9 @@ def flatten(
assert isinstance(nested_dict, dict), "flatten requires a dictionary input"
assert isinstance(separator, six.string_types), "separator must be string"

if root_keys_to_ignore is None:
root_keys_to_ignore = set()

# This global dictionary stores the flattened keys and values and is
# ultimately returned
flattened_dict = dict()
Expand Down Expand Up @@ -106,7 +120,7 @@ def _flatten(object_, key):


def flatten_preserve_lists(nested_dict, separator="_",
root_keys_to_ignore=set(),
root_keys_to_ignore=None,
max_list_index=3, max_depth=3,
replace_separators=None):
"""
Expand Down Expand Up @@ -134,6 +148,9 @@ def flatten_preserve_lists(nested_dict, separator="_",
assert isinstance(separator, six.string_types), \
"separator must be a string"

if root_keys_to_ignore is None:
root_keys_to_ignore = set()

# This global dictionary stores the flattened keys and values and is
# ultimately returned
flattened_dict = dict()
Expand All @@ -158,10 +175,8 @@ def _flatten(object_, key):
first_key = list(object_.keys())[0]
# if only 1 child value, and child value not a dict or list
# flatten immediately
if len(object_) == 1 \
and not (isinstance(object_[first_key], dict)
or isinstance(object_[first_key], list)
):
is_iter = isinstance(object_[first_key], Iterable)
if len(object_) == 1 and not is_iter:
flattened_dict[key] = object_[first_key]
else:
for object_key in object_:
Expand All @@ -174,15 +189,11 @@ def _flatten(object_, key):
object_key,
replace_separators=replace_separators))

elif isinstance(object_, list) or isinstance(object_, set):
elif isinstance(object_, (list, set, tuple)):
for index, item in enumerate(object_):
_flatten(
item,
_construct_key(
key,
separator,
index,
replace_separators=replace_separators))
key = _construct_key(key, separator, index,
replace_separators=replace_separators)
_flatten(item, key)

else:
flattened_dict[key] = object_
Expand Down
9 changes: 0 additions & 9 deletions flatten_json/util.py

This file was deleted.

3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

setup(
name='flatten_json',
py_modules=['flatten_json', 'util'],
version='0.1.8',
version='0.1.9',
description='Flatten JSON objects',
license='MIT',
author='Amir Ziai',
Expand Down
Loading