Last active
August 29, 2015 13:56
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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