Skip to main content

Generate and maintain a pool of unique integer keys.

Project description

keypool is a library to generate and maintain a pool of unique integer keys. Priority is given to reusing freed keys rather than generating new ones.

This package is meant for situations where keys for a dict are irrelevant or arbitrary.

Installation

Python

Only Python 2.6+ is required!

  • Install with pip: pip install keypool

  • Clone and install: python python/setup.py install

If you want to run the tests, ensure nose is installed with pip install nose.

JavaScript

The JavaScript version requires no dependencies or special interpreters.

The basics

Python

from keypool import KeypoolDict
items = KeypoolDict()

# Assign a value with a unique, generated key
items[items.next()] = 'hello, world'

# Assign a value but capture the key
key = items.setitem('hello again, world')

# Assign anything except an integer, like a normal dict
items['hello'] = 'world'

When an item is deleted, its key is freed for reuse:

from keypool import KeypoolDict
items = KeypoolDict()

# Add some items
keys = [items.setitem(word) for word in ['aardvark', 'baboon', 'crocodile']]

# [0, 1, 2]
print(keys)

# Delete 0
del items[keys[0]]

# Add a new item
key = items.setitem('dragonfly')

# 0 (as opposed to 3)
print(key)

JavaScript

var items = keyPool();

// Assign a value with a unique integer.
// The key is immediately generated, unlike in Python
items[items.newKey()] = 'hello, world';

// Assign a value but capture the key
var key = items.set('hello again, world');

// Assign anything except an integer to the internal object
items['hello'] = 'world';

items.del(key);      // key 1 is removed and the next free key will be 1
items.del('hello');  // a key outside the pool is removed

Examples

Let’s say you’re wrapping a timer function in some horrible API:

def timer(unique_name, **kwargs):
      ...

and each active timer needs to be stored for efficient lookup (i.e. a dict). Usually a timestamp or uuid will suffice for this type of problem:

import time

timers = {}

def create_timer(**kwargs):
      key = str(time.time())
      timers[key] = timer(unique_name=key, **kwargs)
      return key

keys = [create_timer(...) for i in xrange(0, 10)]

Oops, the loop is iterating faster than time.time’s precision and thus all keys are identical:

# [1310422700.9400001, 1310422700.9400001, 1310422700.9400001,
#  1310422700.9400001, 1310422700.9400001, 1310422700.9400001,
#  1310422700.9400001, 1310422700.9400001, 1310422700.9400001,
#  1310422700.9400001]
print(keys)

assert not all([keys[0] == key for key in keys])

A KeypoolDict solves this problem in a cleaner fashion with unique integer keys:

from keypool import KeypoolDict
from operator import delitem

timers = KeypoolDict()

def create_timer(**kwargs):
      key = timers.next()
      timers[key] = timer(unique_name=key, **kwargs)
      return key

keys = [create_timer(...) for i in xrange(0, 10)]

No keys are identical now!

# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(keys)

assert all([x == y for x,y in zip(sorted(set(keys)), sorted(keys))])

Keys are also reused when deleted, so arbitrarily increasing values are mostly avoided:

# Delete all the items
[delitem(timers, key) for key in timers.keys()]

# The old keys are now reused
keys = [create_timer(...) for i in xrange(0, 10)]

# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(keys)

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keypool-0.1.tar.gz (3.5 kB view details)

Uploaded Source

File details

Details for the file keypool-0.1.tar.gz.

File metadata

  • Download URL: keypool-0.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for keypool-0.1.tar.gz
Algorithm Hash digest
SHA256 03097d4919c44043a6f2f94dc98bc67062aa5501a8b2ba5bd8c7286b2284a198
MD5 b9f45f37c06853d43eade038815a102d
BLAKE2b-256 8101c03870995cbcb7701be0c2a8dc7397f3de44238d98e2ba7170bad2db7cec

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