benedict.core.match   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 15
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A match() 0 14 3
1
import re
2
3
from benedict.core.keypaths import keypaths
4
from benedict.utils import type_util
5
6
7
def match(d, pattern, separator=".", indexes=True):
8
    if type_util.is_regex(pattern):
9
        regex = pattern
10
    elif type_util.is_string(pattern):
11
        # all indexes wildcard support
12
        pattern = re.sub(r"([\*]{1})", "(.)*", pattern)
13
        # escape square brackets
14
        pattern = re.sub(r"(\[([^\[\]]*)\])", "\\[\\g<2>\\]", pattern)
15
        regex = re.compile(pattern, flags=re.DOTALL)
16
    else:
17
        raise ValueError(f"Expected regex or string, found: {type(pattern)}")
18
    kps = keypaths(d, separator=separator, indexes=indexes)
19
    values = [d.get(kp) for kp in kps if regex.match(kp)]
20
    return values
21