Skip to main content

A robust Python package for building and executing asynchronous workflow tasks in a directed acyclic graph (DAG) pattern. This package provides a type-safe, thread-safe framework for defining, organizing, and running dependent tasks with comprehensive validation and error handling.

Project description

AT Common Utils

A collection of common utilities for AT backend services.

Package Structure

at_common_util/
├── db/                      # Database related utilities
│   ├── __init__.py          # Package exports
│   ├── storage.py           # Database connection and data access (CRUD operations)
│   └── query.py             # Query building utilities
├── workflow/                # Workflow related utilities
│   ├── __init__.py          # Package exports
│   ├── core/                # Core workflow components
│   └── utils/               # Workflow utility functions
└── examples/                # Example implementations
    └── db/                  # Database usage examples

Installation

pip install -e /path/to/at-common-util

Or include in your requirements.txt:

-e /path/to/at-common-util

Database Utilities

The package provides a comprehensive database access layer with three main components:

  1. Storage - Connection management, session handling, and CRUD operations
  2. StorageManager - Registry for managing multiple storage instances (e.g., for multiple databases)
  3. QueryBuilder - Fluent API for constructing complex queries

Basic Usage

from at_common_util.db import Storage, StorageManager
from app.config import settings

# Option 1: Use the Storage class directly
storage = Storage()
storage.init(settings.DB_CONNECTION_URL)

# Option 2: Use the StorageManager for multiple database connections
default_storage = StorageManager.get_storage("default", settings.DB_CONNECTION_URL)
analytics_storage = StorageManager.get_storage("analytics", settings.ANALYTICS_DB_URL)

# Use the storage in your data access layer
async def get_user_by_id(user_id):
    from app.models import User
    return await default_storage.get_by_field(User, "id", user_id)

async def create_user(user_data):
    from app.models import User
    user = User(**user_data)
    return await default_storage.create(user)

async def update_user(user_id, update_data):
    from app.models import User
    return await default_storage.update_by_field(User, "id", user_id, update_data)

async def delete_user(user_id):
    from app.models import User
    return await default_storage.delete_by_field(User, "id", user_id)

Transaction Support

For operations that require transaction support:

from app.models import User, Profile

async def create_user_with_profile(user_data, profile_data):
    async with storage.transaction() as session:
        # Create user
        user = User(**user_data)
        session.add(user)
        await session.flush()  # Flush to get the user ID
        
        # Create profile with user ID reference
        profile = Profile(user_id=user.id, **profile_data)
        session.add(profile)
        
        # Transaction is automatically committed if no exceptions occur
    
    return user

Query Builder API

For more complex queries, use the QueryBuilder:

from at_common_util.db import QueryBuilder
from sqlalchemy import desc
from app.models import User

async def search_users(criteria):
    # Use the query builder to construct a complex query
    query = QueryBuilder.for_model(User) \
        .filter_by(is_active=True) \
        .filter(User.age > 18) \
        .order_by(desc(User.created_at)) \
        .group_by(User.role) \
        .limit(10) \
        .offset(20) \
        .build()
        
    # Execute the query using storage's session
    async with storage.get_session() as session:
        result = await session.execute(query)
        return result.scalars().all()

Available Storage Methods

  • query(model_class, filters=None, sort=None, limit=None, offset=None): Query records with optional filtering, sorting, and pagination
  • find_one(model_class, filters=None): Find a single record matching the filters
  • create(model_obj): Create a new record
  • create_many(model_objects): Create multiple records in a transaction
  • update_by_field(model_class, field_name, field_value, update_data): Update a record by a field value
  • get_by_field(model_class, field_name, field_value): Get a record by a field value
  • delete_by_field(model_class, field_name, field_value): Delete a record by field value
  • delete_many_by_field(model_class, field_name, values): Delete multiple records by field values
  • count(model_class, filters=None): Count the number of records
  • dispose(): Cleanup database connections
  • shutdown(): Close all database connections when shutting down the app

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

at_common_util-0.1.2.tar.gz (32.5 kB view details)

Uploaded Source

Built Distribution

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

at_common_util-0.1.2-py3-none-any.whl (42.2 kB view details)

Uploaded Python 3

File details

Details for the file at_common_util-0.1.2.tar.gz.

File metadata

  • Download URL: at_common_util-0.1.2.tar.gz
  • Upload date:
  • Size: 32.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for at_common_util-0.1.2.tar.gz
Algorithm Hash digest
SHA256 05f5dc4ccd187930fc80350add10c818c614830ec1130ef2ef3bcf3facf8ede3
MD5 16f9500fc5cb751d46cc7253a25b0bbc
BLAKE2b-256 0750593b94f5cfc8b82a03e2ba4c04d43909f3abcf472fdc921b0f98aa480bf3

See more details on using hashes here.

File details

Details for the file at_common_util-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: at_common_util-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 42.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for at_common_util-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a0bbd7ed027bd98c46b06863d88ec062460117acc4255324fcc0b16d2ca5ff81
MD5 f312782f3f5e85947d75508838a28aa5
BLAKE2b-256 56b506dba9d0f41af4260e6faba040fdfe98bdd05379339e94a348ec402ec3be

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