Skip to main content

Small utilities for Redis

Project description

aia_utilities

A Python package providing optimized Redis Streams utilities and time management functions for microservices.

Installation

pip install aia-utilities

Or install from source:

pip install -e .

Quick Start

from aia_utilities import RedisUtilities

# Initialize with defaults (localhost:6379)
ru = RedisUtilities()

# Write data to a stream
ru.write('prices', {'symbol': 'AAPL', 'price': 150.25, 'timestamp': '2026-01-01 10:00:00'}, maxlen=10000)

# Read all entries
entries = ru.read_all('prices')

# Get the latest entry matching a field
latest = ru.get_latest('prices', 'symbol', 'AAPL')

# Clean up
ru.close()

RedisUtilities

High-performance Redis Streams client with connection pooling and batch operations.

Initialization

from aia_utilities import RedisUtilities

# Default configuration
ru = RedisUtilities()

# Custom configuration
ru = RedisUtilities(
    host='redis.example.com',
    port=6380,
    db=2,
    max_connections=20,
    decode_responses=False
)

Methods

Writing Data

# Write a single entry
entry_id = ru.write('stream_name', {'key': 'value'}, maxlen=10000)

# Batch write (faster for bulk inserts)
entries = [{'id': 1, 'data': 'a'}, {'id': 2, 'data': 'b'}]
count = ru.write_batch('stream_name', entries, maxlen=10000)

Reading Data

# Read all entries (sorted by timestamp)
all_entries = ru.read_all('stream_name', order=True)

# Read limited entries
recent = ru.read_all('stream_name', count=100)

# Stream new entries as they arrive (generator)
for entry in ru.read_each('stream_name', start_id='$'):  # '$' = only new entries
    process(entry)

# Get latest entry matching field=value
latest = ru.get_latest('stream_name', 'symbol', 'AAPL')

Deleting Data

# Delete entries matching a field value
deleted_count = ru.clear('stream_name', 'symbol', 'AAPL')

# Delete entire stream
success = ru.delete('stream_name')

Inspection

# Print stream info and sample entries
ru.show('stream_name', sample=5)

# Get stream metadata
info = ru.get_stream_info('stream_name')
# Returns: {'length': 100, 'first_entry': ..., 'last_entry': ..., 'groups': 0}

Data Format

All data is stored as JSON in stream entries. Your dicts are serialized automatically:

# What you write
ru.write('prices', {'symbol': 'AAPL', 'price': 150.25})

# How it's stored in Redis
# Stream entry: {b'data': b'{"symbol": "AAPL", "price": 150.25}'}

TimeManagement

Utilities for timezone conversions between UTC and America/New_York.

from aia_utilities import TimeManagement

tm = TimeManagement()

# Convert UTC to New York time
ny_time = tm.utc_to_ny('2026-01-15T23:00:00Z')
# Returns: '2026-01-15 18:00:00.000000'

# Get UTC offset for a date
offset = tm.get_ny_utc_offset('2026-01-15 12:00:00')
# Returns: -5 (EST) or -4 (EDT depending on date)

# Convert datetime to string
timestamp_str = tm.datetime_to_string(datetime.now())

Helpers

from aia_utilities import Helpers

h = Helpers()

# Text-to-speech (macOS, non-blocking)
h.say_nonblocking("Hello world", voice="Samantha", volume=50)

# Direction label
h.updown(0.5)   # Returns: 'up'
h.updown(-0.3)  # Returns: 'down'
h.updown(0)     # Returns: None

Testing

# Unit tests (mocked Redis)
pytest tests/test_redis_utilities.py -v

# Integration tests (requires running Redis)
pytest tests/test_redis_integration.py -v -s

Requirements

  • Python 3.10+
  • Redis server (for production use)
  • Dependencies: redis, pandas, pytz

Building and Publishing to PyPI

  1. Build distributions:
python -m pip install --upgrade build twine
python -m build
  1. Upload to PyPI:
python -m twine upload dist/*
  1. (Optional) Test upload to Test PyPI first:
python -m twine upload --repository testpypi dist/*

Authentication

Use API tokens (recommended):

export TWINE_USERNAME='__token__'
export TWINE_PASSWORD='pypi-your-token-here'
python -m twine upload dist/*

Or configure ~/.pypirc:

[pypi]
username = __token__
password = pypi-your-token-here

License

MIT

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

aia_utilities-0.1.18.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

aia_utilities-0.1.18-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file aia_utilities-0.1.18.tar.gz.

File metadata

  • Download URL: aia_utilities-0.1.18.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for aia_utilities-0.1.18.tar.gz
Algorithm Hash digest
SHA256 9141ac2534e885e78b7d37975f2922f06de6c5dbc71a04fe9d44937f79dbc255
MD5 34e109d6acf028c8177e720cf9180906
BLAKE2b-256 678111a2c7e09339f9b6a9d2fe7d96419961ae2282afe2968685cb63947d1f8a

See more details on using hashes here.

File details

Details for the file aia_utilities-0.1.18-py3-none-any.whl.

File metadata

  • Download URL: aia_utilities-0.1.18-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for aia_utilities-0.1.18-py3-none-any.whl
Algorithm Hash digest
SHA256 c940b54ded615dfbaa2c4ab96419f909fe2532741ce1c3ff386b68614c4501d4
MD5 71daeb3ff62dbfc13e77a7e49f86f384
BLAKE2b-256 30cef3b46d6e5a6168fc1ab9d25fab95c9d7fcc476bf3d46162ac9dc640085a1

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