Code Monkey home page Code Monkey logo

adtypingdecorators's Introduction

doc License: GPL v3

Status

pytests push-pypi push-doc

maintained issues pr

Compatibilities

ubuntu unix

python

Contact

linkedin website mail

AdTypingDecorators

Python decorators allowing to check and/or enforce types in functions' arguments based on typing hints.

Installation

pip install adtypingdecorators

Usage

import numpy as np
import pandas as pd
from adtypingdecorators import typing_raise, typing_convert, typing_warn, typing_custom


def to_array(a: int):
    return np.array([a, 2 * a])


def to_array_2(a: int):
    return np.array([a, 3 * a])


@typing_raise
def f_raise(a: int):
    return a + 1


@typing_convert
def f_convert(a: int):
    return a + 1


@typing_warn
def f_warn(a: int):
    return a + 1


@typing_custom(
    convertors={int: to_array, "b": to_array_2},
    exclude=["c", pd.DataFrame]
)
def f_custom(a: np.ndarray, b: np.ndarray, c: np.ndarray, d: np.ndarray):
    return a + 1, b + 1, c + 1, d + 1


f_raise(1)  # Returns 2, as expected
# noinspection PyTypeChecker
f_raise(1.5)  # Raises TypeError

# noinspection PyTypeChecker
f_convert(1.5)  # Returns 2 (converted 1.5 into 1)
# noinspection PyTypeChecker
f_convert("foo")  # Raises ValueError (while trying to convert 'foo' to interger)

# noinspection PyTypeChecker
f_warn(1.5)  # Returns 2.5, and warns

# noinspection PyTypeChecker
a_, b_, c_, d_ = f_custom(1, 2, 3, pd.DataFrame([4]))
# a_ is np.array([2, 3])
# b_ is np.array([3, 7])
# c_ is 4
# d_ is pd.DataFrame([5])

adtypingdecorators's People

Stargazers

Christian Ledermann avatar

Watchers

Christophe Geissler 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.