Total Complexity | 7 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |