Completed
Push — master ( 74c8ad...722000 )
by Fabio
01:36
created

benedict.core.match   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 16
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Function

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