Skip to main content

A lightweight circular buffer for handling stream data with fast appending and retrieval.

Project description

stream-buffer

A lightweight circular buffer for handling stream data with fast appending and retrieval.

Motivation

While working with live ticker data from okx, I realized that there was no lightweight buffer that offers fast appending and range based queries for streaming applications. Thus this buffer was created to address this gap.

Use Cases

  • You need fast time-based appends and efficient range retrievals.

  • You want to store only the latest N entries (rolling buffer).

  • You’re working with streaming APIs, market data, sensor feeds, or event logs.

  • You need custom indexing (e.g., timestamps, floats) but want lower overhead than Pandas.

  • You want a minimal and dependency-light alternative to DataFrames or reactive pipelines.

Installation

pip install stream-buffer

Example

from stream_buffer import StreamBuffer
from datetime import datetime, timedelta

# Initialize a buffer with a maximum size of 3
timestamps = [datetime.now() + timedelta(seconds=i) for i in range(5)]
buf = StreamBuffer(max_size=3)

# Add items with explicit datetime keys
for i, ts in enumerate(timestamps):
    buf.add(f"price-{i}", key=ts)

# The buffer now contains only the last 3 items (circular behavior)
print("Buffer contents (key, value):")
for k, v in buf:
    print(k, v)

# Access by index (supports negative indices)
print("\nFirst item:", buf[0])
print("Last item:", buf[-1])

# Slicing
print("\nSlice [0:2]:", buf[0:2])

# Membership test (by key)
key_to_check = buf[1][0]
print(f"\nIs key {key_to_check} in buffer?", key_to_check in buf)

# Get by key (exact and neighbors)
print("\nGet by exact key:", buf.get(key_to_check))
print("Get by non-existent key (neighbors):", buf.get(key_to_check + 0.5, force_exact=False))

# Range query (all items between two keys)
start_key = buf[0][0]
end_key = buf[-1][0]
print("\nRange query:", buf.get_range(start_key, end_key))

# Convert to list
print("\nBuffer as list:", buf.to_list())

# Buffer length
print("\nBuffer length:", len(buf))

# Clear the buffer
buf.clear()
print("\nBuffer cleared. Length:", len(buf))

Expected Output

Buffer contents (key, value):
1750881382.592505 price-2
1750881383.592508 price-3
1750881384.59251 price-4

First item: (np.float64(1750881382.592505), 'price-2')
Last item: (np.float64(1750881384.59251), 'price-4')

Slice [0:2]: [(np.float64(1750881382.592505), 'price-2'), (np.float64(1750881383.592508), 'price-3')]

Is key 1750881383.592508 in buffer? True

Get by exact key: price-3
Get by non-existent key (neighbors): ('price-3', None, 'price-4')

Range query: [(np.float64(1750881382.592505), 'price-2'), (np.float64(1750881383.592508), 'price-3')]

Buffer as list: [(np.float64(1750881382.592505), 'price-2'), (np.float64(1750881383.592508), 'price-3'), (np.float64(1750881384.59251), 'price-4')]

Buffer length: 3

Buffer cleared. Length: 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

stream_buffer-0.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

stream_buffer-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file stream_buffer-0.1.0.tar.gz.

File metadata

  • Download URL: stream_buffer-0.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for stream_buffer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1e42b7eee13c93460756d9549171c9f0de2e79e62dc135303bdc3f4e230753cf
MD5 d223f93968a86ee668788d946bfea827
BLAKE2b-256 25e78386317b0e074d5ecf21b2e0dc7caf216c79a9889e2fc5803f7c8bd7b0f1

See more details on using hashes here.

File details

Details for the file stream_buffer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for stream_buffer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5fb426c0f8c210f5176b8166fab384db973233f4eb7ae47eb009a9580d799ef2
MD5 22e466dca8b331064e2aadad16eb8d22
BLAKE2b-256 2e348aee852b063bc43a5343aeebed1647c265bdfd825317925f55c7efe3ab0b

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