Skip to main content

A robust verification wrapper for NumPy arrays using functional and contract paradigms.

Project description

Verified Numpy

A robust verification wrapper for NumPy arrays using functional and contract paradigms.


Install

uv pip install verified_numpy

Usage

    # An example policy. Users can create their own by combining validators.
    verify_policy: Final[Validator] = combine((
        v_dtype(np.float64),
        v_ndim(2),
        v_finite,
        v_nonempty,
        v_row_major_contiguous,
    ))
    # --- Happy Path: Create and use a valid array ---
    print("--- Basic Success Case ---")
    valid_np_array = np.ascontiguousarray([[1.0, 2.0], [3.0, 4.0]])
    va_res = make_verified(valid_np_array, policy=verify_policy)

    if va_res.is_ok():
        va = va_res.value
        print(f"Successfully created VerifiedArray with shape: {va.shape}")

        # Use safe, explicit properties
        print(f"Array dtype: {va.dtype}, ndim: {va.ndim}")

        # Use NumPy functions that work via __array__ protocol
        print(f"Sum calculated by NumPy: {np.sum(va)}")

        # --- Safe Transformation ---
        print("\n--- Safe Transformation via apply_and_verify ---")
        centered_res = va.apply_and_verify(lambda arr: arr - arr.mean())
        if centered_res.is_ok():
            print(f"Centered array:\n{centered_res.value.value}")
        else:
            print(f"Centering failed: {centered_res.error}")

        # --- Operator Error ---
        print("\n--- Testing Disabled Operator ---")
        try:
            # This will fail intentionally, demonstrating the strong contract boundary.
            result = va + 1
        except TypeError as e:
            print(f"Caught expected error: {e}")

    # --- Failure Path: Try to create an invalid array ---
    print("\n--- Failure Case (Wrong DType) ---")
    invalid_np_array = np.array([[1, 2], [3, 4]], dtype=np.int32)
    va_res_fail = make_verified(invalid_np_array, policy=verify_policy)

    if va_res_fail.is_err():
        print(f"Verification failed as expected: {va_res_fail.error}")

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

verified_numpy-0.1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

verified_numpy-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file verified_numpy-0.1.0.tar.gz.

File metadata

  • Download URL: verified_numpy-0.1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for verified_numpy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9f817684aab42e4089a5dd974ada36607d05eba459c5b54cfe576942eeaf485b
MD5 cbd3b028f56d7c798c4b5c4626bae221
BLAKE2b-256 d3c03ea371dd8dbe7e73c34de9b46bc88bb69448d361c1e299c5c524e4fb17fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for verified_numpy-0.1.0.tar.gz:

Publisher: python-publish.yml on J-joon/verified_numpy

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

File details

Details for the file verified_numpy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: verified_numpy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for verified_numpy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df9f500305858c6371e0762f290021524d7705aa0ac16382a6315d9a2e9ec863
MD5 e829b450a282dc471ea9aec0f0ec0a59
BLAKE2b-256 2458a60e1d097fc0024f797c621f173d05625a7684e6bfdd6be8cac8cb81d13c

See more details on using hashes here.

Provenance

The following attestation bundles were made for verified_numpy-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on J-joon/verified_numpy

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