亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? sect05.htm

?? Pythn design pattern
?? HTM
?? 第 1 頁 / 共 4 頁
字號(hào):
state to the next is often a <I>Template Method</I>, as seen in the following
framework for a basic state machine.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_144">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Each state can be <B>run(&#160;)</B> to
perform its behavior, and (in this design) you can also pass it an
&#147;input&#148; object so it can tell you what new state to move to based on
that &#147;input&#148;. The key distinction between this design and the next
is that here, each <B>State</B> object decides what other states it can move to,
based on the &#147;input&#148;, whereas in the subsequent design all of the
state transitions are held in a single table. Another way to put it is that
here, each <B>State</B> object has its own little <B>State</B> table, and in the
subsequent design there is a single master state transition table for the whole
system.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_145">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c04:statemachine:State.py
# A State has an operation, <font color=#0000ff>and</font> can be moved
# into the next State given an Input:

<font color=#0000ff>class</font> State:
  <font color=#0000ff>def</font> run(self): 
    <font color=#0000ff>assert</font> 1, <font color=#004488>"run not implemented"</font>
  <font color=#0000ff>def</font> next(self, input):
    <font color=#0000ff>assert</font> 1, <font color=#004488>"next not implemented"</font>
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This class is clearly
unnecessary, but it allows us to say that something is a <B>State</B> object in
code, and provide a slightly different error message when all the methods are
not implemented. We could have gotten basically the same effect by saying:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_146">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> State: <font color=#0000ff>pass</font></PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">because we
would still get exceptions if <B>run(&#160;)</B> or <B>next(&#160;)</B> were
called for a derived type, and they hadn&#146;t been implemented.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_147">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>StateMachine</B> keeps track of
the current state, which is initialized by the constructor. The
<B>runAll(&#160;)</B> method takes a list of <B>Input</B> objects. This method
not only moves to the next state, but it also calls <B>run(&#160;)</B> for each
state object &#150; thus you can see it&#146;s an expansion of the idea of the
<B>State</B> pattern, since <B>run(&#160;)</B> does something different
depending on the state that the system is in.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_148">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c04:statemachine:StateMachine.py
# Takes a list of Inputs to move <font color=#0000ff>from</font> State to 
# State using a template method.

<font color=#0000ff>class</font> StateMachine:
  <font color=#0000ff>def</font> __init__(self, initialState):
    self.currentState = initialState
    self.currentState.run()
  # Template method:
  <font color=#0000ff>def</font> runAll(self, inputs):
    <font color=#0000ff>for</font> i <font color=#0000ff>in</font> inputs:
      <font color=#0000ff>print</font> i
      self.currentState = self.currentState.next(i)
      self.currentState.run()
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">I&#146;ve also treated
<B>runAll(&#160;)</B> as a template method. This is typical, but certainly not
required &#150; you could concievably want to override it, but typically the
behavior change will occur in <B>State</B>&#146;s <B>run(&#160;)</B> instead.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_149">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At this point the basic framework for
this style of <I>StateMachine</I> (where each state decides the next states) is
complete. As an example, I&#146;ll use a fancy mousetrap that can move through
several states in the process of trapping a
mouse</FONT><A NAME="fnB13" HREF="#fn13">[13]</A><FONT FACE="Georgia">. The
mouse classes and information are stored in the <B>mouse</B> package, including
a class representing all the possible moves that a mouse can make, which will be
the inputs to the state machine:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_150">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c04:mouse:MouseAction.py

<font color=#0000ff>class</font> MouseAction:
  <font color=#0000ff>def</font> __init__(self, action): 
    self.action = action
  <font color=#0000ff>def</font> __str__(self): <font color=#0000ff>return</font> self.action 
  <font color=#0000ff>def</font> __cmp__(self, other):
    <font color=#0000ff>return</font> cmp(self.action, other.action)
  # Necessary when __cmp__ <font color=#0000ff>or</font> __eq__ <font color=#0000ff>is</font> defined
  # <font color=#0000ff>in</font> order to make this <font color=#0000ff>class</font> usable as a
  # dictionary key:
  <font color=#0000ff>def</font> __hash__(self): 
    <font color=#0000ff>return</font> hash(self.action)

# Static fields; an enumeration of instances:
MouseAction.appears = MouseAction(<font color=#004488>"mouse appears"</font>)
MouseAction.runsAway = MouseAction(<font color=#004488>"mouse runs away"</font>)
MouseAction.enters = MouseAction(<font color=#004488>"mouse enters trap"</font>)
MouseAction.escapes = MouseAction(<font color=#004488>"mouse escapes"</font>)
MouseAction.trapped = MouseAction(<font color=#004488>"mouse trapped"</font>)
MouseAction.removed = MouseAction(<font color=#004488>"mouse removed"</font>)
#:~
</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You&#146;ll note that
<B>__cmp__(&#160;)</B> has been overidden to implement a comparison between
<B>action</B> values. Also, each possible move by a mouse is enumerated as a
<B>MouseAction</B> object, all of which are static fields in <B>MouseAction</B>.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_151">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For creating test code, a sequence of
mouse inputs is provided from a text file:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_152">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#:! c04:mouse:MouseMoves.txt
mouse appears
mouse runs away
mouse appears
mouse enters trap
mouse escapes
mouse appears
mouse enters trap
mouse trapped
mouse removed
mouse appears
mouse runs away
mouse appears
mouse enters trap
mouse trapped
mouse removed
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">With these tools in place,
it&#146;s now possible to create the first version of the mousetrap program.
Each <B>State</B> subclass defines its <B>run(&#160;)</B> behavior, and also
establishes its next state with an <B>if-else</B> clause:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_153">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c04:mousetrap1:MouseTrapTest.py
# State Machine pattern using '<font color=#0000ff>if</font>' statements
# to determine the next state.
<font color=#0000ff>import</font> string, sys
sys.path += ['../statemachine', '../mouse']
<font color=#0000ff>from</font> State <font color=#0000ff>import</font> State
<font color=#0000ff>from</font> StateMachine <font color=#0000ff>import</font> StateMachine
<font color=#0000ff>from</font> MouseAction <font color=#0000ff>import</font> MouseAction
# A different subclass <font color=#0000ff>for</font> each state:

<font color=#0000ff>class</font> Waiting(State):
  <font color=#0000ff>def</font> run(self): 
    <font color=#0000ff>print</font> <font color=#004488>"Waiting: Broadcasting cheese smell"</font>

  <font color=#0000ff>def</font> next(self, input):
    <font color=#0000ff>if</font> input == MouseAction.appears:
      <font color=#0000ff>return</font> MouseTrap.luring
    <font color=#0000ff>return</font> MouseTrap.waiting

<font color=#0000ff>class</font> Luring(State):
  <font color=#0000ff>def</font> run(self):
    <font color=#0000ff>print</font> <font color=#004488>"Luring: Presenting Cheese, door open"</font>

  <font color=#0000ff>def</font> next(self, input):
    <font color=#0000ff>if</font> input == MouseAction.runsAway:
      <font color=#0000ff>return</font> MouseTrap.waiting
    <font color=#0000ff>if</font> input == MouseAction.enters:
      <font color=#0000ff>return</font> MouseTrap.trapping
    <font color=#0000ff>return</font> MouseTrap.luring

<font color=#0000ff>class</font> Trapping(State):
  <font color=#0000ff>def</font> run(self):
    <font color=#0000ff>print</font> <font color=#004488>"Trapping: Closing door"</font>

  <font color=#0000ff>def</font> next(self, input):
    <font color=#0000ff>if</font> input == MouseAction.escapes:
      <font color=#0000ff>return</font> MouseTrap.waiting
    <font color=#0000ff>if</font> input == MouseAction.trapped:
      <font color=#0000ff>return</font> MouseTrap.holding
    <font color=#0000ff>return</font> MouseTrap.trapping

<font color=#0000ff>class</font> Holding(State):
  <font color=#0000ff>def</font> run(self):
    <font color=#0000ff>print</font> <font color=#004488>"Holding: Mouse caught"</font>

  <font color=#0000ff>def</font> next(self, input):
    <font color=#0000ff>if</font> input == MouseAction.removed:
      <font color=#0000ff>return</font> MouseTrap.waiting
    <font color=#0000ff>return</font> MouseTrap.holding

<font color=#0000ff>class</font> MouseTrap(StateMachine):
  <font color=#0000ff>def</font> __init__(self): 
    # Initial state
    StateMachine.__init__(self, MouseTrap.waiting)

# Static variable initialization:
MouseTrap.waiting = Waiting()
MouseTrap.luring = Luring()
MouseTrap.trapping = Trapping()
MouseTrap.holding = Holding()

moves = map(string.strip, 
  open(<font color=#004488>"..</font><font color=#004488>/mouse</font><font color=#004488>/MouseMoves.txt"</font>).readlines())
MouseTrap().runAll(map(MouseAction, moves))
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>StateMachine</B> class
simply defines all the possible states as static objects, and also sets up the
initial state. The <B>UnitTest</B> creates a <B>MouseTrap</B> and then tests it
with all the inputs from a <B>MouseMoveList</B>.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_154">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">While the</FONT><FONT FACE="Georgia"> use
of <B>if</B> statements inside the <B>next(&#160;)</B> methods is perfectly
reasonable, managing a large number of these could become difficult. Another
approach is to create tables inside each <B>State</B> object defining the
various next states based on the input.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_155">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Initially, this seems like it ought to be
quite simple. You should be able to define a static table in each <B>State</B>
subclass that defines the transitions in terms of the other <B>State</B>
objects. However, it turns out that this approach generates cyclic
initialization dependencies. To solve the problem, I&#146;ve had to delay the
initialization of the tables until the first time that the <B>next(&#160;)</B>
method is called for a particular <B>State</B> object. Initially, the
<B>next(&#160;)</B> methods can appear a little strange because of this.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_156">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>StateT</B> class is an
implementation of <B>State</B> (so that the same <B>StateMachine</B> class can
be used from the previous example) that adds a <B>Map</B> and a method to
initialize the map from a two-dimensional array. The <B>next(&#160;)</B> method
has a base-class implementation which must be called from the overridden derived
class <B>next(&#160;)</B> methods after they test for a <B>null Map</B> (and
initialize it if it&#146;s <B>null</B>):
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_157">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c04:mousetrap2:MouseTrap2Test.py
# A better mousetrap using tables
<font color=#0000ff>import</font> string, sys
sys.path += ['../statemachine', '../mouse']
<font color=#0000ff>from</font> State <font color=#0000ff>import</font> State
<font color=#0000ff>from</font> StateMachine <font color=#0000ff>import</font> StateMachine
<font color=#0000ff>from</font> MouseAction <font color=#0000ff>import</font> MouseAction

<font color=#0000ff>class</font> StateT(State):
  <font color=#0000ff>def</font> __init__(self):
    self.transitions = None
  <font color=#0000ff>def</font> next(self, input):
    <font color=#0000ff>if</font> self.transitions.has_key(input):
      <font color=#0000ff>return</font> self.transitions[input]
    <font color=#0000ff>else</font>:
      <font color=#0000ff>raise</font> <font color=#004488>"Input not supported for current state"</font>

<font color=#0000ff>class</font> Waiting(StateT):
  <font color=#0000ff>def</font> run(self): 
    <font color=#0000ff>print</font> <font color=#004488>"Waiting: Broadcasting cheese smell"</font>
  <font color=#0000ff>def</font> next(self, input):
    # Lazy initialization:
    <font color=#0000ff>if</font> <font color=#0000ff>not</font> self.transitions:
      self.transitions = { 
        MouseAction.appears : MouseTrap.luring 
      }
    <font color=#0000ff>return</font> StateT.next(self, input)

<font color=#0000ff>class</font> Luring(StateT):
  <font color=#0000ff>def</font> run(self):
    <font color=#0000ff>print</font> <font color=#004488>"Luring: Presenting Cheese, door open"</font>
  <font color=#0000ff>def</font> next(self, input):
    # Lazy initialization:
    <font color=#0000ff>if</font> <font color=#0000ff>not</font> self.transitions:
      self.transitions = {
        MouseAction.enters : MouseTrap.trapping,
        MouseAction.runsAway : MouseTrap.waiting
      }

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久综合九色综合欧美亚洲| 日本美女一区二区| 日韩一区和二区| 在线观看视频91| 色av成人天堂桃色av| 一本色道**综合亚洲精品蜜桃冫 | 一本色道久久综合亚洲91| 不卡欧美aaaaa| 91在线观看成人| 成人avav在线| 欧美性大战xxxxx久久久| 欧美日韩中文另类| 7777女厕盗摄久久久| 91精品国产乱码| 精品久久久久久久人人人人传媒 | 日本道精品一区二区三区| 99久久精品免费精品国产| 一本大道av伊人久久综合| 91官网在线观看| 91精品国产综合久久小美女| 亚洲精品一区二区三区在线观看 | 色综合久久天天综合网| 欧美三片在线视频观看| 精品国产青草久久久久福利| 日本一区免费视频| 亚洲午夜精品在线| 精品在线免费视频| 成人午夜在线播放| 欧美日本乱大交xxxxx| 久久奇米777| 一区在线观看免费| 免费成人在线播放| 91亚洲精品一区二区乱码| 日韩亚洲欧美在线| 中文字幕一区二区三区色视频 | 国产精品成人免费精品自在线观看| 中文字幕一区二区视频| 婷婷综合久久一区二区三区| 国产iv一区二区三区| 欧美浪妇xxxx高跟鞋交| 国产视频在线观看一区二区三区 | 欧美大片免费久久精品三p| 欧美国产欧美亚州国产日韩mv天天看完整| 中文字幕日韩一区| 免费的国产精品| av一本久道久久综合久久鬼色| 欧美猛男超大videosgay| 久久久久久久久久久黄色| 亚洲另类中文字| 日本三级韩国三级欧美三级| 色婷婷一区二区| 欧美激情一二三区| 美女免费视频一区二区| 色婷婷综合久久久久中文 | 久久色.com| 亚洲午夜激情av| 成人精品高清在线| 国产亚洲欧美日韩在线一区| 天天色综合天天| 欧美综合视频在线观看| 国产精品污网站| 蜜臀av在线播放一区二区三区| 色综合久久久久久久久| 国产精品久久久久久亚洲毛片| 美国毛片一区二区| 欧美精品久久一区| 亚洲影院免费观看| 91国偷自产一区二区开放时间 | 五月天中文字幕一区二区| 国产精品综合网| 欧美草草影院在线视频| 天堂蜜桃91精品| 欧美日韩国产天堂| 日韩国产欧美在线观看| 欧美日韩精品是欧美日韩精品| 一区二区三区自拍| 欧美综合在线视频| 性欧美疯狂xxxxbbbb| 欧美精品在欧美一区二区少妇| 一区二区三区在线影院| 欧美日韩一区二区三区四区| 一区二区三区久久| 99国产精品国产精品毛片| 亚洲色图清纯唯美| 欧美午夜精品电影| 亚洲五月六月丁香激情| 欧洲色大大久久| 免费观看成人av| 久久久久久久久岛国免费| 亚洲人成在线观看一区二区| 欧美中文字幕不卡| 成人动漫中文字幕| 国产精品资源网| 久久99蜜桃精品| 亚洲一区电影777| 26uuu色噜噜精品一区二区| 欧美一三区三区四区免费在线看| 99riav久久精品riav| 国产成人一区二区精品非洲| 久久精品国产999大香线蕉| 亚洲国产精品嫩草影院| 亚洲影院久久精品| 亚洲一区二区三区四区五区中文| 国产亚洲欧洲997久久综合 | 91久久国产综合久久| 成人一级视频在线观看| 国产99久久久国产精品潘金网站| 成人免费视频app| 久久99精品久久久久久| 美美哒免费高清在线观看视频一区二区| 亚洲一区二区三区四区在线免费观看 | 国产成人精品www牛牛影视| 免费高清在线视频一区·| 日韩精品久久久久久| 青青草原综合久久大伊人精品优势| 夜夜亚洲天天久久| 亚洲国产视频一区二区| 亚洲国产精品一区二区尤物区| 亚洲激情自拍偷拍| 亚洲观看高清完整版在线观看 | 久久亚洲精华国产精华液| 正在播放一区二区| 欧美日韩精品高清| 色婷婷av一区二区三区大白胸| 亚洲一区二区偷拍精品| 欧美四级电影在线观看| 紧缚奴在线一区二区三区| 亚洲免费在线观看| 久久伊人中文字幕| 欧美三级欧美一级| 99国产麻豆精品| 国产一区二区不卡老阿姨| 午夜视频在线观看一区二区| 亚洲天堂久久久久久久| 国产亚洲精品精华液| 日韩欧美中文一区| 在线观看91av| 色8久久精品久久久久久蜜| 国产精品久久久久精k8| 国产喷白浆一区二区三区| 亚洲三级电影全部在线观看高清| 亚洲综合久久av| 久久国内精品自在自线400部| 国产毛片精品视频| 91精品福利视频| 欧美大片一区二区| 亚洲欧美另类图片小说| 人人爽香蕉精品| 成人av电影观看| 日韩欧美中文一区二区| 国产精品毛片久久久久久久| 五月激情综合婷婷| 成人爱爱电影网址| 7777精品伊人久久久大香线蕉超级流畅 | 亚洲综合激情小说| 日韩高清一区在线| 国产1区2区3区精品美女| 色8久久精品久久久久久蜜| 欧美成人在线直播| 亚洲色图欧洲色图| 免费一级欧美片在线观看| 波多野结衣中文字幕一区 | 亚洲精品乱码久久久久久久久| 午夜精品视频一区| 成人黄色av网站在线| 91精品黄色片免费大全| 亚洲欧洲国产日韩| 久久av中文字幕片| 色女孩综合影院| 国产亚洲精品精华液| 日韩黄色免费电影| 国产一区二区看久久| 91成人看片片| 中文字幕高清一区| 精彩视频一区二区三区| 欧美性色aⅴ视频一区日韩精品| 精品入口麻豆88视频| 国产精品青草综合久久久久99| 婷婷久久综合九色综合绿巨人| av电影在线观看完整版一区二区| 美女视频黄免费的久久| 日韩主播视频在线| 国产成人精品aa毛片| 欧美日韩一区二区三区免费看 | 9人人澡人人爽人人精品| 色婷婷亚洲综合| 日韩欧美一级二级三级久久久| 久久精品日韩一区二区三区| 亚洲美女屁股眼交| 精品一区二区av| www.色精品| 日韩一级大片在线观看| 中文字幕亚洲成人| 日av在线不卡| 在线视频一区二区三| 久久综合网色—综合色88| 亚洲网友自拍偷拍| 国产成人精品网址| 欧美精品免费视频| 亚洲精品乱码久久久久久久久 |