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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
__all__ = ['normalize',
'possible_json_keys',
'to_camel_case',
'to_pascal_case',
'to_lisp_case',
'to_snake_case',
'repl_or_with_union']
import re
from typing import Iterable, Dict, List
from ..type_def import JSONObject
def normalize(string: str) -> str:
"""
Normalize a string - typically a dataclass field name - for comparison
purposes.
"""
return string.replace('-', '').replace('_', '').upper()
def possible_json_keys(field: str) -> list[str]:
"""
Maps a dataclass field name to its possible keys in a JSON object.
This function checks multiple naming conventions (e.g., camelCase,
PascalCase, kebab-case, etc.) to find the matching key in the JSON
object `o`. It also caches the mapping for future use.
Args:
field (str): The dataclass field name to map.
Returns:
list[str]: The possible JSON keys for the given field.
"""
possible_keys = []
# `camelCase`
_key = to_camel_case(field)
possible_keys.append(_key)
# `PascalCase`: same as `camelCase` but first letter is capitalized
_key = _key[0].upper() + _key[1:]
possible_keys.append(_key)
# `kebab-case`
_key = to_lisp_case(field)
possible_keys.append(_key)
# `Upper-Kebab`: same as `kebab-case`, each word is title-cased
_key = _key.title()
possible_keys.append(_key)
# `Upper_Snake`
_key = _key.replace('-', '_')
possible_keys.append(_key)
# `snake_case`
_key = _key.lower()
possible_keys.append(_key)
# remove 1:1 field mapping from possible keys,
# as that's the first thing we check.
if field in possible_keys:
possible_keys.remove(field)
return possible_keys
def to_camel_case(string: str) -> str:
"""
Convert a string to Camel Case.
Examples::
>>> to_camel_case("device_type")
'deviceType'
"""
string = replace_multi_with_single(
string.replace('-', '_').replace(' ', '_'))
return string[0].lower() + re.sub(
r"(?:_)(.)", lambda m: m.group(1).upper(), string[1:])
def to_pascal_case(string):
"""
Converts a string to Pascal Case (also known as "Upper Camel Case")
Examples::
>>> to_pascal_case("device_type")
'DeviceType'
"""
string = replace_multi_with_single(
string.replace('-', '_').replace(' ', '_'))
return string[0].upper() + re.sub(
r"(?:_)(.)", lambda m: m.group(1).upper(), string[1:])
def to_lisp_case(string: str) -> str:
"""
Make a hyphenated, lowercase form from the expression in the string.
Example::
>>> to_lisp_case("DeviceType")
'device-type'
"""
string = string.replace('_', '-').replace(' ', '-')
# Short path: the field is already lower-cased, so we don't need to handle
# for camel or title case.
if string.islower():
return replace_multi_with_single(string, '-')
result = re.sub(
r'((?!^)(?<!-)[A-Z][a-z]+|(?<=[a-z0-9])[A-Z])', r'-\1', string)
return replace_multi_with_single(result.lower(), '-')
def to_snake_case(string: str) -> str:
"""
Make an underscored, lowercase form from the expression in the string.
Example::
>>> to_snake_case("DeviceType")
'device_type'
"""
string = string.replace('-', '_').replace(' ', '_')
# Short path: the field is already lower-cased, so we don't need to handle
# for camel or title case.
if string.islower():
return replace_multi_with_single(string)
result = re.sub(
r'((?!^)(?<!_)[A-Z][a-z]+|(?<=[a-z0-9])[A-Z])', r'_\1', string)
return replace_multi_with_single(result.lower())
def replace_multi_with_single(string: str, char='_') -> str:
"""
Replace multiple consecutive occurrences of `char` with a single one.
"""
rep = char + char
while rep in string:
string = string.replace(rep, char)
return string
# Note: this is the initial helper function I came up with. This doesn't use
# regex for the string transformation, so it's actually faster than the
# implementation above. However, I do prefer the implementation with regex,
# because its a lot cleaner and more simple than this implementation.
# def to_snake_case_old(string: str):
# """
# Make an underscored, lowercase form from the expression in the string.
# """
# if len(string) < 2:
# return string or ''
#
# string = string.replace('-', '_')
#
# if string.islower():
# return replace_multi_with_single(string)
#
# start_idx = 0
#
# parts = []
# for i, c in enumerate(string):
# c: str
# if c.isupper():
# try:
# next_lower = string[i + 1].islower()
# except IndexError:
# if string[i - 1].islower():
# parts.append(string[start_idx:i])
# parts.append(c)
# else:
# parts.append(string[start_idx:])
# break
# else:
# if i == 0:
# continue
#
# if string[i - 1].islower():
# parts.append(string[start_idx:i])
# start_idx = i
#
# elif next_lower:
# parts.append(string[start_idx:i])
# start_idx = i
# else:
# parts.append(string[start_idx:i + 1])
#
# result = '_'.join(parts).lower()
#
# return replace_multi_with_single(result)
# Constants
OPEN_BRACKET = '['
CLOSE_BRACKET = ']'
COMMA = ','
OR = '|'
# Replace any OR (|) characters in a forward-declared annotation (i.e. string)
# with a `typing.Union` declaration. See below article for more info.
#
# https://stackoverflow.com/q/69606986/10237506
def repl_or_with_union(s: str):
"""
Replace all occurrences of PEP 604- style annotations (i.e. like `X | Y`)
with the Union type from the `typing` module, i.e. like `Union[X, Y]`.
This is a recursive function that splits a complex annotation in order to
traverse and parse it, i.e. one that is declared as follows:
dict[str | Optional[int], list[list[str] | tuple[int | bool] | None]]
"""
return _repl_or_with_union_inner(s.replace(' ', ''))
def _repl_or_with_union_inner(s: str):
# If there is no '|' character in the annotation part, we just return it.
if OR not in s:
return s
# Checking for brackets like `List[int | str]`.
if OPEN_BRACKET in s:
# Get any indices of COMMA or OR outside a braced expression.
indices = _outer_comma_and_pipe_indices(s)
outer_commas = indices[COMMA]
outer_pipes = indices[OR]
# We need to check if there are any commas *outside* a bracketed
# expression. For example, the following cases are what we're looking
# for here:
# value[test], dict[str | int, tuple[bool, str]]
# dict[str | int, str], value[test]
# But we want to ignore cases like these, where all commas are nested
# within a bracketed expression:
# dict[str | int, Union[int, str]]
if outer_commas:
return COMMA.join(
[_repl_or_with_union_inner(i)
for i in _sub_strings(s, outer_commas)])
# We need to check if there are any pipes *outside* a bracketed
# expression. For example:
# value | dict[str | int, list[int | str]]
# dict[str, tuple[int | str]] | value
# But we want to ignore cases like these, where all pipes are
# nested within the a bracketed expression:
# dict[str | int, list[int | str]]
if outer_pipes:
or_parts = [_repl_or_with_union_inner(i)
for i in _sub_strings(s, outer_pipes)]
return f'Union{OPEN_BRACKET}{COMMA.join(or_parts)}{CLOSE_BRACKET}'
# At this point, we know that the annotation does not have an outer
# COMMA or PIPE expression. We also know that the following syntax
# is invalid: `SomeType[str][bool]`. Therefore, knowing this, we can
# assume there is only one outer start and end brace. For example,
# like `SomeType[str | int, list[dict[str, int | bool]]]`.
first_start_bracket = s.index(OPEN_BRACKET)
last_end_bracket = s.rindex(CLOSE_BRACKET)
# Replace the value enclosed in the outermost brackets
bracketed_val = _repl_or_with_union_inner(
s[first_start_bracket + 1:last_end_bracket])
start_val = s[:first_start_bracket]
end_val = s[last_end_bracket + 1:]
return f'{start_val}{OPEN_BRACKET}{bracketed_val}{CLOSE_BRACKET}{end_val}'
elif COMMA in s:
# We are dealing with a string like `int | str, float | None`
return COMMA.join([_repl_or_with_union_inner(i)
for i in s.split(COMMA)])
# We are dealing with a string like `int | str`
return f'Union{OPEN_BRACKET}{s.replace(OR, COMMA)}{CLOSE_BRACKET}'
def _sub_strings(s: str, split_indices: Iterable[int]):
"""Split a string on the specified indices, and return the split parts."""
prev = -1
for idx in split_indices:
yield s[prev+1:idx]
prev = idx
yield s[prev+1:]
def _outer_comma_and_pipe_indices(s: str) -> Dict[str, List[int]]:
"""Return any indices of ',' and '|' that are outside of braces."""
indices = {OR: [], COMMA: []}
brace_dict = {OPEN_BRACKET: 1, CLOSE_BRACKET: -1}
brace_count = 0
for i, char in enumerate(s):
if char in brace_dict:
brace_count += brace_dict[char]
elif not brace_count and char in indices:
indices[char].append(i)
return indices
|