forked from graphql-python/graphql-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_case.py
More file actions
27 lines (18 loc) · 778 Bytes
/
convert_case.py
File metadata and controls
27 lines (18 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Conversion between camel and snake case"""
# uses code from https://github.com/daveoncode/python-string-utils
import re
__all__ = ["camel_to_snake", "snake_to_camel"]
_re_camel_to_snake = re.compile(r"([a-z]|[A-Z0-9]+(?=[A-Z])|[0-9]+)(?=[A-Z])")
_re_snake_to_camel = re.compile(r"(_)([a-z\d])")
def camel_to_snake(s: str) -> str:
"""Convert from CamelCase to snake_case"""
return _re_camel_to_snake.sub(r"\1_", s).lower()
def snake_to_camel(s: str, upper: bool = True) -> str:
"""Convert from snake_case to CamelCase
If upper is set, then convert to upper CamelCase, otherwise the first character
keeps its case.
"""
s = _re_snake_to_camel.sub(lambda m: m.group(2).upper(), s)
if upper:
s = s[:1].upper() + s[1:]
return s