Total Complexity | 8 |
Total Lines | 54 |
Duplicated Lines | 79.63 % |
Changes | 0 |
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): |
|
|
|||
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): |
|
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 |