Last active
August 29, 2015 14:20
-
-
Save andreasnuesslein/80b1c9d8e97147fa79f0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class PickleThis: | |
def __init__(self, filename, age=86400): | |
self.file = filename | |
self.age = age | |
def __enter__(self): | |
if os.path.isfile(self.file) and (time.time() - os.stat(self.file).st_mtime) < self.age: | |
self.new = False | |
with open(self.file, 'rb') as f: | |
self.content = pickle.load(f) | |
print("%s gefunden" % self.file) | |
else: | |
self.new = True | |
self.content = False | |
print("%s inexistent oder zu alt" % self.file) | |
return self | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
if self.new: | |
with open(self.file, 'wb') as f: | |
pickle.dump(self.content, f) | |
print("%s erzeugt" % self.file) | |
def test(): | |
with PickleThis('testpickle.p') as pickel: | |
if not pickel.content: | |
adressen = <crazy sql query> | |
pickel.content = adressen | |
else: | |
adressen = pickel.content | |
x = adressen... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment