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

benedict.core.standardize   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 14
dl 0
loc 23
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A _standardize_item() 0 7 2
A standardize() 0 2 1
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