Skip to main content

A deque-like container that removes items after a time window.

Project description

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.

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

timedeque-0.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

timedeque-0.1.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: timedeque-0.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for timedeque-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3c6f8d6028d18cb4eea20426143b6f29cd4c31bd8a92ee1401e064552a4d15a4
MD5 b651dbb9507aac52b06099009d4efe37
BLAKE2b-256 a04de828e34f1d3d1727a4e7c92155047e33a4c025f56dd4df6887810e88cfb9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: timedeque-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for timedeque-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6440ae9d73770d9c78156e39d1b224bd821d7ae797a2e2d4b5d14d0620c1b440
MD5 9356f268769843c9a82329afa92c4759
BLAKE2b-256 7b1cafd01028546d79641a50af147a671d28167e6490c67d86b133f68f5fdee8

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