Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class State:
- def __init__(self, scale=None, snap=None, chat=None, crash=None):
- self.__scale = scale
- self.__snap = snap
- self.__chat = chat
- self.__crash = crash
- @property
- def scale(self):
- return self.__scale
- @property
- def snap(self):
- return self.__snap
- @property
- def chat(self):
- return self.__chat
- @property
- def crash(self):
- return self.__crash
- class Mealy:
- def __init__(self):
- self.current_state = 'A'
- self.branches = {'A': State(scale=['B', 0], snap=['G', 1]),
- 'B': State(snap=['C', 2], chat=['D', 3]),
- 'C': State(scale=['D', 4]),
- 'D': State(scale=['E', 5]),
- 'E': State(crash=['F', 6]),
- 'F': State(snap=['G', 7]),
- 'G': State(scale=['H', 8], snap=['B', 9]),
- 'H': State(scale=['H', 10], snap=['A', 11])
- }
- def scale(self):
- curr_method = self.branches[self.current_state].scale
- if curr_method:
- self.current_state = curr_method[0]
- return curr_method[1]
- raise MealyError('scale')
- def snap(self):
- curr_method = self.branches[self.current_state].snap
- if curr_method:
- self.current_state = curr_method[0]
- return curr_method[1]
- raise MealyError('snap')
- def chat(self):
- curr_method = self.branches[self.current_state].chat
- if curr_method:
- self.current_state = curr_method[0]
- return curr_method[1]
- raise MealyError('chat')
- def crash(self):
- curr_method = self.branches[self.current_state].crash
- if curr_method:
- self.current_state = curr_method[0]
- return curr_method[1]
- raise MealyError('crash')
- class MealyError(Exception):
- def __init__(self, *args):
- if args:
- self.message = args[0]
- else:
- self.message = None
- def __str__(self):
- if self.message:
- return self.message
- else:
- return ''
- def test():
- o = main()
- o.snap() # 1
- o.scale() # 8
- o.snap() # 11
- o.scale() # 0
- o.snap() # 2
- o.scale() # 4
- o.scale() # 5
- o.crash() # 6
- o.snap() # 7
- o.scale() # 8
- o.scale() # 10
- o.scale() # 10
- o.snap() # 11
- o.snap() # 1
- o.snap() # 9
- o.chat() # 3
- o.scale()
- o.crash()
- o.snap()
- o.scale()
- o.snap()
- o.snap() # 1
- o.snap() # 9
- o.chat() # 3
- o.scale() # 5
- o.crash() # 6
- o.snap() # 7
- o.scale() # 8
- o.scale() # 10
- o.scale() # 10
- o.snap() # 11
- o.scale() # 0
- o.snap() # 2
- o.scale() # 4
- def main():
- return Mealy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement