Advertisement
selebry

fsd

Mar 20th, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. class State:
  2. def __init__(self, scale=None, snap=None, chat=None, crash=None):
  3. self.__scale = scale
  4. self.__snap = snap
  5. self.__chat = chat
  6. self.__crash = crash
  7.  
  8. @property
  9. def scale(self):
  10. return self.__scale
  11.  
  12. @property
  13. def snap(self):
  14. return self.__snap
  15.  
  16. @property
  17. def chat(self):
  18. return self.__chat
  19.  
  20. @property
  21. def crash(self):
  22. return self.__crash
  23.  
  24.  
  25. class Mealy:
  26. def __init__(self):
  27. self.current_state = 'A'
  28. self.branches = {'A': State(scale=['B', 0], snap=['G', 1]),
  29. 'B': State(snap=['C', 2], chat=['D', 3]),
  30. 'C': State(scale=['D', 4]),
  31. 'D': State(scale=['E', 5]),
  32. 'E': State(crash=['F', 6]),
  33. 'F': State(snap=['G', 7]),
  34. 'G': State(scale=['H', 8], snap=['B', 9]),
  35. 'H': State(scale=['H', 10], snap=['A', 11])
  36. }
  37.  
  38. def scale(self):
  39. curr_method = self.branches[self.current_state].scale
  40. if curr_method:
  41. self.current_state = curr_method[0]
  42. return curr_method[1]
  43. raise MealyError('scale')
  44.  
  45. def snap(self):
  46. curr_method = self.branches[self.current_state].snap
  47. if curr_method:
  48. self.current_state = curr_method[0]
  49. return curr_method[1]
  50. raise MealyError('snap')
  51.  
  52. def chat(self):
  53. curr_method = self.branches[self.current_state].chat
  54. if curr_method:
  55. self.current_state = curr_method[0]
  56. return curr_method[1]
  57. raise MealyError('chat')
  58.  
  59. def crash(self):
  60. curr_method = self.branches[self.current_state].crash
  61. if curr_method:
  62. self.current_state = curr_method[0]
  63. return curr_method[1]
  64. raise MealyError('crash')
  65.  
  66.  
  67. class MealyError(Exception):
  68. def __init__(self, *args):
  69. if args:
  70. self.message = args[0]
  71. else:
  72. self.message = None
  73.  
  74. def __str__(self):
  75. if self.message:
  76. return self.message
  77. else:
  78. return ''
  79.  
  80.  
  81. def test():
  82. o = main()
  83. o.snap() # 1
  84. o.scale() # 8
  85. o.snap() # 11
  86. o.scale() # 0
  87. o.snap() # 2
  88. o.scale() # 4
  89. o.scale() # 5
  90. o.crash() # 6
  91. o.snap() # 7
  92. o.scale() # 8
  93. o.scale() # 10
  94. o.scale() # 10
  95. o.snap() # 11
  96. o.snap() # 1
  97. o.snap() # 9
  98. o.chat() # 3
  99. o.scale()
  100. o.crash()
  101. o.snap()
  102. o.scale()
  103. o.snap()
  104. o.snap() # 1
  105. o.snap() # 9
  106. o.chat() # 3
  107. o.scale() # 5
  108. o.crash() # 6
  109. o.snap() # 7
  110. o.scale() # 8
  111. o.scale() # 10
  112. o.scale() # 10
  113. o.snap() # 11
  114. o.scale() # 0
  115. o.snap() # 2
  116. o.scale() # 4
  117.  
  118.  
  119. def main():
  120. return Mealy()
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement