Skip to main content

IO effect handlers for Python

Project description

iofx: I/O Effect Detection for Python Functions

iofx is a Python library that provides automated detection and validation of file I/O effects for functions. It leverages Pydantic for type checking and effect validation.

Features

  • Automatic detection of file read/write operations based on parameter types
  • Runtime validation of file effects
  • Integration with Pydantic for robust type checking
  • Easy-to-use decorator for adding effect detection to functions

Installation

pip install iofx

Quick Start

from iofx import create_function_model
from pydantic import FilePath, NewPath

def process_file(input_path: FilePath, output_path: NewPath) -> None:
    with open(input_path) as infile, open(output_path, "w") as outfile:
        outfile.write(infile.read().upper())

# Create the function model
process_file_model = create_function_model(process_file)

# Usage
try:
    result = process_file_model(
        input_path="existing_input.txt",
        output_path="new_output.txt",
    )
    print("File processed successfully")
except ValueError as e:
    print(f"Effect check failed: {e}")

How It Works

  1. create_function_model analyzes the function's signature and parameter types.
  2. It automatically detects potential file I/O effects based on parameter annotations:
    • FilePath: Indicates a file read operation
    • NewPath: Indicates a file write operation to a new file
    • Path: Indicates a potential file append operation
  3. At runtime, it checks if the file operations are valid (e.g., input file exists, output file doesn't exist for NewPath).

API Reference

create_function_model(func: Callable[P, R]) -> FunctionModel[P, R]

Creates a FunctionModel instance for the given function, which wraps the original function with effect detection and validation.

class FunctionModel(BaseModel, Generic[P, R])

  • func: The original function
  • parameters: List of ParameterInfo objects describing function parameters
  • return_type: Return type of the function
  • effects: List of FileEffect objects describing detected file I/O effects

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

iofx-0.1.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

iofx-0.1.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file iofx-0.1.1.tar.gz.

File metadata

  • Download URL: iofx-0.1.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.17.3 CPython/3.10.14 Linux/5.15.0-117-generic

File hashes

Hashes for iofx-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e4194e2794e7556eb76b6953b72acb8af93e0b7846896f64113b6167e53e81e9
MD5 079c02c36c890a89c131cb5b85939988
BLAKE2b-256 ac3019d68bf70e95a6510734a56e0ca62ce1b061e83718301465ab2d49c9fa37

See more details on using hashes here.

File details

Details for the file iofx-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: iofx-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.17.3 CPython/3.10.14 Linux/5.15.0-117-generic

File hashes

Hashes for iofx-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b08f6c3c2979c25c07d377f486f14c653ec4df26e43d9e0a5388cd5b42b154a6
MD5 21b5de36220f141777b5f80298510e34
BLAKE2b-256 7adef0d821a86d6fa7e1deafd98e79be3d3d485eb33aed6d293c34709701d9e6

See more details on using hashes here.

Supported by

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