Passed
Push — main ( cee75c...37036d )
by Douglas
02:08
created

mandos.model.utils.MiscUtils.adjust_filename()   B

Complexity

Conditions 8

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 15
nop 4
dl 0
loc 16
rs 7.3333
c 0
b 0
f 0
1
import enum
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
from typing import Optional, Union
3
4
from mandos import logger
5
6
7
class TrueFalseUnknown(enum.Enum):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
8
    true = enum.auto()
9
    false = enum.auto()
10
    unknown = enum.auto()
11
12
    @classmethod
13
    def parse(cls, s: str):
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...
14
        tf_map = {
15
            "t": TrueFalseUnknown.true,
16
            "f": TrueFalseUnknown.false,
17
            "true": TrueFalseUnknown.true,
18
            "false": TrueFalseUnknown.false,
19
        }
20
        return tf_map.get(s.lower().strip(), TrueFalseUnknown.unknown)
21
22
23
class CleverEnum(enum.Enum):
24
    """
25
    An enum with a ``.of`` method that finds values
26
    with limited string/value fixing.
27
    May support an "unmatched" type -- a fallback value when there is no match.
28
    This is similar to pocketutils' simpler ``SmartEnum``.
29
    It is mainly useful for enums corresponding to concepts in ChEMBL and PubChem,
30
    where it's acceptable for the user to input spaces (like the database concepts use)
31
    rather than the underscores that Python requires.
32
    """
33
34
    @classmethod
35
    def _unmatched_type(cls) -> Optional[__qualname__]:
36
        return None
37
38
    @classmethod
39
    def or_none(cls, s: Union[int, str, __qualname__]) -> Optional[__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...
40
        try:
41
            return cls.of(s)
42
        except KeyError:
43
            return None
44
45
    @classmethod
46
    def of(cls, s: Union[int, str, __qualname__]) -> __qualname__:
0 ignored issues
show
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...
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...
47
        """
48
        Turns a string or int into this type.
49
        Case-insensitive. Replaces `` ``, ``.``, and ``-`` with ``_``.
50
        """
51
        if isinstance(s, cls):
52
            return s
53
        key = s.strip().replace(" ", "_").replace(".", "_").replace("-", "_").lower()
54
        try:
55
            if isinstance(s, str):
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
56
                return cls[key]
57
            elif isinstance(key, int):
58
                return cls(key)
59
            else:
60
                raise TypeError(f"Lookup type {type(s)} for value {s} not a str or int")
61
        except KeyError:
62
            unk = cls._unmatched_type()
63
            if unk is None:
64
                raise
65
            logger.error(f"Value {key} not found. Using {unk}")
66
            if not isinstance(unk, cls):
67
                raise AssertionError(f"Wrong type {type(unk)} (lookup: {s})")
68
            return unk
69
70
71
__all__ = ["TrueFalseUnknown", "CleverEnum"]
72