Skip to content

Instantly share code, notes, and snippets.

@jakemmarsh
Last active Aug 29, 2015
Embed
What would you like to do?
a simple keystore system in Python. Multiple values can be stored in a single key, and are retrieved based on time of creation.
import time
class keyStore:
def __init__(self):
self.storage = {}
def put(self, key, value):
# create array if it doesn't exist
if key not in self.storage:
self.storage[key] = []
# store value with current time
self.storage[key].append(value, time.time())
def get(self, key, timestamp = None):
# check if key has ever been stored
if key not in self.storage:
return None
# get newest value if no timestamp specified
if(timestamp == None):
timestamp = time.time()
# loop through key's array backwards
for item in reversed(self.storage[key]):
# if item is newer or equal to specified timestamp, return value
if(item[1] <= timestamp):
return item[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment