https://github.com/ctgk/pyautocast
Python library to automatically cast function arguments using decorator.
https://github.com/ctgk/pyautocast
Last synced: 2 months ago
JSON representation
Python library to automatically cast function arguments using decorator.
- Host: GitHub
- URL: https://github.com/ctgk/pyautocast
- Owner: ctgk
- License: mit
- Created: 2020-04-03T10:36:33.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-03T17:30:52.000Z (about 5 years ago)
- Last Synced: 2025-02-12T15:18:03.193Z (3 months ago)
- Language: Python
- Size: 15.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pyautocast
Python library to automatically cast function arguments using decorator.## Install
```bash
pip install pyautocast
```## Usage
```py
>>> from pyautocast import autocast
>>> @autocast(x=str)
... def func(x):
... assert(isinstance(x, str))
... return "arg 'x' in func() is " + x
...
>>> func(2)
"arg 'x' in func() is 2"
>>> func([1, 2, 3])
"arg 'x' in func() is [1, 2, 3]"
``````py
>>> from pyautocast import CustomCast
>>> mycast = CustomCast()
>>> mycast.add_cast_rule(int, tuple, lambda x: (x, x))
>>> @mycast.autocast(x=tuple)
... def func(x):
... print(x)
>>> func(2)
(2, 2)
>>> func(-4.5)
Traceback (most recent call last):
...
TypeError: 'float' object is not iterable
```