Skip to main content

codercore

Project description

Coder Core

Async-first Python utility library providing common infrastructure for backend services.

Features

  • Database — Async PostgreSQL session management via SQLAlchemy 2.0 + asyncpg, with Google Cloud SQL connector support
  • Cursor-based pagination — Keyset pagination for SQLAlchemy queries with multi-column ordering and composite IDs
  • Redis — Connection factory with TTL defaults and a @cache decorator for transparent async caching
  • Aggregation — Query mixins for grouped and date-bucketed aggregations (year through second precision)
  • Password hashing — bcrypt utilities for hashing and verification
  • HTTP — Async context manager for validating third-party API responses
  • Settings — Environment-variable-based configuration loader for database and Redis
  • Test utilities — Reusable pytest fixtures for async database sessions, Redis connections, and Pydantic validation helpers

Installation

pip install codercore

Quick Start

Database Sessions

from codercore.db import sessionmaker, get_connection_url

url = get_connection_url("postgresql+asyncpg", "user", "pass", "localhost", "mydb")
Session = sessionmaker(url)

async with Session() as session:
    result = await session.execute(...)

Cursor-Based Pagination

from codercore.db.pagination import paginate, Cursor
from codercore.lib.collection import Direction

statement = paginate(
    select(User),
    id_column=User.id,
    cursor=Cursor.decode(cursor_token) if cursor_token else None,
    order_by=User.created_at,
    order_direction=Direction.DESC,
    limit=20,
)

Redis Caching

from codercore.lib.redis import connection, cache

redis = connection(host="localhost", port=6379, default_ttl_in_seconds=300)

@cache(key=lambda user_id: f"user:{user_id}", ex=60)
async def get_user(user_id: str) -> str:
    ...

Password Hashing

from codercore.lib.hash import bcrypt_hash, bcrypt_check_plaintext_equals_hash

hashed, salt = bcrypt_hash("my_password")
is_valid = bcrypt_check_plaintext_equals_hash("my_password", hashed, salt)

Requirements

  • Python 3.12+
  • PostgreSQL 15+ (required by codercore.db)
  • Redis 7+ (required by codercore.lib.redis)

Modules such as codercore.lib.hash, codercore.lib.http, and codercore.lib.aggregation can be used independently without PostgreSQL or Redis.

Documentation

Build and serve the API reference locally:

pip install -e ".[docs]"
mkdocs serve

Then visit http://127.0.0.1:8000. To build static HTML:

mkdocs build

Development

pip install -e ".[test,dev]"

Running Tests

Tests run against real PostgreSQL and Redis instances via Docker Compose:

docker compose -f test-compose.yml up --build

Linting

This project uses pre-commit for linting:

pre-commit install
pre-commit run --all-files

License

Apache-2.0

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

codercore-8.3.0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

codercore-8.3.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file codercore-8.3.0.tar.gz.

File metadata

  • Download URL: codercore-8.3.0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codercore-8.3.0.tar.gz
Algorithm Hash digest
SHA256 17e558941e5c23abfe45f0b2e95186ff733e940835deeb97fbd8379c1b068f50
MD5 ae36a41f822a5b19d88b91b111b5a20b
BLAKE2b-256 63ce6c564abe926a93aa4f870a30e223830df1c7c348b415d67fa038287d215d

See more details on using hashes here.

File details

Details for the file codercore-8.3.0-py3-none-any.whl.

File metadata

  • Download URL: codercore-8.3.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codercore-8.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9694bcc447918465434183d4ffe020ac38cf989fd260029e47579d2a9ea4a381
MD5 c7a1cfc732edc286155dd83caba18d77
BLAKE2b-256 14292dd2b5e564151ded8c7b16440a4be8f8d5a4fe93d31d7264486f6cabdaac

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