-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate reducer and splitter in favor of reducers and splitters (#38)
- Loading branch information
Showing
7 changed files
with
113 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters