Skip to main content

A self updating dictionary like structure

Project description

Time Dictionary

Unittests

Now objects age too! - Automatically manage your objects based on age

How to install

> pip install timedict

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.2.tar.gz (5.2 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.2-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: timedict-0.0.2.tar.gz
  • Upload date:
  • Size: 5.2 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.2.tar.gz
Algorithm Hash digest
SHA256 4904dcc98d60427089040a7e57a9581cded8401666a4f9997cd1238aed2f2b9b
MD5 0b4d590a5f2bedc6701953e95e98da28
BLAKE2b-256 56c232818bddbdb31f16100076e972df23223e469d2cbf7372d3fab47be8b69c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: timedict-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2f9c367bada729b8a8964d87d91e14f4af39acbb6fcfd75500287892232bad43
MD5 aa7097c6c6cf64bf000b86bd26f1a322
BLAKE2b-256 cef7b9a3b676786dc8fc8438dc8ec9d641537a28cdbc557d370e8945daf5af9a

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