benedict.core.standardize._standardize_item()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nop 3
1
import re
2
3
from slugify import slugify
4
5
from benedict.core.rename import rename
6
from benedict.core.traverse import traverse
7
from benedict.utils import type_util
8
9
10
def _standardize_item(d, key, value):
11
    if type_util.is_string(key):
12
        # https://stackoverflow.com/a/12867228/2096218
13
        norm_key = re.sub(r"((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))", r"_\1", key)
14
        norm_key = slugify(norm_key, separator="_")
15
        rename(d, key, norm_key)
16
17
18
def standardize(d):
19
    traverse(d, _standardize_item)
20