Passed
Push — dependabot/pip/flake8-bugbear-... ( 22089b...82a4d5 )
by
unknown
01:29
created

mandos.model.CleverEnum.of()   B

Complexity

Conditions 6

Size

Total Lines 18
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nop 2
dl 0
loc 18
rs 8.6166
c 0
b 0
f 0
1
from __future__ import annotations
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
3
import abc
4
import logging
5
import enum
6
from pathlib import Path
7
from typing import Optional, Union
8
9
from pocketutils.core.dot_dict import NestedDotDict
0 ignored issues
show
introduced by
Unable to import 'pocketutils.core.dot_dict'
Loading history...
10
11
logger = logging.getLogger("logger")
12
13
14
class CompoundNotFoundError(LookupError):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
15
    """"""
16
17
18
@enum.unique
19
class CleverEnum(enum.Enum, metaclass=abc.ABCMeta):
20
    """
21
    An enum with a ``.of`` method that finds values
22
    with limited string/value fixing.
23
    May support an "unmatched" type -- a fallback value when there is no match.
24
    This is similar to pocketutils' simpler ``SmartEnum``.
25
    It is mainly useful for enums corresponding to concepts in ChEMBL and PubChem,
26
    where it's acceptable for the user to input spaces (like the database concepts use)
27
    rather than the underscores that Python requires.
28
    """
29
30
    @classmethod
31
    def _unmatched_type(cls) -> Optional[__qualname__]:
32
        return None
33
34
    @classmethod
35
    def of(cls, s: Union[int, str]) -> __qualname__:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
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...
Coding Style Naming introduced by
Method name "of" 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...
36
        key = s.replace(" ", "_").replace("-", "_").lower()
37
        try:
38
            if isinstance(s, str):
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
39
                return cls[key]
40
            elif isinstance(key, int):
41
                return cls(key)
42
            else:
43
                raise TypeError(f"Lookup type {type(s)} for value {s} not a str or int")
44
        except KeyError:
45
            unk = cls._unmatched_type()
46
            if unk is None:
47
                raise
48
            logger.error(f"Target type {key} not found. Using TargetType.unknown.")
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
49
            if not isinstance(unk, cls):
50
                raise AssertionError(f"Wrong type {type(unk)} (lookup: {s})")
51
            return unk
52
53
54
class MandosResources:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
55
    @classmethod
56
    def contains(cls, *nodes: Union[Path, str], suffix: Optional[str] = None) -> bool:
57
        """Returns whether a resource file (or dir) exists."""
58
        return cls.path(*nodes, suffix=suffix).exists()
59
60
    @classmethod
61
    def path(cls, *nodes: Union[Path, str], suffix: Optional[str] = None) -> Path:
62
        """Gets a path of a test resource file under resources/."""
63
        path = Path(Path(__file__).parent.parent, "resources", *nodes)
64
        return path.with_suffix(path.suffix if suffix is None else suffix)
65
66
    @classmethod
67
    def json(cls, *nodes: Union[Path, str], suffix: Optional[str] = None) -> NestedDotDict:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
68
        return NestedDotDict.read_json(cls.path(*nodes, suffix=suffix))
69
70
71
__all__ = ["CompoundNotFoundError", "MandosResources", "CleverEnum"]
72