Guest User

Untitled

a guest
Jul 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. class T(object):
  2. """
  3. This class shows how to create dynamic attributes that are only processed when called directly for the first time.
  4. After the first call, it returns the previously fetched value.
  5.  
  6. The dynamic attributes must be in the _dynamic dictionary and must have a processing method with the following name:
  7.  
  8. get_<dynamic_attribute_name>(self)
  9. """
  10.  
  11. def __init__(self):
  12. super(T, self).__init__()
  13.  
  14. self.static = 1
  15.  
  16. self._dynamic = {
  17. 'directed': None,
  18. 'wrote': None,
  19. 'performed': None,
  20. 'playsCharacter': None,
  21. }
  22.  
  23. def __getattr__(self, name):
  24.  
  25. if name in self._dynamic.keys():
  26. print '> In dynamic Keys'
  27.  
  28. if not self._dynamic[name]:
  29. self._dynamic[name] = getattr(self, 'get_' + name)()
  30.  
  31. return self._dynamic[name]
  32. else:
  33. return super(T, self).__getattribute__(name)
  34.  
  35. def get_directed(self):
  36. print '> getting directed'
  37. return 'directed'
  38.  
  39. def get_wrote(self):
  40. print '> getting wrote'
  41. return 'wrote'
  42.  
  43. def get_performed(self):
  44. print '> getting performed'
  45. return 'performed'
  46.  
  47. def get_playsCharacter(self):
  48. print '> getting character'
  49. return 'character'
  50.  
  51.  
  52. if __name__ == '__main__':
  53.  
  54. print 'Running tests...\n'
  55.  
  56. t = T()
  57.  
  58. print '[static] Expecting 1:', t.static, '\n'
  59.  
  60. print '--- Dynamic First Time ---\n'
  61.  
  62. print '[dynamic - directed] First time:'
  63. print t.directed, '\n'
  64.  
  65. print '[dynamic - wrote] First time:'
  66. print t.wrote, '\n'
  67.  
  68. print '[dynamic - performed] First time:'
  69. print t.performed, '\n'
  70.  
  71. print '[dynamic - playsCharacter] First time:'
  72. print t.playsCharacter, '\n'
  73.  
  74. print '--- Dynamic Second Time ---\n'
  75.  
  76. print '[dynamic - directed] Second time:'
  77. print t.directed, '\n'
  78.  
  79. print '[dynamic - performed] Second time:'
  80. print t.performed, '\n'
  81.  
  82. print '[dynamic - wrote] Second time:'
  83. print t.wrote, '\n'
  84.  
  85. print '[dynamic - playsCharacter] Second time:'
  86. print t.playsCharacter, '\n'
  87.  
  88. print '--- Static creation ---\n'
  89.  
  90. t.second_static = 2
  91.  
  92. print '[second_static] Expecting 2:', t.second_static, '\n'
  93.  
  94. print '--- Attribute Error ---\n'
  95.  
  96. try:
  97. print '[attribute_error] Expecting Exception:',
  98. print t.nonExisting, '\n'
  99. except AttributeError:
  100. print 'AttributeError Caught\n'
  101.  
  102. print '--- Insanity Tests ---\n'
  103.  
  104. print '[static] Expecting 1:', t.static, '\n'
  105.  
  106. t.static = 0
  107.  
  108. print '[static] Expecting 0:', t.static, '\n'
  109.  
  110. print '[dynamic - directed] Third time:'
  111. print t.directed, '\n'
  112.  
  113. print '\n--- Done! ---'
Add Comment
Please, Sign In to add comment