Coder Social home page Coder Social logo

nptyping's Introduction

PyPI version Build Status

nptyping

Type hints for Numpy!

Installation

pip install nptyping

Usage

Use the nptyping type hints like the regular type hints from typing:

from nptyping import Array


def func1(arr: Array[int]):  # A numpy.ndarray that contains numbers
    ...

You can also define the shape of an array:

Array[str, 3, 2]    # 3 rows and 2 columns
Array[str, 3]       # 3 rows and an undefined number of columns
Array[str, 3, ...]  # 3 rows and an undefined number of columns
Array[str, ..., 2]  # an undefined number of rows and 2 columns

Heterogeneous arrays are supported as well:

Array[int, float, str]       # int, float and str on columns 1, 2 and 3 resp.
Array[int, float, str, ...]  # int, float and str on columns 1, 2 and 3 resp.
Array[int, float, str, 3]    # 3 rows and int, float and str on columns 1, 2 and 3 resp.

nptyping also supports instance checks:

import numpy
from nptyping import Array


arr = numpy.array([[1, 2],
                   [3, 4],
                   [5, 6]])

isinstance(arr, Array[int, 3, 2])    # True
isinstance(arr, Array[str, 3, 2])    # False
isinstance(arr, Array[int, 3, ...])  # True
isinstance(arr, Array[int, 3, 6])    # False

Also for heterogeneous arrays:

import numpy
from nptyping import Array


arr = np.array([(1, 2.0, '3'),
                (4, 5.0, '6')],
               dtype=[('a', int), ('b', float), ('c', str)])

isinstance(arr, Array[int, float, str])     # True
isinstance(arr, Array[float, float, str])   # False
isinstance(arr, Array[int, float, str, 2])  # True

nptyping's People

Contributors

ramonhagenaars avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.