<s> import random <EOL> import time <EOL> from guild . actor import * <EOL> from guild . stm import Store , ConcurrentUpdate , BusyRetry , retry <EOL> class InsufficientFunds ( ActorException ) : <EOL> pass <EOL> class Account ( object ) : <EOL> def __init__ ( self , balance = <NUM_LIT:10> ) : <EOL> self . account_info = Store ( ) <EOL> curr_balance = self . account_info . checkout ( "<STR_LIT>" ) <EOL> curr_balance . set ( balance ) <EOL> curr_balance . commit ( ) <EOL> @ retry <EOL> def deposit ( self , amount ) : <EOL> curr_balance = self . account_info . checkout ( "<STR_LIT>" ) <EOL> new_balance = curr_balance . value + amount <EOL> curr_balance . set ( new_balance ) <EOL> curr_balance . commit ( ) <EOL> return new_balance <EOL> @ retry <EOL> def withdraw ( self , amount ) : <EOL> curr_balance = self . account_info . checkout ( "<STR_LIT>" ) <EOL> if curr_balance . value < amount : <EOL> raise InsufficientFunds ( "<STR_LIT>" , <EOL> requested = amount , <EOL> balance = curr_balance . value ) <EOL> new_balance = curr_balance . value - amount <EOL> curr_balance . set ( new_balance ) <EOL> curr_balance . commit ( ) <EOL> return amount <EOL> @ property <EOL> def balance ( self ) : <EOL> curr_balance = self . account_info . checkout ( "<STR_LIT>" ) <EOL> return curr_balance . value <EOL> def transfer ( amount , payer , payee ) : <EOL> funds = payer . withdraw ( amount ) <EOL> payee . deposit ( funds ) <EOL> class MischiefMaker ( Actor ) : <EOL> def __init__ ( self , myaccount , friendsaccount , name ) : <EOL> super ( MischiefMaker , self ) . __init__ ( ) <EOL> self . myaccount = myaccount <EOL> self . friendsaccount = friendsaccount <EOL> self . grabbed = <NUM_LIT:0> <EOL> self . name = name <EOL> @ process_method <EOL> def process ( self ) : <EOL> try : <EOL> grab = random . randint ( <NUM_LIT:1> , <NUM_LIT:10> ) * <NUM_LIT:10> <EOL> transfer ( grab , self . friendsaccount , self . myaccount ) <EOL> except InsufficientFunds as e : <EOL> print "<STR_LIT>" , e . balance , "<STR_LIT:<>" , e . requested <EOL> self . stop ( ) <EOL> return <EOL> self . grabbed = self . grabbed + grab <EOL> account1 = Account ( <NUM_LIT:1000> ) <EOL> account2 = Account ( <NUM_LIT:1000> ) <EOL> barney = MischiefMaker ( account2 , account1 , "<STR_LIT>" ) . go ( ) <EOL> fred = MischiefMaker ( account1 , account2 , "<STR_LIT>" ) . go ( ) <EOL> wait_for ( fred , barney ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , fred . grabbed <EOL> print "<STR_LIT>" , barney . grabbed <EOL> print "<STR_LIT>" , fred . grabbed + barney . grabbed <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , account1 . balance , account2 . balance <EOL> print "<STR_LIT>" , account1 . balance + account2 . balance </s>
