当前位置: 首页>>代码示例>>Python>>正文


Python StateMachine.do_event方法代码示例

本文整理汇总了Python中statemachine.StateMachine.do_event方法的典型用法代码示例。如果您正苦于以下问题:Python StateMachine.do_event方法的具体用法?Python StateMachine.do_event怎么用?Python StateMachine.do_event使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在statemachine.StateMachine的用法示例。


在下文中一共展示了StateMachine.do_event方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from statemachine import StateMachine [as 别名]
# 或者: from statemachine.StateMachine import do_event [as 别名]
def main():
    #This makes it so if you hit CTRL+C curses doesn't eat the terminal alive!
    signal.signal(signal.SIGINT, signal_handler)
    
    #Setup curses
    stdscr = curses.initscr()
    curses.noecho()
    curses.cbreak()
    curses.curs_set(0)
    
    #Create the State Machine instance
    s = StateMachine()
    
    win = curses.newwin(20, 40, 2, 2)
    win.addstr(1, 7, "CODE:\n")
    win.addstr(18, 7, "Press q or CTRL-c to quit.")
    win.border()
    win.refresh()
    
    #This while loop keeps checking the input you type on the keyboard
    while True:
        #Get a single keypress and turn it into a string
        win.border()
        c = chr(win.getch())
        win.addstr(13, 7, "Correct PIN: "+" ".join(s.correct_code))
    
        #if you press q, terminate the program
        if c == 'q':
            break
    
        if c.isalnum():
            win.addstr(10, 7, "Prev PIN: "+" ".join(s.cur_code))
    
            old_state = s.state
            s.do_event(StateMachine.E_KEYPRESS, c)
            new_state = s.state
    
            #Write out some debug data
            win.addstr(15, 7, "OLD STATE: %s        "%s.STATE_NAMES[old_state])
            win.addstr(16, 7, "NEW STATE: %s        "%s.STATE_NAMES[new_state])
            win.addstr(18, 7, "Press q or CTRL-c to quit.")
    
            if s.state == StateMachine.IDLE:
                win.erase()
                win.addstr(1, 7, "CODE:  ")

            win.addstr(2, 7, '* '*len(s.cur_code))
    
            if s.state == StateMachine.CODEOK:
                win.addstr(1, 7, "SUCCESS")
    
            elif s.state == StateMachine.CODEBAD:
                win.addstr(1, 7, "NO!    ")
    
            win.addstr(11, 7, "Curr PIN: "+" ".join(s.cur_code))
    
            #Curses only draws changes to the screen when you ask nicely.
            win.refresh()
    
    cleanup_curses()
开发者ID:diamondman,项目名称:StatemachineExample,代码行数:62,代码来源:combolock.py

示例2: ComboLock

# 需要导入模块: from statemachine import StateMachine [as 别名]
# 或者: from statemachine.StateMachine import do_event [as 别名]

#.........这里部分代码省略.........
        self.win.addstr(18, 7, "Press q or CTRL-c to quit.")

        #Curses doesn't draw to the screen as soon as you call addstr. 
        #It gets a list of changes together and puts them on the screen 
        #when you call refresh on the window. This allows your interface
        #to never look like it it being drawn because it puts it up once
        #you have everything ready in the background.
        self.win.refresh()
    

    def run(self):
        self.init_curses()
        #This next line initializes the display so you see something 
        #before pressing the first code. The proceeding calls to this
        #function happen in the while loop after each read.
        self._display_UI()

        #This while loop keeps checking the input you type on the keyboard
        while True:
            #Get a single keypress and turn it into a string
            c = chr(self.win.getch())
        
            #if you press q, terminate the program.
            if c == 'q': 
                break
        
            #This function call is built into python and returns true if the string
            #contains letters and/or numbers. If you want to allow other characters
            #like the home key, etc, you will need to edit or add to this if 
            #to accept that type of key too.
            if c.isalnum():
                old_state = self._sm.state

                self._sm.do_event(StateMachine.E_KEYPRESS, c)
                #It is a good idea to limit the number of places certain things 
                #happen. Idealy all drawing to the screen will be done in the 
                #display_ui function so looking only at that function we can see
                #everything that will be drawn. The following print of OLD STATE
                #is an example of when trying to do that can be hard.
                self._display_UI()

                #This following addstr prints off the old state of the state 
                #machine. The old_state variable stores the needed state before 
                #the do_event function is called which causes that state to 
                #change. This is only debug data so I am not making it super fancy. 
                #
                #Note that if you wanted to add printing this code to the
                #display_UI function, you would have to pass it to the function 
                #in one of several ways: 
                # 1) Pass it to the function like 
                #      self._display_UI(old_code, old_state)
                # 2) Store it as a variable in self.
                #      self.old_code = self._sm.cur_code
                #    and then read it in the display function from self.
                # 3) Make the state machine store the old state so when there would 
                #    be a state variable and old_state variable, etc.
                #      self.__sm.old_state
                #
                #It is important to think about haw data can be passed around, stored
                #and accessed. If you don't, then there will be lots of frustration
                #about not being able to get your data and you may never want to use
                #classes for anything.
                #
                #If this access management seems pointlessly complex, notice that 
                #without this comment or the following debug code, this whole function
                #is very simple and easy to follow with no unrelated code mixed together.
开发者ID:diamondman,项目名称:StatemachineExample,代码行数:70,代码来源:combolock.py


注:本文中的statemachine.StateMachine.do_event方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。