Skip to main content

Extending the dict data type, with a rolling window feature, that automatically purge elements falling outside the defined window.

Project description

Requirements

  • Python – one of the following:

Installation

Package is uploaded on PyPI.

You can install it with pip:

$ python3 -m pip install TimedDict

Backwards compatibility

Backwards compatibility is only guaranteed between minor versions above the stable version (1.0) Therefore it’s advised to to pin the module on a version ether the specific version like: TimedDict==X.X.X

or get the latest minor version: TimedDict~=X.X.X

See more examples of how to pin version in PEP-440.

Documentation

For support, please refer to StackOverflow.

Example

The following example showcases

import time
from timeddict import TimedDict

events_window = TimedDict()

now = time.time()

# Assign values like a normal dict like:
events_window[now] = 'value_1'
events_window[now + 1] = 'value_2'

# ...or like:
events_window.update({now + 2: {'values': {'value_3', 'value_4'}}})

print('Raw data:')
print(events_window)

# NOTE:
# As the TimedDict has a thread running purging old elements, it's important to ether
# use the protect() or pause() followed by a resume() when iterating.

# Automatic by the use of context manager, protect() approach
with events_window.protect():
    print('\n- protect()')
    for event in events_window:
        print(event)

# Manual setting, pause() and resume() approach
events_window.pause()
print('\n- pause() followed by resume()')

for event in events_window:
    print(event)

events_window.resume()

# TTL example
print('\nLength of the TimedDict: {}'.format(len(events_window)))
print(events_window)
time.sleep(1.1)
print(events_window)
time.sleep(1)
print(events_window)
time.sleep(1)
print(events_window)

# Gracefully stop the purge thread
events_window.stop()

This example will print:

Raw data:
{1534608053.6948583: 'value_1', 1534608054.6948583: 'value_2', 1534608055.6948583: {'values': {'value_4', 'value_3'}}}

- protect()
1534608053.6948583
1534608054.6948583
1534608055.6948583

- pause() followed by resume()
1534608053.6948583
1534608054.6948583
1534608055.6948583

Length of the TimedDict: 3
{1534608053.6948583: 'value_1', 1534608054.6948583: 'value_2', 1534608055.6948583: {'values': {'value_4', 'value_3'}}}
{1534608054.6948583: 'value_2', 1534608055.6948583: {'values': {'value_4', 'value_3'}}}
{1534608055.6948583: {'values': {'value_4', 'value_3'}}}
{}

License

TimedDict is released under the MIT License. See LICENSE for more information.

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

TimedDict-0.2.2.tar.gz (3.7 kB view details)

Uploaded Source

File details

Details for the file TimedDict-0.2.2.tar.gz.

File metadata

  • Download URL: TimedDict-0.2.2.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.3

File hashes

Hashes for TimedDict-0.2.2.tar.gz
Algorithm Hash digest
SHA256 746581e50a48ffc723ee440aa16962c92b86d22aad88ea4d8258d3ce1f738dce
MD5 a5a9dedcca80158ecc9f0e74ba888cbf
BLAKE2b-256 3f9356b88a3a58a087d9d0f31177cb17bbfbea71d4c494d2de7b4be2dc71d338

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