Last active
April 29, 2020 19:53
-
-
Save netskink/fa15056f7fd968e224e3704b33732518 to your computer and use it in GitHub Desktop.
a state machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example usage of state machine class | |
# | |
# | |
# | |
class ns_MouseAction: | |
def __init__(self, action): | |
self.action = action | |
def __str__(self): | |
return self.action | |
# This was a python 2 method for comparing instances | |
# of classes. ie. objects. In python 3 the __cmp__ routine | |
# was dropped in favor of __eq__, __ne__, __lt__, __le__ | |
# __gt__, and __ge__ | |
def __cmp__(self, other): | |
return cmp(self.action, other.action) | |
# Necessary when __cmd__ or __eq__ is defined | |
# in order to make this class usable as a | |
# dictionary key: | |
def __hash__(self): | |
return hash(self.action) | |
# Static fields an enumeration of instances | |
# These are all the possible actions and they instances | |
# of this class with the string corresponding to the name | |
# of the action. | |
# | |
# | |
ns_MouseAction.appears = ns_MouseAction("mouse appears") | |
ns_MouseAction.runsAway = ns_MouseAction("mouse runs away") | |
ns_MouseAction.enters = ns_MouseAction("mouse enters trap") | |
ns_MouseAction.escapes = ns_MouseAction("mouse escapes") | |
ns_MouseAction.trapped = ns_MouseAction("mouse trapped") | |
ns_MouseAction.removed = ns_MouseAction("mouse removed") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# stuff deleted to get to the point of confusion | |
# he is trying to run stript on the iterator of the open | |
# this fails on umicropython | |
# moves = map(string.strip, open("../mouse/MouseMoves.txt").readlines()) | |
#### | |
# | |
# Executing the State Machine with a scenario provided by | |
# a text file. | |
# | |
############################ | |
# this returns a list a list of moves without strip | |
list_of_moves = open("MouseMoves.txt").readlines() | |
#print(list_of_moves) | |
# this was his code when he used iterators | |
#ns_MouseTrap().runAll(map(ns_MouseAction, moves)) | |
# This code will call ns_MouseAction on each line | |
ns_MouseTrap().runAll(map(ns_MouseAction, list_of_moves)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment