<s> __author__ = """<STR_LIT>""" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from os import getcwd <EOL> from os import sys <EOL> sys . path . append ( getcwd ( ) ) <EOL> from MOAL . helpers . display import Section <EOL> from MOAL . helpers . display import print_h2 <EOL> from MOAL . helpers . display import print_success <EOL> from MOAL . helpers . display import print_error <EOL> from MOAL . helpers . display import print_info <EOL> from MOAL . helpers . display import divider <EOL> DEBUG = True if __name__ == '<STR_LIT:__main__>' else False <EOL> class InvalidTransition : <EOL> pass <EOL> class FiniteStateMachine ( object ) : <EOL> def __init__ ( self , states = { } , default = None , debug = False , max = <NUM_LIT:20> ) : <EOL> self . debug = debug <EOL> self . max = max <EOL> self . states = { } <EOL> self . default = default <EOL> self . current = self . default <EOL> self . step = <NUM_LIT:0> <EOL> self . string = '<STR_LIT>' <EOL> self . value = '<STR_LIT>' <EOL> for state , data in states . iteritems ( ) : <EOL> self . __setitem__ ( state , data ) <EOL> def __getitem__ ( self , state ) : <EOL> return self . states [ state ] <EOL> def __delitem__ ( self , state ) : <EOL> del self . states [ state ] <EOL> def __setitem__ ( self , state , data ) : <EOL> self . states [ state ] = data <EOL> def __str__ ( self ) : <EOL> for node , data in self . states . iteritems ( ) : <EOL> print ( '<STR_LIT>' . format ( node ) ) <EOL> if type ( data ) != dict : <EOL> divider ( newline = False ) <EOL> continue <EOL> for state , transition in data [ '<STR_LIT>' ] . iteritems ( ) : <EOL> print ( '<STR_LIT>' . format ( state , transition ) ) <EOL> divider ( newline = False ) <EOL> return '<STR_LIT>' <EOL> def reset ( self ) : <EOL> self . step = <NUM_LIT:0> <EOL> self . current = self . default <EOL> def _xform ( self , edge ) : <EOL> """<STR_LIT>""" <EOL> return int ( edge ) <EOL> def _get_transition ( self , edge ) : <EOL> try : <EOL> return self . states [ self . current ] [ '<STR_LIT>' ] [ self . _xform ( edge ) ] <EOL> except KeyError : <EOL> raise InvalidTransition <EOL> def _get_current ( self ) : <EOL> try : <EOL> return self . current , self . states [ self . current ] <EOL> except KeyError : <EOL> return None , None <EOL> def _print ( self , edge , old , new ) : <EOL> print ( '<STR_LIT>' . format ( <EOL> self . step * '<STR_LIT:U+0020>' , edge , old , new ) ) <EOL> def transition ( self , edge ) : <EOL> old , old_data = self . _get_current ( ) <EOL> self . current = self . _get_transition ( edge ) <EOL> new , new_data = self . _get_current ( ) <EOL> self . _print ( edge , old , new ) <EOL> return new_data <EOL> def get_next_value ( self , node , edge ) : <EOL> if node not in self . states : <EOL> return None <EOL> _node = self . states [ node ] [ '<STR_LIT>' ] <EOL> if _node is None or edge not in _node : <EOL> return None <EOL> return _node [ edge ] <EOL> def _set_current ( self , node_label ) : <EOL> self . current = node_label <EOL> class Acceptor ( FiniteStateMachine ) : <EOL> def test ( self , string ) : <EOL> try : <EOL> print_info ( '<STR_LIT:{}>' . format ( string ) , prefix = '<STR_LIT>' ) <EOL> chars = list ( str ( string ) ) <EOL> for char in chars : <EOL> self . step += <NUM_LIT:1> <EOL> self . transition ( char ) <EOL> self . reset ( ) <EOL> print_success ( '<STR_LIT>' . join ( chars ) , prefix = '<STR_LIT>' ) <EOL> return True <EOL> except InvalidTransition : <EOL> print_error ( '<STR_LIT>' . join ( chars ) , prefix = '<STR_LIT>' ) <EOL> return False <EOL> class Classifier ( Acceptor ) : <EOL> """<STR_LIT>""" <EOL> def classify ( self , * args , ** kwargs ) : <EOL> return super ( Classifier , self ) . test ( * args , ** kwargs ) <EOL> if DEBUG : <EOL> with Section ( '<STR_LIT>' ) : <EOL> print_h2 ( '<STR_LIT>' ) <EOL> acceptor = Acceptor ( <EOL> states = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : { <NUM_LIT:0> : '<STR_LIT>' , <NUM_LIT:1> : '<STR_LIT>' } , '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : { <NUM_LIT:0> : '<STR_LIT>' , <NUM_LIT:1> : '<STR_LIT>' } , '<STR_LIT>' : '<STR_LIT>' } } , <EOL> default = '<STR_LIT>' ) <EOL> assert acceptor . test ( '<STR_LIT>' ) <EOL> assert acceptor . test ( '<STR_LIT>' ) <EOL> assert not acceptor . test ( '<STR_LIT>' ) <EOL> assert acceptor . test ( '<STR_LIT>' ) <EOL> assert acceptor . test ( '<STR_LIT:0>' ) <EOL> assert acceptor . test ( '<STR_LIT:1>' ) </s>
