Skip to content

Commit

Permalink
Deprecate reducer and splitter in favor of reducers and splitters (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianlini authored Oct 26, 2020
1 parent 9aa848e commit 40974bf
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 95 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ The `reducer` parameter supports ``'tuple'``, ``'path'``, ``'underscore'``, ``'d

There is also a factory function `make_reducer()` to help you create customized reducer. The function currently only supports customized delimiter:

>>> from flatten_dict.reducer import make_reducer
>>> from flatten_dict.reducers import make_reducer
>>> pprint(flatten(normal_dict, reducer=make_reducer(delimiter='_')))
{'a': '0',
'b_a': '1.0',
Expand Down Expand Up @@ -258,7 +258,7 @@ The `splitter` parameter supports ``'tuple'``, ``'path'``, ``'underscore'``, ``'

There is also a factory function `make_splitter()` to help you create customized splitter. The function currently only supports customized delimiter:

>>> from flatten_dict.splitter import make_splitter
>>> from flatten_dict.splitters import make_splitter
>>> pprint(unflatten(flat_dict, splitter=make_splitter(delimiter='_')))
{'a': '0',
'b': {'a': '1.0', 'b': '1.1'},
Expand Down
4 changes: 2 additions & 2 deletions src/flatten_dict/flatten_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import six

from .reducer import tuple_reducer, path_reducer, dot_reducer, underscore_reducer
from .splitter import tuple_splitter, path_splitter, dot_splitter, underscore_splitter
from .reducers import tuple_reducer, path_reducer, dot_reducer, underscore_reducer
from .splitters import tuple_splitter, path_splitter, dot_splitter, underscore_splitter


REDUCER_DICT = {
Expand Down
56 changes: 7 additions & 49 deletions src/flatten_dict/reducer.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,9 @@
def tuple_reducer(k1, k2):
if k1 is None:
return (k2,)
else:
return k1 + (k2,)
"""`flatten_dict.reducer` is deprecated in favor of `flatten_dict.reducers`."""
import warnings

from .reducers import * # noqa: F401, F403

def path_reducer(k1, k2):
import os.path

if k1 is None:
return k2
else:
return os.path.join(k1, k2)


def dot_reducer(k1, k2):
if k1 is None:
return k2
else:
return "{}.{}".format(k1, k2)


def underscore_reducer(k1, k2):
if k1 is None:
return k2
else:
return "{}_{}".format(k1, k2)


def make_reducer(delimiter):
"""Create a reducer with a custom delimiter.
Parameters
----------
delimiter : str
Delimiter to use to join keys.
Returns
-------
f : Callable
Callable that can be passed to `flatten()`'s `reducer` argument.
"""

def f(k1, k2):
if k1 is None:
return k2
else:
return "{}{}{}".format(k1, delimiter, k2)

return f
warnings.warn(
"`flatten_dict.reducer` is deprecated in favor of `flatten_dict.reducers`.",
FutureWarning,
)
51 changes: 51 additions & 0 deletions src/flatten_dict/reducers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
def tuple_reducer(k1, k2):
if k1 is None:
return (k2,)
else:
return k1 + (k2,)


def path_reducer(k1, k2):
import os.path

if k1 is None:
return k2
else:
return os.path.join(k1, k2)


def dot_reducer(k1, k2):
if k1 is None:
return k2
else:
return "{}.{}".format(k1, k2)


def underscore_reducer(k1, k2):
if k1 is None:
return k2
else:
return "{}_{}".format(k1, k2)


def make_reducer(delimiter):
"""Create a reducer with a custom delimiter.
Parameters
----------
delimiter : str
Delimiter to use to join keys.
Returns
-------
f : Callable
Callable that can be passed to `flatten()`'s `reducer` argument.
"""

def f(k1, k2):
if k1 is None:
return k2
else:
return "{}{}{}".format(k1, delimiter, k2)

return f
47 changes: 7 additions & 40 deletions src/flatten_dict/splitter.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
def tuple_splitter(flat_key):
return flat_key
"""`flatten_dict.splitter` is deprecated in favor of `flatten_dict.splitters`."""
import warnings

from .splitters import * # noqa: F401, F403

def path_splitter(flat_key):
try:
from pathlib import PurePath
except ImportError:
from pathlib2 import PurePath
keys = PurePath(flat_key).parts
return keys


def dot_splitter(flat_key):
keys = tuple(flat_key.split("."))
return keys


def underscore_splitter(flat_key):
keys = tuple(flat_key.split("_"))
return keys


def make_splitter(delimiter):
"""Create a reducer with a custom delimiter.
Parameters
----------
delimiter : str
Delimiter to use to split keys.
Returns
-------
f : Callable
Callable that can be passed to ``unflatten``'s ``splitter`` argument.
"""

def f(flat_key):
keys = tuple(flat_key.split(delimiter))
return keys

return f
warnings.warn(
"`flatten_dict.splitter` is deprecated in favor of `flatten_dict.splitters`.",
FutureWarning,
)
42 changes: 42 additions & 0 deletions src/flatten_dict/splitters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
def tuple_splitter(flat_key):
return flat_key


def path_splitter(flat_key):
try:
from pathlib import PurePath
except ImportError:
from pathlib2 import PurePath
keys = PurePath(flat_key).parts
return keys


def dot_splitter(flat_key):
keys = tuple(flat_key.split("."))
return keys


def underscore_splitter(flat_key):
keys = tuple(flat_key.split("_"))
return keys


def make_splitter(delimiter):
"""Create a reducer with a custom delimiter.
Parameters
----------
delimiter : str
Delimiter to use to split keys.
Returns
-------
f : Callable
Callable that can be passed to ``unflatten``'s ``splitter`` argument.
"""

def f(flat_key):
keys = tuple(flat_key.split(delimiter))
return keys

return f
4 changes: 2 additions & 2 deletions src/flatten_dict/tests/flatten_dict_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import pytest

from flatten_dict import flatten, unflatten
from flatten_dict.reducer import (
from flatten_dict.reducers import (
tuple_reducer,
path_reducer,
underscore_reducer,
make_reducer,
)
from flatten_dict.splitter import (
from flatten_dict.splitters import (
tuple_splitter,
path_splitter,
underscore_splitter,
Expand Down

0 comments on commit 40974bf

Please sign in to comment.