diff --git a/README.rst b/README.rst index 05aeadc..a342660 100644 --- a/README.rst +++ b/README.rst @@ -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', @@ -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'}, diff --git a/src/flatten_dict/flatten_dict.py b/src/flatten_dict/flatten_dict.py index f3a2003..7f55088 100644 --- a/src/flatten_dict/flatten_dict.py +++ b/src/flatten_dict/flatten_dict.py @@ -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 = { diff --git a/src/flatten_dict/reducer.py b/src/flatten_dict/reducer.py index 1bcd068..5df5c14 100644 --- a/src/flatten_dict/reducer.py +++ b/src/flatten_dict/reducer.py @@ -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, +) diff --git a/src/flatten_dict/reducers.py b/src/flatten_dict/reducers.py new file mode 100644 index 0000000..1bcd068 --- /dev/null +++ b/src/flatten_dict/reducers.py @@ -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 diff --git a/src/flatten_dict/splitter.py b/src/flatten_dict/splitter.py index 94a81b4..0eb43cc 100644 --- a/src/flatten_dict/splitter.py +++ b/src/flatten_dict/splitter.py @@ -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, +) diff --git a/src/flatten_dict/splitters.py b/src/flatten_dict/splitters.py new file mode 100644 index 0000000..94a81b4 --- /dev/null +++ b/src/flatten_dict/splitters.py @@ -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 diff --git a/src/flatten_dict/tests/flatten_dict_test.py b/src/flatten_dict/tests/flatten_dict_test.py index 2bdd33d..d2fd454 100644 --- a/src/flatten_dict/tests/flatten_dict_test.py +++ b/src/flatten_dict/tests/flatten_dict_test.py @@ -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,