Passed
Push — master ( deac8e...af9889 )
by Max
01:03
created

structured_data._matchable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 25
dl 0
loc 46
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A Matchable.__bool__() 0 2 1
A Matchable.match() 0 7 2
A Matchable.__getitem__() 0 4 2
A Matchable.__call__() 0 2 1
A Matchable.__init__() 0 3 1
1
from __future__ import annotations
2
3
import typing
4
5
from . import _match_dict
6
from . import _match_failure
7
8
9
class Matchable:
10
    """Given a value, attempt to match against a target.
11
12
    The truthiness of ``Matchable`` values varies on whether they have bindings
13
    associated with them. They are truthy exactly when they have bindings.
14
15
    ``Matchable`` values provide two basic forms of syntactic sugar.
16
    ``m_able(target)`` is equivalent to ``m_able.match(target)``, and
17
    ``m_able[k]`` will return ``m_able.matches[k]`` if the ``Matchable`` is
18
    truthy, and raise a ``ValueError`` otherwise.
19
    """
20
21
    value: typing.Any
22
    matches: typing.Optional[_match_dict.MatchDict]
23
24
    def __init__(self, value: typing.Any):
25
        self.value = value
26
        self.matches = None
27
28
    def match(self, target) -> Matchable:
29
        """Match against target, generating a set of bindings."""
30
        try:
31
            self.matches = _match_dict.match(target, self.value)
32
        except _match_failure.MatchFailure:
33
            self.matches = None
34
        return self
35
36
    def __call__(self, target) -> Matchable:
37
        return self.match(target)
38
39
    def __getitem__(self, key):
40
        if self.matches is None:
41
            raise ValueError
42
        return self.matches[key]
43
44
    def __bool__(self):
45
        return self.matches is not None
46