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.23.0.tar.gz (43.3 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.23.0-py3-none-any.whl (62.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pypaya_python_tools-0.23.0.tar.gz
Algorithm Hash digest
SHA256 a76ae815cecf126d3ae7438199d3f8ca8e0a450b3e95fd2e550300417b5a7e30
MD5 712a3e4ffd8dbffff257954f0de5c0d6
BLAKE2b-256 040850423c1756becd0da87bbd9c20467820849622cf5258cff132c01106dc1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pypaya_python_tools-0.23.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d02e9ae4cb52fff3f3b1198a3550c9228ee7e88112edca2ea410b567239219c
MD5 2a4c24306797337cbfd36f2602f453e5
BLAKE2b-256 ff73089e86ea6a98129552b083ade43ef1dbc964865aaa4272bce2dab444a7b6

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