Passed
Push — dependabot/pip/flake8-bugbear-... ( 5c5892...6076c0 )
by
unknown
01:34
created

RegexMapParser.from_resource()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import re
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
from pathlib import Path
3
from typing import Mapping
4
5
from mandos.model import MandosResources
6
7
8
class RegexMap:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
9
    def __init__(self, dct: Mapping[re.Pattern, str]):
10
        self._dct = dct
11
12
    def get(self, s: str) -> str:
0 ignored issues
show
Coding Style Naming introduced by
Argument name "s" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
introduced by
Missing function or method docstring
Loading history...
13
        for pat, fixed in self._dct.items():
14
            sub = pat.sub(fixed, s)
15
            if sub != s:
16
                return sub
17
        return s
18
19
20
class RegexMapParser:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
21
    @classmethod
22
    def from_resource(cls, name: str):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
23
        return cls.from_path(MandosResources.path("mappings", name, suffix=".regexes"))
24
25
    @classmethod
26
    def from_path(cls, path: Path):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
27
        dct = {}
28
        for line in path.read_text(encoding="utf8").splitlines():
29
            if not line.startswith("#"):
30
                key, value = line.split("--->")
31
                key, value = key.strip(), value.strip()
32
                dct[re.compile(key)] = value
33
        return RegexMap(dct)
34
35
36
if __name__ == "__main__":
37
    mp = RegexMapParser.from_resource("@targets_neuro.regex")
38
    print(mp.get("Dopamine D3 receptor"))
39
40
41
__all__ = ["RegexMap", "RegexMapParser"]
42