nptyping


Namenptyping JSON
Version 2.4.1 PyPI version JSON
download
home_pagehttps://github.com/ramonhagenaars/nptyping
SummaryType hints for NumPy.
upload_time2022-11-16 20:20:43
maintainer
docs_urlNone
authorRamon Hagenaars
requires_python>=3.7
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI version](https://img.shields.io/pypi/pyversions/nptyping.svg)](https://img.shields.io/pypi/pyversions/nptyping.svg)
[![Downloads](https://pepy.tech/badge/nptyping/month)](https://pepy.tech/project/nptyping)
[![PyPI version](https://badge.fury.io/py/nptyping.svg)](https://badge.fury.io/py/nptyping)
[![codecov](https://codecov.io/gh/ramonhagenaars/nptyping/branch/master/graph/badge.svg)](https://codecov.io/gh/ramonhagenaars/nptyping)
[![Code style](https://img.shields.io/badge/code%20style-black-black)](https://img.shields.io/badge/code%20style-black-black)


<p align='center'>
  <a href='https://https://pypi.org/project/nptyping/'>
    <img src='https://github.com/ramonhagenaars/nptyping/raw/master/resources/logo.png' />
  </a> 
</p>

💡 *Type hints for `NumPy`* <br/>
💡 *Type hints for `pandas.DataFrame`* <br/>
💡 *Extensive dynamic type checks for dtypes shapes and structures* <br/>

Example of a hinted `numpy.ndarray`:

```python
>>> from nptyping import NDArray, Int, Shape

>>> arr: NDArray[Shape["2, 2"], Int]

```

Example of a hinted `pandas.DataFrame`:

```python
>>> from nptyping import DataFrame, Structure as S

>>> df: DataFrame[S["name: Str, x: Float, y: Float"]]

```
⚠️`pandas.DataFrame` is not yet supported on Python 3.11.

### Installation

| Command                          | Description                                               |
|:---------------------------------|-----------------------------------------------------------|
| `pip install nptyping`           | Install the basics                                        |
| `pip install nptyping[pandas]`   | Install with pandas extension (⚠️Python 3.10 or lower)  |
| `pip install nptyping[complete]` | Install with all extensions                               |

### Instance checking

Example of instance checking:
```python
>>> import numpy as np

>>> isinstance(np.array([[1, 2], [3, 4]]), NDArray[Shape["2, 2"], Int])
True

>>> isinstance(np.array([[1., 2.], [3., 4.]]), NDArray[Shape["2, 2"], Int])
False

>>> isinstance(np.array([1, 2, 3, 4]), NDArray[Shape["2, 2"], Int])
False

```

`nptyping` also provides `assert_isinstance`. In contrast to `assert isinstance(...)`, this won't cause IDEs or MyPy
complaints. Here is an example: 
```python
>>> from nptyping import assert_isinstance

>>> assert_isinstance(np.array([1]), NDArray[Shape["1"], Int])
True

```

### NumPy Structured arrays

You can also express structured arrays using `nptyping.Structure`:
```python
>>> from nptyping import Structure

>>> Structure["name: Str, age: Int"]
Structure['age: Int, name: Str']

```

Here is an example to see it in action:
```python
>>> from typing import Any
>>> import numpy as np
>>> from nptyping import NDArray, Structure

>>> arr = np.array([("Peter", 34)], dtype=[("name", "U10"), ("age", "i4")])
>>> isinstance(arr, NDArray[Any, Structure["name: Str, age: Int"]])
True

```

Subarrays can be expressed with a shape expression between square brackets:
```python
>>> Structure["name: Int[3, 3]"]
Structure['name: Int[3, 3]']

```

### NumPy Record arrays
The recarray is a specialization of a structured array. You can use `RecArray`
to express them.

```python
>>> from nptyping import RecArray

>>> arr = np.array([("Peter", 34)], dtype=[("name", "U10"), ("age", "i4")])
>>> rec_arr = arr.view(np.recarray)
>>> isinstance(rec_arr, RecArray[Any, Structure["name: Str, age: Int"]])
True

```

### Pandas DataFrames
Pandas DataFrames can be expressed with `Structure` also. To make it more concise, you may want to alias `Structure`.
```python
>>> from nptyping import DataFrame, Structure as S

>>> df: DataFrame[S["x: Float, y: Float"]]

```

### More examples

Here is an example of a rich expression that can be done with `nptyping`:
```python
def plan_route(
        locations: NDArray[Shape["[from, to], [x, y]"], Float]
) -> NDArray[Shape["* stops, [x, y]"], Float]:
    ...

```

More examples can be found in the [documentation](https://github.com/ramonhagenaars/nptyping/blob/master/USERDOCS.md#Examples).

## Documentation

* [User documentation](https://github.com/ramonhagenaars/nptyping/blob/master/USERDOCS.md) <br/>
The place to go if you are using this library. <br/><br/>
  
* [Release notes](https://github.com/ramonhagenaars/nptyping/blob/master/HISTORY.md) <br/>
To see what's new, check out the release notes. <br/><br/>

* [Contributing](https://github.com/ramonhagenaars/nptyping/blob/master/CONTRIBUTING.md) <br/>
If you're interested in developing along, find the guidelines here. <br/><br/>

* [License](https://github.com/ramonhagenaars/nptyping/blob/master/LICENSE) <br/>
If you want to check out how open source this library is.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ramonhagenaars/nptyping",
    "name": "nptyping",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "Ramon Hagenaars",
    "author_email": "ramon.hagenaars@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/65/99/fbc6c585dfef7803886a137820cc557e54472dcbf1c2ef34e033640964e9/nptyping-2.4.1.tar.gz",
    "platform": null,
    "description": "[![PyPI version](https://img.shields.io/pypi/pyversions/nptyping.svg)](https://img.shields.io/pypi/pyversions/nptyping.svg)\n[![Downloads](https://pepy.tech/badge/nptyping/month)](https://pepy.tech/project/nptyping)\n[![PyPI version](https://badge.fury.io/py/nptyping.svg)](https://badge.fury.io/py/nptyping)\n[![codecov](https://codecov.io/gh/ramonhagenaars/nptyping/branch/master/graph/badge.svg)](https://codecov.io/gh/ramonhagenaars/nptyping)\n[![Code style](https://img.shields.io/badge/code%20style-black-black)](https://img.shields.io/badge/code%20style-black-black)\n\n\n<p align='center'>\n  <a href='https://https://pypi.org/project/nptyping/'>\n    <img src='https://github.com/ramonhagenaars/nptyping/raw/master/resources/logo.png' />\n  </a> \n</p>\n\n\ud83d\udca1 *Type hints for `NumPy`* <br/>\n\ud83d\udca1 *Type hints for `pandas.DataFrame`* <br/>\n\ud83d\udca1 *Extensive dynamic type checks for dtypes shapes and structures* <br/>\n\nExample of a hinted `numpy.ndarray`:\n\n```python\n>>> from nptyping import NDArray, Int, Shape\n\n>>> arr: NDArray[Shape[\"2, 2\"], Int]\n\n```\n\nExample of a hinted `pandas.DataFrame`:\n\n```python\n>>> from nptyping import DataFrame, Structure as S\n\n>>> df: DataFrame[S[\"name: Str, x: Float, y: Float\"]]\n\n```\n\u26a0\ufe0f`pandas.DataFrame` is not yet supported on Python 3.11.\n\n### Installation\n\n| Command                          | Description                                               |\n|:---------------------------------|-----------------------------------------------------------|\n| `pip install nptyping`           | Install the basics                                        |\n| `pip install nptyping[pandas]`   | Install with pandas extension (\u26a0\ufe0fPython 3.10 or lower)  |\n| `pip install nptyping[complete]` | Install with all extensions                               |\n\n### Instance checking\n\nExample of instance checking:\n```python\n>>> import numpy as np\n\n>>> isinstance(np.array([[1, 2], [3, 4]]), NDArray[Shape[\"2, 2\"], Int])\nTrue\n\n>>> isinstance(np.array([[1., 2.], [3., 4.]]), NDArray[Shape[\"2, 2\"], Int])\nFalse\n\n>>> isinstance(np.array([1, 2, 3, 4]), NDArray[Shape[\"2, 2\"], Int])\nFalse\n\n```\n\n`nptyping` also provides `assert_isinstance`. In contrast to `assert isinstance(...)`, this won't cause IDEs or MyPy\ncomplaints. Here is an example: \n```python\n>>> from nptyping import assert_isinstance\n\n>>> assert_isinstance(np.array([1]), NDArray[Shape[\"1\"], Int])\nTrue\n\n```\n\n### NumPy Structured arrays\n\nYou can also express structured arrays using `nptyping.Structure`:\n```python\n>>> from nptyping import Structure\n\n>>> Structure[\"name: Str, age: Int\"]\nStructure['age: Int, name: Str']\n\n```\n\nHere is an example to see it in action:\n```python\n>>> from typing import Any\n>>> import numpy as np\n>>> from nptyping import NDArray, Structure\n\n>>> arr = np.array([(\"Peter\", 34)], dtype=[(\"name\", \"U10\"), (\"age\", \"i4\")])\n>>> isinstance(arr, NDArray[Any, Structure[\"name: Str, age: Int\"]])\nTrue\n\n```\n\nSubarrays can be expressed with a shape expression between square brackets:\n```python\n>>> Structure[\"name: Int[3, 3]\"]\nStructure['name: Int[3, 3]']\n\n```\n\n### NumPy Record arrays\nThe recarray is a specialization of a structured array. You can use `RecArray`\nto express them.\n\n```python\n>>> from nptyping import RecArray\n\n>>> arr = np.array([(\"Peter\", 34)], dtype=[(\"name\", \"U10\"), (\"age\", \"i4\")])\n>>> rec_arr = arr.view(np.recarray)\n>>> isinstance(rec_arr, RecArray[Any, Structure[\"name: Str, age: Int\"]])\nTrue\n\n```\n\n### Pandas DataFrames\nPandas DataFrames can be expressed with `Structure` also. To make it more concise, you may want to alias `Structure`.\n```python\n>>> from nptyping import DataFrame, Structure as S\n\n>>> df: DataFrame[S[\"x: Float, y: Float\"]]\n\n```\n\n### More examples\n\nHere is an example of a rich expression that can be done with `nptyping`:\n```python\ndef plan_route(\n        locations: NDArray[Shape[\"[from, to], [x, y]\"], Float]\n) -> NDArray[Shape[\"* stops, [x, y]\"], Float]:\n    ...\n\n```\n\nMore examples can be found in the [documentation](https://github.com/ramonhagenaars/nptyping/blob/master/USERDOCS.md#Examples).\n\n## Documentation\n\n* [User documentation](https://github.com/ramonhagenaars/nptyping/blob/master/USERDOCS.md) <br/>\nThe place to go if you are using this library. <br/><br/>\n  \n* [Release notes](https://github.com/ramonhagenaars/nptyping/blob/master/HISTORY.md) <br/>\nTo see what's new, check out the release notes. <br/><br/>\n\n* [Contributing](https://github.com/ramonhagenaars/nptyping/blob/master/CONTRIBUTING.md) <br/>\nIf you're interested in developing along, find the guidelines here. <br/><br/>\n\n* [License](https://github.com/ramonhagenaars/nptyping/blob/master/LICENSE) <br/>\nIf you want to check out how open source this library is.\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Type hints for NumPy.",
    "version": "2.4.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "e5f67438f6672f54730e04b3e1c8df3b",
                "sha256": "23e8164b1e2c55e872f392ca7516b9b1b0cb400b03b70accaa63998b4106b0b3"
            },
            "downloads": -1,
            "filename": "nptyping-2.4.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e5f67438f6672f54730e04b3e1c8df3b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 36003,
            "upload_time": "2022-11-16T20:20:40",
            "upload_time_iso_8601": "2022-11-16T20:20:40.921507Z",
            "url": "https://files.pythonhosted.org/packages/b2/c1/e6f8c5f28f9b3bdb5c9c1d349a51941a30f90347b82bd5594363e81cf3ff/nptyping-2.4.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "1c239c9afa3371131871b3f186a52a0b",
                "sha256": "57ba684ee5fc5eb681ee04270ee94adb879e4372ce6b640defa08ace8e1df295"
            },
            "downloads": -1,
            "filename": "nptyping-2.4.1.tar.gz",
            "has_sig": false,
            "md5_digest": "1c239c9afa3371131871b3f186a52a0b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 20055,
            "upload_time": "2022-11-16T20:20:43",
            "upload_time_iso_8601": "2022-11-16T20:20:43.242205Z",
            "url": "https://files.pythonhosted.org/packages/65/99/fbc6c585dfef7803886a137820cc557e54472dcbf1c2ef34e033640964e9/nptyping-2.4.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-11-16 20:20:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "ramonhagenaars",
    "github_project": "nptyping",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "nptyping"
}
        
Elapsed time: 0.03243s