nptyping.types._unicode   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 79.63 %

Importance

Changes 0
Metric Value
wmc 8
eloc 32
dl 43
loc 54
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _UnicodeMeta.__subclasscheck__() 4 4 2
A _UnicodeMeta.__eq__() 2 2 1
A _UnicodeMeta.__hash__() 2 2 1
A _UnicodeMeta.__instancecheck__() 7 7 2
A Unicode.type_of() 9 9 1
A Unicode._after_subscription() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from inspect import getmro
2
from typing import Any, Type
3
4
import numpy as np
5
6
from nptyping.types._nptype import NPType, SimpleNPTypeMeta
7
8
9 View Code Duplication
class _UnicodeMeta(SimpleNPTypeMeta):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
10
    def __eq__(cls, other: Any) -> bool:
11
        return hash(cls) == hash(other)
12
13
    def __hash__(cls: Any) -> int:
14
        return hash(cls.__name__) * hash(cls.chars)
15
16
    def __instancecheck__(cls, instance: Any) -> bool:
17
        from nptyping.functions._get_type import get_type_str
18
        try:
19
            unicode = get_type_str(instance)
20
        except TypeError:
21
            return False
22
        return issubclass(unicode, cls)
23
24
    def __subclasscheck__(cls, subclass: type) -> bool:
25
        if Unicode in getmro(subclass):
26
            return cls.chars is Any or subclass.chars <= cls.chars
27
        return False
28
29
30 View Code Duplication
class Unicode(NPType, np.compat.unicode, metaclass=_UnicodeMeta):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
31
    """
32
    A numpy unicode. Can be given the number of characters optionally.
33
34
    >>> Unicode[50]
35
    Unicode[50]
36
    """
37
    chars = Any
38
    _repr_args = None
39
40
    @classmethod
41
    def _after_subscription(cls, args: Any) -> None:
42
        cls.chars = int(args)
43
        cls._repr_args = int(args)
44
45
    @classmethod
46
    def type_of(cls, obj: Any) -> Type['Unicode']:
47
        """
48
        Return the NPType that corresponds to obj.
49
        :param obj: a string compatible object.
50
        :return: a Unicode type.
51
        """
52
        from nptyping.functions._get_type import get_type_str
53
        return get_type_str(obj)
54