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
np_ringbuffer-1.0.0.tar.gz
(4.4 kB
view hashes)
Built Distribution
Close
Hashes for np_ringbuffer-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 409a2c6c2f149b65a192338159bfa3635762824780c0dc5f918d19dfbbc181fc |
|
MD5 | ea39b803c6ed2bb01407c866ab5d5666 |
|
BLAKE2b-256 | b2b272b081f97988f856c1f4551df50637755b6a433224efc8693d859ecef9e1 |