Passed
Push — master ( b636f8...bb23a2 )
by Max
56s
created

structured_data._patterns.bind   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Bind.structure() 0 3 1
A Bind.bindings() 0 3 1
A Bind.destructure() 0 7 4
A Bind.__new__() 0 4 1
1
from .._not_in import not_in
2
from .basic_patterns import Pattern
3
from .compound_match import CompoundMatch
4
5
6
class Bind(CompoundMatch, tuple):
7
    """A wrapper that adds additional bindings to a successful match."""
8
9
    __slots__ = ()
10
11
    def __new__(*args, **kwargs):
12
        cls, structure = args
13
        not_in(kwargs, "_")
14
        return super(Bind, cls).__new__(cls, (structure, tuple(kwargs.items())))
15
16
    @property
17
    def structure(self):
18
        return self[0]
19
20
    @property
21
    def bindings(self):
22
        return self[1]
23
24
    def destructure(self, value):
25
        if value is self:
26
            return [Pattern(name) for (name, _) in reversed(self.bindings)] + [
27
                self.structure
28
            ]
29
        return [binding_value for (_, binding_value) in reversed(self.bindings)] + [
30
            value
31
        ]
32