Passed
Push — master ( db2481...3bd527 )
by Fabio
03:28
created

benedict.core.standardize   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 14
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A standardize() 0 9 2
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(d):
13
    def standardize_item(item_dict, item_key, item_value):
14
        if type_util.is_string(item_key):
15
            # https://stackoverflow.com/a/12867228/2096218
16
            norm_key = re.sub(
17
                r'((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))', r'_\1', item_key)
18
            norm_key = slugify(norm_key, separator='_')
19
            rename(item_dict, item_key, norm_key)
20
    traverse(d, standardize_item)