Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #394 from dephell/flatdict
Browse files Browse the repository at this point in the history
Remove flatdict
  • Loading branch information
orsinium committed Mar 11, 2020
2 parents e95b34a + 8922d5b commit d79070b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
23 changes: 19 additions & 4 deletions dephell/actions/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from ..imports import lazy_import


flatdict = lazy_import('flatdict')
pygments = lazy_import('pygments')
pygments_lexers = lazy_import('pygments.lexers')
pygments_formatters = lazy_import('pygments.formatters')
Expand Down Expand Up @@ -38,6 +37,23 @@ def _flatten(value) -> list:
return new_value


def _flatdict(data, sep: str = '.', prefix: str = ''):
if isinstance(data, (list, tuple)):
return [_flatdict(row) for row in data]
if isinstance(data, dict):
result = dict()
for key, value in data.items():
new_key = str(key)
if prefix:
new_key = prefix + sep + new_key
if isinstance(value, dict):
result.update(_flatdict(data=value, sep=sep, prefix=new_key))
else:
result[new_key] = value
return result
return data


FILTERS = {
'each()': _each,
'first()': lambda v: v[0],
Expand Down Expand Up @@ -101,17 +117,16 @@ def _beautify(data, *, colors: bool, table: bool) -> str:
if table:
# one dict
if isinstance(data, dict):
data = flatdict.FlatDict(data, delimiter='.').items()
return tabulate.tabulate(
data,
_flatdict(data).items(),
headers=('key', 'value'),
tablefmt='fancy_grid',
)
# list of dicts
if isinstance(data, list) and data and isinstance(data[0], dict):
table = []
for row in data:
row = flatdict.FlatDict(row, delimiter='.')
row = _flatdict(row)
keys = tuple(row)
row = [v for _, v in sorted(row.items())]
table.append(row)
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ bowler = {optional = true, version = "*"}
docker = {optional = true, version = "*"}
dockerpty = {optional = true, version = "*"}
fissix = {optional = true, allows-prereleases = true, version = "*"}
flatdict = {optional = true, version = "*"}
graphviz = {optional = true, version = "*"}
html5lib = {optional = true, version = "*"}
pygments = {optional = true, version = "*"}
Expand Down Expand Up @@ -198,4 +197,4 @@ isort = {extras = ["pyproject"], version = "*"}
[tool.poetry.extras]
docs = ["alabaster", "pygments-github-lexers", "recommonmark", "sphinx"]
tests = ["aioresponses", "pytest", "requests-mock"]
full = ["aiofiles", "appdirs", "autopep8", "bowler", "colorama", "docker", "dockerpty", "fissix", "flatdict", "graphviz", "html5lib", "pygments", "ruamel-yaml", "tabulate", "yapf"]
full = ["aiofiles", "appdirs", "autopep8", "bowler", "colorama", "docker", "dockerpty", "fissix", "graphviz", "html5lib", "pygments", "ruamel-yaml", "tabulate", "yapf"]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
],
"full": [
"aiofiles", "appdirs", "autopep8", "bowler", "colorama", "docker",
"dockerpty", "fissix", "flatdict", "graphviz", "html5lib",
"dockerpty", "fissix", "graphviz", "html5lib",
"pygments", "tabulate", "yapf"
],
"tests": ["aioresponses", "pytest", "requests-mock"]
Expand Down
15 changes: 15 additions & 0 deletions tests/test_actions/test_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

from dephell.actions._json import _flatdict


@pytest.mark.parametrize('given, expected', [
(1, 1),
({1: 2}, {'1': 2}),
({1: {2: 3}}, {'1.2': 3}),
({1: {2: 3, 4: 5}}, {'1.2': 3, '1.4': 5}),
({1: {2: 3, 4: 5}, 6: 7}, {'1.2': 3, '1.4': 5, '6': 7}),
([{1: {2: 3}}, {4: {5: 6}}], [{'1.2': 3}, {'4.5': 6}]),
])
def test_flatdict(given, expected):
assert _flatdict(given) == expected

0 comments on commit d79070b

Please sign in to comment.