-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.py
190 lines (143 loc) · 4.87 KB
/
main.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: utf-8 -*-
"""
This module contains all the core logic for humps.
"""
import re
import sys
try:
from collections.abc import Mapping
except ImportError:
from collections import Mapping
_ver = sys.version_info
is_py2 = _ver[0] == 2
is_py3 = _ver[0] == 3
if is_py2: # pragma: no cover
str = unicode # noqa
if is_py3: # pragma: no cover
str = str
ACRONYM_RE = re.compile(r"([A-Z]+)(?=[A-Z][a-z])")
PASCAL_RE = re.compile(r"([^\-_\s]+)")
SPLIT_RE = re.compile(r"([\-_\s]*[A-Z0-9]+[^A-Z\-_\s]+[\-_\s]*)")
UNDERSCORE_RE = re.compile(r"([^\-_\s])[\-_\s]+([^\-_\s])")
def pascalize(str_or_iter):
"""Convert a string, dict, or list of dicts to pascal case.
:param str_or_iter:
A string or iterable.
:type str_or_iter: Union[list, dict, str]
:rtype: Union[list, dict, str]
:returns:
pascalized string, dictionary, or list of dictionaries.
"""
if isinstance(str_or_iter, (list, Mapping)):
return _process_keys(str_or_iter, pascalize)
s = str(str_or_iter)
if s.isnumeric():
return str_or_iter
if s.isupper():
return str_or_iter
s = camelize(
PASCAL_RE.sub(lambda m: m.group(1)[0].upper() + m.group(1)[1:], s),
)
return s[0].upper() + s[1:]
def camelize(str_or_iter):
"""Convert a string, dict, or list of dicts to camel case.
:param str_or_iter:
A string or iterable.
:type str_or_iter: Union[list, dict, str]
:rtype: Union[list, dict, str]
:returns:
camelized string, dictionary, or list of dictionaries.
"""
if isinstance(str_or_iter, (list, Mapping)):
return _process_keys(str_or_iter, camelize)
s = str(str_or_iter)
if s.isnumeric():
return str_or_iter
if s.isupper():
return str_or_iter
return "".join(
[
s[0].lower() if not s[:2].isupper() else s[0],
UNDERSCORE_RE.sub(
lambda m: m.group(1) + m.group(2).upper(), s[1:]
),
]
)
def decamelize(str_or_iter):
"""Convert a string, dict, or list of dicts to snake case.
:param str_or_iter:
A string or iterable.
:type str_or_iter: Union[list, dict, str]
:rtype: Union[list, dict, str]
:returns:
snake cased string, dictionary, or list of dictionaries.
"""
if isinstance(str_or_iter, (list, Mapping)):
return _process_keys(str_or_iter, decamelize)
s = str(str_or_iter)
if s.isnumeric():
return str_or_iter
if s.isupper():
return str_or_iter
return separate_words(_fix_abbrevations(s)).lower()
def depascalize(str_or_iter):
"""Convert a string, dict, or list of dicts to snake case.
:param str_or_iter:
A string or iterable.
:type str_or_iter: Union[list, dict, str]
:rtype: Union[list, dict, str]
:returns:
snake cased string, dictionary, or list of dictionaries.
"""
return decamelize(str_or_iter)
def is_camelcase(str_or_iter):
"""Determine if a string, dict, or list of dicts is camel case.
:param str_or_iter:
A string or iterable.
:type str_or_iter: Union[list, dict, str]
:rtype: bool
:returns:
True/False whether string or iterable is camel case
"""
return str_or_iter == camelize(str_or_iter)
def is_pascalcase(str_or_iter):
"""Determine if a string, dict, or list of dicts is pascal case.
:param str_or_iter:
A string or iterable.
:type str_or_iter: Union[list, dict, str]
:rtype: bool
:returns:
True/False whether string or iterable is pascal case
"""
return str_or_iter == pascalize(str_or_iter)
def is_snakecase(str_or_iter):
"""Determine if a string, dict, or list of dicts is snake case.
:param str_or_iter:
A string or iterable.
:type str_or_iter: Union[list, dict, str]
:rtype: bool
:returns:
True/False whether string or iterable is snake case
"""
return str_or_iter == decamelize(str_or_iter)
def _process_keys(str_or_iter, fn):
if isinstance(str_or_iter, list):
return [_process_keys(k, fn) for k in str_or_iter]
elif isinstance(str_or_iter, Mapping):
return {fn(k): _process_keys(v, fn) for k, v in str_or_iter.items()}
else:
return str_or_iter
def _fix_abbrevations(string):
"""Rewrite incorrectly cased acronyms, initialisms, and abbrevations,
allowing them to be decamelized correctly. For example, given the string
"APIReponse", this function is responsible for ensuring the output is
"api_response" instead of "a_p_i_response".
:param str string:
A string that may contain an incorrectly cased abbrevation.
:rtype: str
:returns:
A rewritten string that is safe for decamelization.
"""
return ACRONYM_RE.sub(lambda m: m.group(0).title(), string)
def separate_words(string, separator="_", split=SPLIT_RE.split):
return separator.join(s for s in split(string) if s)