Skip to main content

TimeDeque

TimeDeque is a small Python utility for keeping only the most recent items in a deque.

Each item is stored with the time it was added. When items become older than the configured time window, they are automatically removed.

Installation

pip install timedeque

Basic Usage

from timedeque import TimeDeque
from time import sleep

dq = TimeDeque(seconds=2)

dq.append("first")
dq.append("second")

print(list(dq))
# ['first', 'second']

sleep(3)

print(list(dq))
# []

Why use this?

Python's built-in collections.deque is useful for storing ordered data, but it does not remove items based on age.

TimeDeque is useful when you only care about recent events, such as:

  • recent trades
  • recent errors
  • rate-limit windows
  • rolling event buffers
  • short-term signals

API

TimeDeque(seconds)

Creates a new deque that keeps items for the given number of seconds.

dq = TimeDeque(seconds=60)

append(item)

Adds an item to the deque with the current time.

dq.append("event")

len(dq)

Returns the number of non-expired items.

len(dq)

bool(dq)

Returns True if the deque has any non-expired items.

if dq:
    print("There are recent items")

Iteration

You can iterate over the current non-expired items.

for item in dq:
    print(item)

Indexing

You can access an item by index.

first_item = dq[0]

to_list()

Returns the current non-expired items as a regular list.

items = dq.to_list()

clear()

Removes all items.

dq.clear()

Example

from timedeque import TimeDeque
from time import sleep

errors = TimeDeque(seconds=300)

errors.append("connection failed")
errors.append("timeout")

if len(errors) >= 2:
    print("Multiple recent errors")

Requirements

Python 3.10 or newer.

License

MIT License.

Metadata

Release files for timedeque 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for timedeque 0.1.0
File Size Uploaded Provenance
timedeque-0.1.0.tar.gz 3.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for timedeque 0.1.0
File Interpreter ABI Platform
timedeque-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.9 kB

Release files / timedeque-0.1.0.tar.gz

Download URL timedeque-0.1.0.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3c6f8d6028d18cb4eea20426143b6f29cd4c31bd8a92ee1401e064552a4d15a4
BLAKE2b-256 checksum
How to use checksums
a04de828e34f1d3d1727a4e7c92155047e33a4c025f56dd4df6887810e88cfb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / timedeque-0.1.0-py3-none-any.whl

Download URL timedeque-0.1.0-py3-none-any.whl
Size 3.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6440ae9d73770d9c78156e39d1b224bd821d7ae797a2e2d4b5d14d0620c1b440
BLAKE2b-256 checksum
How to use checksums
7b1cafd01028546d79641a50af147a671d28167e6490c67d86b133f68f5fdee8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Security for timedeque 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page