Skip to main content

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

  1. 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"
  1. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

argcast-0.3.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

argcast-0.3.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

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

Hashes for argcast-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9e29cb64245b2f3c40424e512071b5579c2e2cc093159363cf5292db63196d23
MD5 53b45ee9c4b0a8b3458308a0c3f3908c
BLAKE2b-256 025e7e9c688515721fa9b84d225f3a61e016832c72ffcf34035c09f7af38db5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for argcast-0.3.0.tar.gz:

Publisher: release.yaml on bsdz/argcast

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for argcast-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f81d2b25c272ad7bb513702a7db04f028c923e8b27d3b78e1db6f54e2613250b
MD5 208d3ef3f2a76c95f49295d3744b2779
BLAKE2b-256 b917f7540327e631d4d5c80ec3d8aa81794cf625e43770e34bf87f9306585f93

See more details on using hashes here.

Provenance

The following attestation bundles were made for argcast-0.3.0-py3-none-any.whl:

Publisher: release.yaml on bsdz/argcast

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page