Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1069 from Pyprohly/objector-cleanup-1
Objector changes
- Loading branch information
Showing
14 changed files
with
93 additions
and
61 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
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
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
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
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,3 +1,21 @@ | ||
"""Package imports for utilities.""" | ||
|
||
__all__ = ("cache",) | ||
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()} |
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,29 @@ | ||
from praw.util import camel_to_snake | ||
|
||
from .. import UnitTest | ||
|
||
|
||
class TestUtil(UnitTest): | ||
def test_camel_to_snake(self): | ||
test_strings = ( | ||
("camelCase", "camel_case"), | ||
("PascalCase", "pascal_case"), | ||
("camelCasePlace", "camel_case_place"), | ||
("Pascal8Camel8Snake", "pascal8_camel8_snake"), | ||
("HTTPResponseCode", "http_response_code"), | ||
("ResponseHTTP", "response_http"), | ||
("ResponseHTTP200", "response_http200"), | ||
("getHTTPResponseCode", "get_http_response_code"), | ||
("get200HTTPResponseCode", "get200_http_response_code"), | ||
("getHTTP200ResponseCode", "get_http200_response_code"), | ||
("12PolarBears", "12_polar_bears"), | ||
("11buzzingBees", "11buzzing_bees"), | ||
("TacocaT", "tacoca_t"), | ||
("fooBARbaz", "foo_ba_rbaz"), | ||
("foo_BAR_baz", "foo_bar_baz"), | ||
("Base_BASE", "base_base"), | ||
("Case_Case", "case_case"), | ||
("FACE_Face", "face_face"), | ||
) | ||
for camel, snake in test_strings: | ||
assert camel_to_snake(camel) == snake |