2022-10-14 14:53:06 +00:00
|
|
|
import re
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
from slugify import slugify
|
|
|
|
|
2022-10-14 14:53:06 +00:00
|
|
|
from benedict.core.rename import rename
|
|
|
|
from benedict.core.traverse import traverse
|
|
|
|
from benedict.utils import type_util
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
|
2020-02-06 14:18:01 +00:00
|
|
|
def _standardize_item(d, key, value):
|
|
|
|
if type_util.is_string(key):
|
|
|
|
# https://stackoverflow.com/a/12867228/2096218
|
2022-02-13 10:35:43 +00:00
|
|
|
norm_key = re.sub(r"((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))", r"_\1", key)
|
|
|
|
norm_key = slugify(norm_key, separator="_")
|
2020-02-06 14:18:01 +00:00
|
|
|
rename(d, key, norm_key)
|
|
|
|
|
|
|
|
|
2020-02-03 17:25:54 +00:00
|
|
|
def standardize(d):
|
2020-02-06 14:18:01 +00:00
|
|
|
traverse(d, _standardize_item)
|