Skip to content
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

Add option to treat digits as capitals in dict_to_snake #418

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 11 additions & 4 deletions camel_converter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ def dict_to_camel(data: dict[Any, Any]) -> dict[Any, Any]:
return converted


def dict_to_snake(data: dict[Any, Any]) -> dict[Any, Any]:
def dict_to_snake(
data: dict[Any, Any], *, treat_digits_as_capitals: bool = False
) -> dict[Any, Any]:
"""Converts dictionary keys from camel case or pascal case to snake case.

Only keys of type string are converted, any other type is left unchanged.

Args:
data: The dictionary to convert
data:
The dictionary to convert
treat_digits_as_capitals:
Whether to treat digits as capitals. For example myVariable2 would become my_variable_2
rather than my_variable2.

Returns:
A dictionary with they keys of type string converted from camel case or pascal case to
Expand All @@ -49,7 +55,7 @@ def dict_to_snake(data: dict[Any, Any]) -> dict[Any, Any]:
converted: dict[Any, Any] = {}
for k, v in data.items():
if isinstance(k, str):
key = to_snake(k)
key = to_snake(k, treat_digits_as_capitals=treat_digits_as_capitals)
else:
key = k

Expand Down Expand Up @@ -118,7 +124,8 @@ def to_snake(camel_string: str, *, treat_digits_as_capitals: bool = False) -> st
camel_string:
String in camel case or pascal case format. For example myVariable.
treat_digits_as_capitals:
Whether to treat digits as capitals. For example myVariable2 would become my_variable_2 rather than my_variable2.
Whether to treat digits as capitals. For example myVariable2 would become my_variable_2
rather than my_variable2.

Returns:
The string in snake case format. For example my_variable.
Expand Down
39 changes: 33 additions & 6 deletions tests/test_camel_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
"test_dict, expected",
[
(
{"convert_me": "val 1", "me_also": "val 2", "unchanged": "val 3", 1: "val 4"},
{
"convert_me": "val 1",
"me_also": "val 2",
"unchanged": "val 3",
1: "val 4",
},
{"convertMe": "val 1", "meAlso": "val 2", "unchanged": "val 3", 1: "val 4"},
),
(
Expand Down Expand Up @@ -93,16 +98,26 @@ def test_dict_to_pascal(test_dict, expected):


@pytest.mark.parametrize(
"test_dict, expected",
"test_dict, expected, treat_digits_as_capitals",
[
(
{"convertMe": "val 1", "meAlso": "val 2", "unchanged": "val 3", 1: "val 4"},
{"convert_me": "val 1", "me_also": "val 2", "unchanged": "val 3", 1: "val 4"},
{
"convert_me": "val 1",
"me_also": "val 2",
"unchanged": "val 3",
1: "val 4",
},
False,
),
(
{
"convertMe": "val 1",
"meAlso": {"nestedConvert": 1, "unchanged": 2, "anotherNest": {"oneMore": "hi"}},
"meAlso": {
"nestedConvert": 1,
"unchanged": 2,
"anotherNest": {"oneMore": "hi"},
},
1: {"anotherNested": "a"},
},
{
Expand All @@ -114,19 +129,31 @@ def test_dict_to_pascal(test_dict, expected):
},
1: {"another_nested": "a"},
},
False,
),
(
{"testConvert": [{"changeMe": 1, "unchanged": 2}, 1]},
{"test_convert": [{"change_me": 1, "unchanged": 2}, 1]},
False,
),
(
{"testConvert": ({"changeMe": 1, "unchanged": 2}, 1)},
{"test_convert": ({"change_me": 1, "unchanged": 2}, 1)},
False,
),
(
{"aTestWith12Number": 1, "AnotherTestWith12Number": 2, "endNumber1": 3},
{
"a_test_with_1_2_number": 1,
"another_test_with_1_2_number": 2,
"end_number_1": 3,
},
True,
),
],
)
def test_dict_to_snake(test_dict, expected):
assert dict_to_snake(test_dict) == expected
def test_dict_to_snake(test_dict, expected, treat_digits_as_capitals):
assert dict_to_snake(test_dict, treat_digits_as_capitals=treat_digits_as_capitals) == expected


@pytest.mark.parametrize(
Expand Down