Skip to content

Instantly share code, notes, and snippets.

@edcrypt
Created August 30, 2017 21:19
Show Gist options
  • Select an option

  • Save edcrypt/f253822512c4e073fb5747695902fa5b to your computer and use it in GitHub Desktop.

Select an option

Save edcrypt/f253822512c4e073fb5747695902fa5b to your computer and use it in GitHub Desktop.

Revisions

  1. edcrypt created this gist Aug 30, 2017.
    31 changes: 31 additions & 0 deletions coro_account.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@



    def Account(balance):
    ret = None
    while True:
    # receive message
    selector, *args = yield ret
    # dispatch
    if selector == 'withdraw':
    print('Got withdraw')
    new_balance = balance - args[0]
    if new_balance < 0:
    ret = 'NO LIMIT'
    else:
    balance = new_balance
    ret = balance
    elif selector == 'deposit':
    print('Got deposit')
    balance += args[0]
    ret = balance

    account = Account(100)

    # init...
    next(account)

    print(account.send(['withdraw', 7]))
    print(account.send(['deposit', 8]))