A module for ring buffer operations using NumPy backend
Project description
NP Ringbuffer
np_ringbuffer provides ring buffer operations using NumPy backend.
Example usage
from np_ringbuffer.ringbuffer import NumpyRingBuffer, TimedNumpyRingBuffer
import numpy as np
# construct from an existing numpy array
ringbuffer = NumpyRingBuffer.create_from_array(np.array([1,2,3]))
ringbuffer.append(3)
print(ringbuffer.values()) # [2,3,3]
ringbuffer.add_to_latest(10)
print(ringbuffer.values()) # [2,3,13]
# construct an empty buffer with 5 capacity
ringbuffer = NumpyRingBuffer(capacity=10)
ringbuffer.append(3)
print(ringbuffer.values()) # [3]
ringbuffer.add_to_latest(10)
print(ringbuffer.values()) # [13]
# automate the append and add_to_latest with interval_ms
timed_ringbuffer = TimedNumpyRingBuffer(interval_ms=60_000, capacity=10)
timed_ringbuffer.add(1)
Practical usuage
In finance, if you want to build an real-time trading volume accumulator in 1-minute bucket. You can do something like this (this example uses 1-second bar for simplicity sake):
# volume 1-second bar example
def generate_volume() -> float:
return random.random() * 100
def get_volume(iterations: int = 1_000_000, pause_ms: float = 1e-5):
for _ in range(iterations):
time.sleep(pause_ms)
yield generate_volume()
timed_ringbuffer = TimedNumpyRingBuffer(interval_ms=1000, capacity=10)
for volume in get_volume():
timed_ringbuffer.add(volume)
print(timed_ringbuffer.values())
All of these examples are included in example.py
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
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 np_ringbuffer-1.0.0.tar.gz.
File metadata
- Download URL: np_ringbuffer-1.0.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0619bdee8b8ebb42011a210a4d72a7f816785f4129910a0396063f20c205c115
|
|
| MD5 |
4fd01e7ba5371d48053aa075f784abaa
|
|
| BLAKE2b-256 |
69641f9fe43f6b71a7ae3321cecb9ae505f31dc89889f85b47dd847e375750a1
|
File details
Details for the file np_ringbuffer-1.0.0-py3-none-any.whl.
File metadata
- Download URL: np_ringbuffer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
409a2c6c2f149b65a192338159bfa3635762824780c0dc5f918d19dfbbc181fc
|
|
| MD5 |
ea39b803c6ed2bb01407c866ab5d5666
|
|
| BLAKE2b-256 |
b2b272b081f97988f856c1f4551df50637755b6a433224efc8693d859ecef9e1
|