Skip to content

Instantly share code, notes, and snippets.

@jakemmarsh
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakemmarsh/8892083 to your computer and use it in GitHub Desktop.
Save jakemmarsh/8892083 to your computer and use it in GitHub Desktop.
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