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]])

Warning

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"

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.2.0.tar.gz (3.4 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.2.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file argcast-0.2.0.tar.gz.

File metadata

  • Download URL: argcast-0.2.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for argcast-0.2.0.tar.gz
Algorithm Hash digest
SHA256 032bea56e9cf26aa9cbea86ceb404fdc153f1262911789e894cdd4ad9050c3dc
MD5 eed4bf7c1e738b57cd7aae0fd57f3ed3
BLAKE2b-256 40d694a0df914b9ff670919370f34f4f84094c7e22d536a935cc4d4a326ce883

See more details on using hashes here.

Provenance

The following attestation bundles were made for argcast-0.2.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.2.0-py3-none-any.whl.

File metadata

  • Download URL: argcast-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 45b2b9ded6de4fa4aed305568ac0edc3f240bb5c0b5d2ba0cebb3e5c9c6c7b52
MD5 286ee2f43144237f634d0b8426b04af5
BLAKE2b-256 4d57dc20a893d2f986f9cc51a1407ccd0ac51d2491f8c9aceb2d8e2d490be788

See more details on using hashes here.

Provenance

The following attestation bundles were made for argcast-0.2.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