Skip to main content

Awesome Python code for Python developers to make working with Python easier.

Project description

pypaya-python-tools

A comprehensive toolkit for Python developers to simplify common programming tasks and enable advanced Python features with a clean, intuitive API.

Overview

pypaya-python-tools is a meta-package for Python developers that provides tools for:

  • Dynamic importing of modules and objects
  • Safe object access and manipulation
  • Configuration-based object creation
  • Plugin architecture development
  • Dynamic code execution
  • Working with coding LLMs

The package focuses on making advanced Python features safer, more consistent, and easier to use.

Installation

pip install pypaya-python-tools

Key Features

Dynamic Imports

Import modules and objects dynamically with better error handling and security controls:

from pypaya_python_tools.importing import import_module, import_object

# Import a module
json = import_module('json')

# Import a specific object from a module
dumps = import_object('json', 'dumps')

# Import from a file
MyClass = import_object('/path/to/file.py', 'MyClass')

Object Operations

Access and manipulate object attributes and methods safely:

from pypaya_python_tools.object_operations import get_attribute, set_attribute, call

# Safely get an attribute with a default value
value = get_attribute(obj, 'attr_name', default='default_value')

# Set an attribute with validation
set_attribute(obj, 'attr_name', new_value)

# Call a method safely
result = call(obj.method, arg1, arg2, keyword=value)

Configuration-Based Objects

Create objects from configuration dictionaries:

from pypaya_python_tools.create_from_config import create_instance

# Create an instance from configuration
config = {
    "module": "myapp.models",
    "class": "User",
    "kwargs": {
        "name": "Alice",
        "role": "admin"
    }
}

user = create_instance(config)

Operation Chains

Chain operations together with context tracking and better error handling:

from pypaya_python_tools.chains import ImportChain, OperationChain

# Import and use a class in one chain
user = (
    ImportChain()
    .from_module("myapp.models")
    .get_class("User")
    .to_access_chain()
    .instantiate(name="Bob")
    .get_attribute("profile")
    .value
)

Code Execution

Execute Python code with better security and output capture:

from pypaya_python_tools.execution import PythonREPL

# Create a REPL with security settings
repl = PythonREPL(security=ExecutionSecurity(allow_subprocess=False))

# Execute code and capture results
result = repl.execute("""
x = 5
y = 10
print(f"Sum: {x + y}")
""")

print(f"Output: {result.stdout}")
print(f"Result: {result.result}")

LLM Code Tools

Present code to LLMs in optimal formats:

from pypaya_python_tools.coding_with_llms import CodePresenter

# Create a presenter for a project
presenter = CodePresenter("/path/to/project")

# Show project structure
structure = presenter.show_structure()

# Show specific files
content = presenter.show_content(["main.py", "utils/helpers.py"])

# Combine structure and content
full_presentation = presenter.combine(content_paths=["main.py"])

Subpackages

  • importing - Tools for dynamic importing
  • object_operations - Safe object attribute and method access
  • create_from_config - Configuration-based object creation
  • chains - Operation chaining with context tracking
  • execution - Safe dynamic code execution
  • decorating - Useful function and class decorators
  • coding_with_llms - Tools for working with code in LLMs

Example Use Cases

Configuration-Driven Data Pipeline

from pypaya_python_tools.create_from_config import create_instance

# Define pipeline stages from configuration
pipeline_config = [
    {
        "class": "DataValidator",
        "module": "myapp.pipeline",
        "kwargs": {
            "rules": ["no_empty", "no_duplicates"]
        }
    },
    {
        "class": "DataTransformer",
        "module": "myapp.pipeline",
        "kwargs": {
            "transformations": ["lowercase", "trim_whitespace"]
        }
    },
    {
        "class": "DataExporter",
        "module": "myapp.pipeline",
        "kwargs": {
            "format": "json",
            "destination": "output.json"
        }
    }
]

# Create the pipeline
pipeline = [create_instance(stage_config) for stage_config in pipeline_config]

# Process data through the pipeline
data = load_data()
for stage in pipeline:
    data = stage.process(data)

Safe Runtime Object Manipulation

from pypaya_python_tools.object_operations import (
    AccessManager, OperationSecurity, OperationType, Operation
)

# Create a security policy
security = OperationSecurity(
    allow_private_access=False,
    allow_protected_access=True,
    blocked_methods={"delete", "reset", "remove"}
)

# Create an access manager
manager = AccessManager(security)

# Safely access an object
def safe_access(user_obj, attr_name, operation_type, *args, **kwargs):
    """Safely access user objects with runtime validation."""
    operation = Operation(
        type=operation_type,
        args=args,
        kwargs=kwargs
    )
    
    try:
        return manager.access_object(user_obj, operation)
    except Exception as e:
        print(f"Access denied: {e}")
        return None

# Example usage
user_data = safe_access(
    user_object, 
    "profile", 
    OperationType.GET_ATTRIBUTE, 
    "profile"
)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

pypaya_python_tools-0.13.0.tar.gz (35.6 kB view details)

Uploaded Source

Built Distribution

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

pypaya_python_tools-0.13.0-py3-none-any.whl (51.7 kB view details)

Uploaded Python 3

File details

Details for the file pypaya_python_tools-0.13.0.tar.gz.

File metadata

  • Download URL: pypaya_python_tools-0.13.0.tar.gz
  • Upload date:
  • Size: 35.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for pypaya_python_tools-0.13.0.tar.gz
Algorithm Hash digest
SHA256 d00019846e33ddc0ef0ddb971ca0afe8cefdec2e6250bd96d5d0a62a47416a4e
MD5 408e1954d92257d5dd6a6d56437ed226
BLAKE2b-256 9de45ed16d9c813f1cf6444432fb85b3cc62dea83014dd1e71877ca38198c35a

See more details on using hashes here.

File details

Details for the file pypaya_python_tools-0.13.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pypaya_python_tools-0.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6134f5bdb522f9dab3e66ad11a978e1cbe24c447a9868af66e7ea80b88f85d3e
MD5 2979aeeebcaa41fe85199fc5e53b113f
BLAKE2b-256 078a07f2bf77e8225e60b9c5b1369f248e4c5960ff687714dd3ba0fae944010c

See more details on using hashes here.

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