Automatically cast function parameters at run time based on their types.
Project description
argcast - Run time function argument caster
Automatically cast function parameters at run time based on their types.
Installation
pip install argcast
Usage
Below is an example usage:
from decimal import Decimal
from enum import Enum
import numpy as np
import pandas as pd
from argcast import coerce_params
class MatrixOp(Enum):
INVERSE = 1
TRANSPOSE = 2
@classmethod
def get(cls, name):
return cls[name] if isinstance(name, str) else cls(name)
coerce = coerce_params({np.ndarray: np.array, MatrixOp: MatrixOp.get})
@coerce
def f(
a: np.ndarray, b: np.ndarray, k: np.float64, b_trans: MatrixOp
) -> pd.DataFrame:
if b_trans == MatrixOp.INVERSE:
b = np.linalg.inv(b)
elif b_trans == MatrixOp.TRANSPOSE:
b = b.T
return k * a @ b
f([[1, 2], [3, 4]], [[5, 6], [7, 8]], Decimal("2.0"), "TRANSPOSE")
# returns pd.DataFrame([[34.0, 46.0], [78.0, 106.0]])
Limitations
- Using this decorator will confuse type checkers like mypy and you might wish to place a top level comment in your module like:
# mypy: disable-error-code="return-value"
- Although basic sequence and mapping types are supported, ie list|tuple|set[str|int|...] or dict[..., ...], many other types aren't (eg nested types list[list[...]] etc).
Development
Before commit run following format commands in project folder:
poetry run black .
poetry run isort . --profile black
poetry run docformatter . --recursive --in-place --black
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file argcast-0.3.0.tar.gz.
File metadata
- Download URL: argcast-0.3.0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e29cb64245b2f3c40424e512071b5579c2e2cc093159363cf5292db63196d23
|
|
| MD5 |
53b45ee9c4b0a8b3458308a0c3f3908c
|
|
| BLAKE2b-256 |
025e7e9c688515721fa9b84d225f3a61e016832c72ffcf34035c09f7af38db5f
|
Provenance
The following attestation bundles were made for argcast-0.3.0.tar.gz:
Publisher:
release.yaml on bsdz/argcast
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
argcast-0.3.0.tar.gz -
Subject digest:
9e29cb64245b2f3c40424e512071b5579c2e2cc093159363cf5292db63196d23 - Sigstore transparency entry: 219851762
- Sigstore integration time:
-
Permalink:
bsdz/argcast@e83f4fd9405f6e2ce7f5411357854672cc533077 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/bsdz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@e83f4fd9405f6e2ce7f5411357854672cc533077 -
Trigger Event:
release
-
Statement type:
File details
Details for the file argcast-0.3.0-py3-none-any.whl.
File metadata
- Download URL: argcast-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f81d2b25c272ad7bb513702a7db04f028c923e8b27d3b78e1db6f54e2613250b
|
|
| MD5 |
208d3ef3f2a76c95f49295d3744b2779
|
|
| BLAKE2b-256 |
b917f7540327e631d4d5c80ec3d8aa81794cf625e43770e34bf87f9306585f93
|
Provenance
The following attestation bundles were made for argcast-0.3.0-py3-none-any.whl:
Publisher:
release.yaml on bsdz/argcast
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
argcast-0.3.0-py3-none-any.whl -
Subject digest:
f81d2b25c272ad7bb513702a7db04f028c923e8b27d3b78e1db6f54e2613250b - Sigstore transparency entry: 219851767
- Sigstore integration time:
-
Permalink:
bsdz/argcast@e83f4fd9405f6e2ce7f5411357854672cc533077 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/bsdz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@e83f4fd9405f6e2ce7f5411357854672cc533077 -
Trigger Event:
release
-
Statement type: