caching/_examples_/property_cache_pickle.py

70 lines
1.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
sys.path.append('../..')
import report
import caching
import time
report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ])
class test_slow_data(object):
DATA_VERSION = 0.1
KEY_ONE = '1'
KEY_TWO = '2'
KEY_THREE = 'three'
KEY_FOUR = 'four'
KEY_FIVE = 'five'
KEYS = [KEY_ONE, KEY_TWO, KEY_THREE, KEY_FOUR, KEY_FIVE]
def data_version(self):
return self.DATA_VERSION
def get(self, key, default=None):
try:
return getattr(self, f'__{key}__')()
except AttributeError:
return default
def keys(self):
return self.KEYS
def uid(self):
return None
def print_n_sleep(self, k):
sys.stdout.write('slow get executed for %s\n' % k)
time.sleep(3)
def __1__(self):
self.print_n_sleep("__1__")
return 'one'
def __2__(self):
self.print_n_sleep("__2__")
return 'two'
def __three__(self):
self.print_n_sleep("__three__")
return 'three'
def __four__(self):
self.print_n_sleep("__four__")
return 'four'
def __five__(self):
self.print_n_sleep("__five__")
return 'five'
if __name__ == "__main__":
data = caching.property_cache_pickle(test_slow_data(), 'cache.pickle')
data.add_source_get_keys(data.KEY_THREE)
print('Testing property_cache (pickle):\\n--------------------------------')
for key in data.keys():
print(data.get(key))