Shared utilities for Investify services
Project description
investify-utils
Shared utilities for Investify services.
Installation
# Sync PostgreSQL client (psycopg3 + SQLAlchemy + pangres)
pip install investify-utils[postgres]
# Async PostgreSQL client (asyncpg + SQLAlchemy)
pip install investify-utils[postgres-async]
PostgreSQL Clients
Sync Client (PostgresClient)
Uses psycopg3 + SQLAlchemy for connection pooling, pangres for upsert.
from investify_utils.postgres import PostgresClient
client = PostgresClient(
host="localhost",
port=5432,
username="user",
password="pass",
database="db",
# SQLAlchemy pool options
pool_size=5,
pool_recycle=3600,
)
# Read
df = client.read("SELECT * FROM table")
# Insert (uses COPY protocol)
client.insert(df, table="my_table", schema="public")
# Upsert (DataFrame index = primary keys)
df = df.set_index(["id"])
client.upsert(df, table="my_table", schema="public")
# Execute DDL/DML
client.execute("DELETE FROM table WHERE id = 1")
# Close (important for Celery workers)
client.close()
Async Client (AsyncPostgresClient)
Uses asyncpg + SQLAlchemy async engine. Read-only.
from investify_utils.postgres import AsyncPostgresClient
client = AsyncPostgresClient(
host="localhost",
port=5432,
username="user",
password="pass",
database="db",
# SQLAlchemy pool options
pool_size=5,
pool_recycle=3600,
)
# In async context
df = await client.read("SELECT * FROM table")
await client.close()
# Or as context manager
async with AsyncPostgresClient(...) as client:
df = await client.read("SELECT * FROM table")
Celery Integration
Both clients use lazy initialization, safe for Celery prefork workers.
# instances/postgres_core.py
from investify_utils.postgres import PostgresClient
from config import settings
postgres_core = PostgresClient(**settings.postgres.model_dump())
# celery_app.py
from celery.signals import worker_process_init
@worker_process_init.connect
def init_worker(**_kwargs):
# Reset engine for each worker (optional with lazy init)
from instances.postgres_core import postgres_core
postgres_core.close()
Development
uv sync
uv run ruff check investify_utils/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
investify_utils-2.0.0a5.tar.gz
(11.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file investify_utils-2.0.0a5.tar.gz.
File metadata
- Download URL: investify_utils-2.0.0a5.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a300bf3b743faf996bb6532e02bc2cb83717f9f5215c7e1c0bc2c0d690a2808
|
|
| MD5 |
27367dca39ea7be568d02f9ef3a43965
|
|
| BLAKE2b-256 |
88b258a4e9c9e1891e9029b15ef7907c9a22df0bf8d29b38818e1ac8b0728a68
|
File details
Details for the file investify_utils-2.0.0a5-py3-none-any.whl.
File metadata
- Download URL: investify_utils-2.0.0a5-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
343749829aacf3364f000bdac7da4f95baeb2e9f950f382774c987452631dacd
|
|
| MD5 |
e8da497cfbe541980bc3a91496d37ac4
|
|
| BLAKE2b-256 |
8be0ca46bf3a9483659d7dff9f9d62e98b0c096c004462a306072300b6c0797d
|