<s> from calvin . actor . actor import Actor , ActionResult , manage , condition , guard <EOL> from calvin . runtime . north . calvin_token import EOSToken , ExceptionToken <EOL> class Dict ( Actor ) : <EOL> """<STR_LIT>""" <EOL> @ manage ( [ '<STR_LIT:n>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def init ( self , n = <NUM_LIT:1> ) : <EOL> self . n = n if n > <NUM_LIT:0> else <NUM_LIT:0> <EOL> self . _dict = { } <EOL> self . done = False <EOL> def _bail ( self ) : <EOL> self . _dict = ExceptionToken ( ) <EOL> self . done = True <EOL> def exception_handler ( self , action , args , context ) : <EOL> if self . n or not ( '<STR_LIT:key>' in context [ '<STR_LIT>' ] and '<STR_LIT:value>' in context [ '<STR_LIT>' ] ) : <EOL> self . _bail ( ) <EOL> self . done = True <EOL> return ActionResult ( ) <EOL> @ condition ( [ '<STR_LIT:key>' , '<STR_LIT:value>' ] , [ ] ) <EOL> @ guard ( lambda self , key , value : not self . n and not self . done ) <EOL> def add_entry_EOS ( self , key , value ) : <EOL> if isinstance ( key , basestring ) : <EOL> self . _dict [ key ] = value <EOL> else : <EOL> self . _bail ( ) <EOL> return ActionResult ( ) <EOL> @ condition ( [ '<STR_LIT:key>' , '<STR_LIT:value>' ] , [ ] ) <EOL> @ guard ( lambda self , key , value : self . n and not self . done ) <EOL> def add_entry ( self , key , value ) : <EOL> if isinstance ( key , basestring ) : <EOL> self . _dict [ key ] = value <EOL> self . done = bool ( len ( self . _dict ) == self . n ) <EOL> else : <EOL> self . _bail ( ) <EOL> return ActionResult ( ) <EOL> @ condition ( [ ] , [ '<STR_LIT>' ] ) <EOL> @ guard ( lambda self : self . done ) <EOL> def produce_dict ( self ) : <EOL> res = self . _dict <EOL> self . done = False <EOL> self . _dict = { } <EOL> return ActionResult ( production = ( res , ) ) <EOL> action_priority = ( produce_dict , add_entry , add_entry_EOS ) <EOL> test_set = [ <EOL> { <EOL> '<STR_LIT>' : { '<STR_LIT:key>' : [ "<STR_LIT:a>" , "<STR_LIT:b>" ] , '<STR_LIT:value>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ { "<STR_LIT:a>" : <NUM_LIT:1> } , { "<STR_LIT:b>" : <NUM_LIT:2> } ] } , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : [ lambda self : self . init ( n = <NUM_LIT:2> ) ] , <EOL> '<STR_LIT>' : { '<STR_LIT:key>' : [ "<STR_LIT:a>" , "<STR_LIT:b>" ] , '<STR_LIT:value>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ { "<STR_LIT:a>" : <NUM_LIT:1> , "<STR_LIT:b>" : <NUM_LIT:2> } ] } , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : [ lambda self : self . init ( n = <NUM_LIT:0> ) ] , <EOL> '<STR_LIT>' : { '<STR_LIT:key>' : [ "<STR_LIT:a>" , "<STR_LIT:b>" , EOSToken ( ) ] , '<STR_LIT:value>' : [ <NUM_LIT:1> , <NUM_LIT:2> , EOSToken ( ) ] } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ { "<STR_LIT:a>" : <NUM_LIT:1> , "<STR_LIT:b>" : <NUM_LIT:2> } ] } , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : [ lambda self : self . init ( n = <NUM_LIT:0> ) ] , <EOL> '<STR_LIT>' : { '<STR_LIT:key>' : [ "<STR_LIT:a>" , EOSToken ( ) ] , '<STR_LIT:value>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : [ lambda self : self . init ( n = <NUM_LIT:2> ) ] , <EOL> '<STR_LIT>' : { '<STR_LIT:key>' : [ "<STR_LIT:a>" , <NUM_LIT:1> , "<STR_LIT:b>" , "<STR_LIT:c>" ] , '<STR_LIT:value>' : [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT> ] } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT>' , { "<STR_LIT:b>" : <NUM_LIT:30> , "<STR_LIT:c>" : <NUM_LIT> } ] } , <EOL> } , <EOL> ] </s>
