-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from ramonhagenaars/Release/1.4.0
Release/1.4.0
- Loading branch information
Showing
17 changed files
with
346 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
__title__ = 'nptyping' | ||
__version__ = '1.3.0' | ||
__version__ = '1.4.0' | ||
__author__ = 'Ramon Hagenaars' | ||
__author_email__ = '[email protected]' | ||
__description__ = 'Type hints for Numpy.' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import Any | ||
|
||
import numpy as np | ||
from typish import SubscriptableType | ||
|
||
from nptyping.types._nptype import NPType | ||
|
||
|
||
def is_structured_type(dtype: np.dtype) -> bool: | ||
"""Detect if the type is a structured array type.""" | ||
return hasattr(dtype, 'fields') and dtype.fields is not None | ||
|
||
|
||
class _StructuredTypeMeta(SubscriptableType): | ||
def __hash__(cls) -> int: | ||
return hash(cls.fields) | ||
|
||
def __repr__(cls) -> str: | ||
if cls.fields is not None: | ||
field_strs = ', '.join([str(f) for f in cls.fields]) | ||
return 'StructuredType[{}]'.format(field_strs) | ||
return 'StructuredType' | ||
|
||
def __eq__(cls, instance: Any) -> bool: | ||
if hasattr(instance, 'fields'): | ||
return (instance.fields == cls.fields | ||
or tuple(instance.fields) == tuple(cls.fields)) | ||
return False | ||
|
||
def __instancecheck__(cls, instance: Any) -> bool: | ||
from nptyping.functions._get_type import get_type | ||
|
||
return (is_structured_type(instance) | ||
and (cls == instance or cls == get_type(instance))) | ||
|
||
__str__ = __repr__ | ||
__subclasscheck__ = __eq__ | ||
|
||
|
||
class StructuredType(NPType, metaclass=_StructuredTypeMeta): | ||
""" | ||
Corresponds to the dtype of a structured NumPy array. | ||
""" | ||
fields = None | ||
|
||
@classmethod | ||
def _after_subscription(cls, args: Any) -> None: | ||
from nptyping.functions._get_type import get_type | ||
|
||
if isinstance(args, np.dtype): | ||
cls.fields = tuple(get_type(args.fields[n][0]) for n in args.names) | ||
elif isinstance(args, tuple): | ||
cls.fields = tuple(get_type(t) for t in args) | ||
elif isinstance(args, type): | ||
cls.fields = (get_type(args),) | ||
else: | ||
raise Exception( | ||
'Incompatible arguments to StructuredType: {}'.format(args)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Any | ||
|
||
import numpy as np | ||
from typish import SubscriptableType | ||
|
||
from nptyping.types._nptype import NPType | ||
|
||
|
||
def is_subarray_type(dtype: np.dtype) -> bool: | ||
"""Detect if the type is a subarray.""" | ||
return (hasattr(dtype, 'shape') | ||
and isinstance(dtype.shape, tuple) | ||
and len(dtype.shape) != 0) | ||
|
||
|
||
class _SubArrayTypeMeta(SubscriptableType): | ||
def __hash__(cls) -> int: | ||
return hash((cls.base, cls.shape)) | ||
|
||
def __repr__(cls) -> str: | ||
if cls.base is not None: | ||
return 'SubArrayType[{0}, {1}]'.format(cls.base, cls.shape) | ||
return 'SubArrayType' | ||
|
||
def __eq__(cls, instance: Any) -> bool: | ||
if hasattr(instance, 'base') and hasattr(instance, 'shape'): | ||
return instance.base == cls.base and instance.shape == cls.shape | ||
return False | ||
|
||
def __instancecheck__(cls, instance: Any) -> bool: | ||
from nptyping.functions._get_type import get_type | ||
|
||
if is_subarray_type(instance): | ||
if cls == instance: | ||
return True | ||
return cls == get_type(instance) | ||
return False | ||
|
||
__str__ = __repr__ | ||
__subclasscheck__ = __eq__ | ||
|
||
|
||
class SubArrayType(NPType, metaclass=_SubArrayTypeMeta): | ||
""" | ||
Corresponds to the dtype of a NumPy subarray. | ||
""" | ||
base = None | ||
shape = None | ||
|
||
@classmethod | ||
def _after_subscription(cls, args: Any) -> None: | ||
from nptyping.functions._get_type import get_type | ||
|
||
if isinstance(args, np.dtype): | ||
cls.base = get_type(args.base) | ||
cls.shape = args.shape | ||
elif isinstance(args, tuple): | ||
cls.base, cls.shape = args | ||
else: | ||
raise Exception( | ||
'Incompatible arguments to SubArrayType: {}'.format(args)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
[pylint] | ||
disable = too-many-instance-attributes,too-many-arguments | ||
,missing-module-docstring,too-few-public-methods,fixme | ||
,invalid-name,import-outside-toplevel,unidiomatic-typecheck | ||
,cyclic-import,duplicate-code | ||
disable = | ||
too-many-instance-attributes, | ||
too-many-arguments, | ||
missing-module-docstring, | ||
too-few-public-methods, | ||
fixme, | ||
invalid-name, | ||
import-outside-toplevel, | ||
unidiomatic-typecheck, | ||
cyclic-import, | ||
duplicate-code, | ||
unsubscriptable-object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.