Skip to content

Commit

Permalink
Move the snake case functions into their own module (#1198)
Browse files Browse the repository at this point in the history
* 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
PythonCoderAS authored and jarhill0 committed Dec 28, 2019
1 parent 418f258 commit e47ccba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
21 changes: 2 additions & 19 deletions praw/util/__init__.py
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
19 changes: 19 additions & 0 deletions praw/util/snake.py
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()}

0 comments on commit e47ccba

Please sign in to comment.