How to use the nptyping.types._nptype.NPType function in nptyping

To help you get started, we’ve selected a few nptyping examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ramonhagenaars / nptyping / nptyping / types / _datetime64.py View on Github external
class _Datetime64Meta(type):
    def __repr__(cls):
        return 'Datetime64'

    __str__ = __repr__

    def __instancecheck__(cls, instance: Any) -> bool:
        from nptyping.functions._get_type import get_type_datetime64
        datetime_ = get_type_datetime64(instance)
        return issubclass(datetime_, cls)

    def __subclasscheck__(cls, subclass: type) -> bool:
        return Datetime64 in get_mro(subclass)


class Datetime64(NPType, numpy.datetime64, metaclass=_Datetime64Meta):
    """
    Corresponds to numpy.datetime64.
    """

    @classmethod
    def type_of(cls, obj: Any) -> Type['Bool']:
        """
        Return the NPType that corresponds to obj.
        :param obj: a string compatible object.
        :return: a Datetime64 type.
        """
        from nptyping.functions._get_type import get_type_datetime64
        return get_type_datetime64(obj)
github ramonhagenaars / nptyping / nptyping / types / _bool.py View on Github external
class _BoolMeta(type):
    def __repr__(cls):
        return 'Bool'

    __str__ = __repr__

    def __instancecheck__(cls, instance: Any) -> bool:
        from nptyping.functions._get_type import get_type_bool
        bool_ = get_type_bool(instance)
        return issubclass(bool_, cls)

    def __subclasscheck__(cls, subclass: type) -> bool:
        return Bool in get_mro(subclass)


class Bool(NPType, numpy.bool_, metaclass=_BoolMeta):
    """
    Corresponds to numpy.bool_.
    """

    @classmethod
    def type_of(cls, obj: Any) -> Type['Bool']:
        """
        Return the NPType that corresponds to obj.
        :param obj: a string compatible object.
        :return: a Unicode type.
        """
        from nptyping.functions._get_type import get_type_bool
        return get_type_bool(obj)
github ramonhagenaars / nptyping / nptyping / types / _object.py View on Github external
import numpy

from nptyping.types._nptype import NPType


class _ObjectMeta(type):
    def __repr__(cls):
        return 'Object'

    __str__ = __repr__


class Object(NPType, numpy.generic, metaclass=_ObjectMeta):
    """
    Corresponds to numpy.object.
github ramonhagenaars / nptyping / nptyping / functions / _get_type.py View on Github external
"""
    return Timedelta64


# Library private.
def get_type_complex(_: Any) -> Type[Complex128]:
    """
    Return the NPType that corresponds to obj.
    :param _: a complex128 compatible object.
    :return: a Complex128 type.
    """
    return Complex128


_delegates = [
    (NPType, lambda x: x),
    (type, _get_type_type),
    (bool, get_type_bool),
    (int, get_type_int),
    (float, get_type_float),
    (str, get_type_str),
    (complex, get_type_complex),
    (datetime, get_type_datetime64),
    (timedelta, get_type_timedelta64),
    (numpy.datetime64, get_type_datetime64),
    (numpy.timedelta64, get_type_timedelta64),
    (numpy.signedinteger, get_type_int),
    (numpy.unsignedinteger, get_type_uint),
    (numpy.floating, get_type_float),
    (numpy.bool_, get_type_bool),
    (numpy.dtype, _get_type_dtype),
    (numpy.ndarray, _get_type_arrary),
github ramonhagenaars / nptyping / nptyping / types / _timedelta64.py View on Github external
class _Timedelta64Meta(type):
    def __repr__(cls):
        return 'Timedelta64'

    __str__ = __repr__

    def __instancecheck__(cls, instance: Any) -> bool:
        from nptyping.functions._get_type import get_type_timedelta64
        timedelta_ = get_type_timedelta64(instance)
        return issubclass(timedelta_, cls)

    def __subclasscheck__(cls, subclass: type) -> bool:
        return Timedelta64 in get_mro(subclass)


class Timedelta64(NPType, numpy.timedelta64, metaclass=_Timedelta64Meta):
    """
    Corresponds to numpy.timedelta64.
    """

    @classmethod
    def type_of(cls, obj: Any) -> Type['Bool']:
        """
        Return the NPType that corresponds to obj.
        :param obj: a string compatible object.
        :return: a Timedelta64 type.
        """
        from nptyping.functions._get_type import get_type_timedelta64
        return get_type_timedelta64(obj)
github ramonhagenaars / nptyping / nptyping / types / _unicode.py View on Github external
def __instancecheck__(cls, instance: Any) -> bool:
        from nptyping.functions._get_type import get_type_str
        try:
            unicode = get_type_str(instance)
        except TypeError:
            return False
        return issubclass(unicode, cls)

    def __subclasscheck__(cls, subclass: type) -> bool:
        if Unicode in get_mro(subclass):
            return cls.chars is Any or subclass.chars <= cls.chars
        return False


class Unicode(NPType, numpy.unicode, metaclass=_UnicodeMeta):
    """
    A numpy unicode. Can be given the number of characters optionally.

    >>> Unicode[50]
    Unicode[50]
    """
    chars = Any
    _repr_args = None

    @classmethod
    def _after_subscription(cls, args: Any) -> None:
        cls.chars = int(args)
        cls._repr_args = int(args)

    @classmethod
    def type_of(cls, obj: Any) -> Type['Unicode']:
github ramonhagenaars / nptyping / nptyping / types / _number.py View on Github external
return issubclass(get_type(instance), cls)

    def __subclasscheck__(cls, subclass: type) -> bool:
        result = False
        if cls == subclass:
            result = True
        elif _is_a(subclass, Number):
            # Cover nptyping number types.
            result = _is_number_subclass_of(subclass, cls)
        elif _is_number_type(subclass):
            result = _is_numpy_or_python_type_subclass_of(subclass, cls)
        return result


class Number(NPType, metaclass=_NumberMeta):
    """
    Superclass for number types (integers and floating point numbers). Can be
    optionally given the number of bits.
    """
    base = None
    npbase = None
    _bits = None
    _repr_args = None

    @classmethod
    def _after_subscription(cls, args: Any) -> None:
        if isinstance(args, tuple):
            cls.base = args[0]
            cls.npbase = args[1]
            return