python-benedict/benedict/core/standardize.py

20 lines
515 B
Python
Raw Normal View History

import re
from slugify import slugify
from benedict.core.rename import rename
from benedict.core.traverse import traverse
from benedict.utils import type_util
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)
def standardize(d):
2020-02-06 14:18:01 +00:00
traverse(d, _standardize_item)