Skip to content

fix: use different separator to flatten dict #4629

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

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/sagemaker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ def can_model_package_source_uri_autopopulate(source_uri: str):
)


def flatten_dict(source_dict: Dict[str, Any], sep: str = ".") -> Dict[str, Any]:
def flatten_dict(source_dict: Dict[str, Any], sep: str = "^") -> Dict[str, Any]:
"""Flatten a nested dictionary.

Args:
Expand All @@ -1620,7 +1620,7 @@ def flatten_dict(source_dict: Dict[str, Any], sep: str = ".") -> Dict[str, Any]:
return {}


def unflatten_dict(source_dict: Dict[str, Any], sep: str = ".") -> Dict[str, Any]:
def unflatten_dict(source_dict: Dict[str, Any], sep: str = "^") -> Dict[str, Any]:
"""Unflatten a flattened dictionary back into a nested dictionary.

Args:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,7 @@ def test_can_model_package_source_uri_autopopulate():
class TestDeepMergeDict(TestCase):
def test_flatten_dict_basic(self):
nested_dict = {"a": 1, "b": {"x": 2, "y": {"p": 3, "q": 4}}, "c": 5}
flattened_dict = {"a": 1, "b.x": 2, "b.y.p": 3, "b.y.q": 4, "c": 5}
flattened_dict = {"a": 1, "b^x": 2, "b^y^p": 3, "b^y^q": 4, "c": 5}
self.assertDictEqual(flatten_dict(nested_dict), flattened_dict)
self.assertDictEqual(unflatten_dict(flattened_dict), nested_dict)

Expand All @@ -1837,7 +1837,7 @@ def test_flatten_dict_no_nested(self):

def test_flatten_dict_with_various_types(self):
nested_dict = {"a": [1, 2, 3], "b": {"x": None, "y": {"p": [], "q": ""}}, "c": 9}
flattened_dict = {"a": [1, 2, 3], "b.x": None, "b.y.p": [], "b.y.q": "", "c": 9}
flattened_dict = {"a": [1, 2, 3], "b^x": None, "b^y^p": [], "b^y^q": "", "c": 9}
self.assertDictEqual(flatten_dict(nested_dict), flattened_dict)
self.assertDictEqual(unflatten_dict(flattened_dict), nested_dict)

Expand Down