Skip to main content

A self updating dictionary like structure

Project description

Time Dictionary

How to install

> pip install time_dict

Overview

This module implements self-updating structure that is able to handle updating and removing object based on age. Objects added to this structure are assigned timestamp at insertion, then when age of object is exceeded optional action function is called and object is removed from the structure.

NOTE

When using this structure you must explicitly delete it due to thread locking either by calling destroy() method or deleting it as del d or the interpreter will hang at exit

Example Usage

from time_dict import TimeDict
cache = TimeDict(action_time=2, poll_time=0.5)
key = '1'
cache[key] = 1
key in cache
del cache

Main parameters are:

action_time - which specifies age in second at which objects should be deleted - 
            time when actions should be performed and object will be deleted from structure

poll_time - frequency in seconds of polling the objects for age timeout,
            experimentally should be around 1/4 of the action_time or less. 
            Please not that too frequent polling
            may negatively affect you application performance

action  -   function that is called on object age timeout. 
            Signature is 'fn(key:Any, value:Any) -> None'

no_delete - do not delete objects when actions was called

Class PARTIALLY implements dictionary interface, implementations allows for:

d = TimeDict(action_time=2, poll_time=0.5)
insertion:
    d[key] = value
updating:
    NOTE: updating only changes the value, age remains unchanged
    d[key] = value
deletion:
      del d[key]
testing for membership:
    key in d
checking length:
    len(d)

All of the above method have optimistic time complexity of O(1) and pessimistic of O(n) due to thread locking except for del which always is O(n)

Rest of the dictionary interface is not implemented by design.

Methods are:

clear() → None

Safely clear all data in the structure :raises Exception

destroy() → None

Destroys the structure, must be called to properly deinitialize it

flush() → None

Flush all the objects by calling the action function, does not remove objects Does not respect object age, calls action method on all objects :raises Exception if updated thread died

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

timedict-0.0.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

timedict-0.0.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file timedict-0.0.1.tar.gz.

File metadata

  • Download URL: timedict-0.0.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0

File hashes

Hashes for timedict-0.0.1.tar.gz
Algorithm Hash digest
SHA256 17e572a6656d2a25c93625b1089878b90908f784514f96d3797bba7aa7fcd9b1
MD5 5e68b387778af78e4a94cb80d99e908a
BLAKE2b-256 b53343612a8259f6460bc289d75f5b1acc6ed3f29ff4b756a97f67d8e05f9bc3

See more details on using hashes here.

File details

Details for the file timedict-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: timedict-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0

File hashes

Hashes for timedict-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bbfb04edb9340c022458f284a8008ef869118306c2e5aaa47acc572ee433623b
MD5 5e98c91799f954d3cbe37fa8b12f0518
BLAKE2b-256 b65321573b427c981638059d1ab033b25ebe006b71f1d72685279128d1caaa87

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