Python Library Caching
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

property_cache_json.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import sys
  4. sys.path.append('../..')
  5. import report
  6. import caching
  7. import time
  8. report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ])
  9. class test_slow_data(object):
  10. DATA_VERSION = 0.1
  11. KEY_ONE = '1'
  12. KEY_TWO = '2'
  13. KEY_THREE = 'three'
  14. KEY_FOUR = 'four'
  15. KEY_FIVE = 'five'
  16. KEYS = [KEY_ONE, KEY_TWO, KEY_THREE, KEY_FOUR, KEY_FIVE]
  17. def data_version(self):
  18. return self.DATA_VERSION
  19. def get(self, key, default=None):
  20. try:
  21. return getattr(self, f'__{key}__')()
  22. except AttributeError:
  23. return default
  24. def keys(self):
  25. return self.KEYS
  26. def uid(self):
  27. return None
  28. def print_n_sleep(self, k):
  29. sys.stdout.write('slow get executed for %s\n' % k)
  30. time.sleep(3)
  31. def __1__(self):
  32. self.print_n_sleep("__1__")
  33. return 'one'
  34. def __2__(self):
  35. self.print_n_sleep("__2__")
  36. return 'two'
  37. def __three__(self):
  38. self.print_n_sleep("__three__")
  39. return 'three'
  40. def __four__(self):
  41. self.print_n_sleep("__four__")
  42. return 'four'
  43. def __five__(self):
  44. self.print_n_sleep("__five__")
  45. return 'five'
  46. if __name__ == "__main__":
  47. data = caching.property_cache_json(test_slow_data(), 'cache.json')
  48. data.add_source_get_keys(data.KEY_THREE)
  49. print('Testing property_cache (json):\\n--------------------------------')
  50. for key in data.keys():
  51. print(data.get(key))