Passed
Push — master ( b3caf6...57589b )
by Fabio
02:22
created

benedict.core.standardize._standardize_item()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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