-
-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the snake case functions into their own module (#1198)
* Move the snake case functions into their own module * Address flake8 * Address pydocstyle * Address black * Update __init__.py * Update __init__.py * Update __init__.py
- Loading branch information
1 parent
418f258
commit e47ccba
Showing
2 changed files
with
21 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,4 @@ | ||
"""Package imports for utilities.""" | ||
|
||
import re | ||
|
||
__all__ = ("cache", "camel_to_snake", "snake_case_keys") | ||
|
||
_re_camel_to_snake = re.compile(r"([a-z0-9](?=[A-Z])|[A-Z](?=[A-Z][a-z]))") | ||
|
||
|
||
def camel_to_snake(name): | ||
"""Convert `name` from camelCase to snake_case.""" | ||
return _re_camel_to_snake.sub(r"\1_", name).lower() | ||
|
||
|
||
def snake_case_keys(dictionary): | ||
"""Return a new dictionary with keys converted to snake_case. | ||
:param dictionary: The dict to be corrected. | ||
""" | ||
return {camel_to_snake(k): v for k, v in dictionary.items()} | ||
from .cache import cachedproperty # noqa: F401 | ||
from .snake import camel_to_snake, snake_case_keys # noqa: F401 |
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,19 @@ | ||
"""Contains functions dealing with snake case conversions.""" | ||
|
||
import re | ||
|
||
_re_camel_to_snake = re.compile(r"([a-z0-9](?=[A-Z])|[A-Z](?=[A-Z][a-z]))") | ||
|
||
|
||
def camel_to_snake(name): | ||
"""Convert `name` from camelCase to snake_case.""" | ||
return _re_camel_to_snake.sub(r"\1_", name).lower() | ||
|
||
|
||
def snake_case_keys(dictionary): | ||
"""Return a new dictionary with keys converted to snake_case. | ||
:param dictionary: The dict to be corrected. | ||
""" | ||
return {camel_to_snake(k): v for k, v in dictionary.items()} |