update from Atlas
This commit is contained in:
87
attic/control/adder/coroadder.py
Normal file
87
attic/control/adder/coroadder.py
Normal file
@@ -0,0 +1,87 @@
|
||||
"""
|
||||
|
||||
>>> adder = adder_coro()
|
||||
>>> next(adder)
|
||||
0
|
||||
>>> adder.send(10)
|
||||
10
|
||||
>>> adder.send(20)
|
||||
30
|
||||
>>> adder.send(30)
|
||||
60
|
||||
>>> try:
|
||||
... next(adder)
|
||||
... except StopIteration as exc:
|
||||
... result = exc.value
|
||||
...
|
||||
>>> result
|
||||
Result(sum=60, terms=3, average=20.0)
|
||||
|
||||
|
||||
Closing a coroutine:
|
||||
|
||||
>>> adder = adder_coro()
|
||||
>>> next(adder)
|
||||
0
|
||||
>>> adder.send(1)
|
||||
1
|
||||
>>> adder.send(10)
|
||||
11
|
||||
>>> adder.close()
|
||||
>>> try:
|
||||
... next(adder)
|
||||
... except StopIteration as exc:
|
||||
... exc.value is None
|
||||
...
|
||||
True
|
||||
|
||||
"""
|
||||
|
||||
import sys
|
||||
import collections
|
||||
|
||||
Result = collections.namedtuple('Result', 'sum terms average')
|
||||
|
||||
|
||||
def adder_coro(initial=0):
|
||||
total = initial
|
||||
count = 0
|
||||
while True:
|
||||
try:
|
||||
term = yield total
|
||||
except GeneratorExit:
|
||||
break
|
||||
if term is None:
|
||||
break
|
||||
total += term
|
||||
count += 1
|
||||
return Result(total, count, total/count)
|
||||
|
||||
|
||||
def prompt():
|
||||
while True:
|
||||
try:
|
||||
term = float(input('+ '))
|
||||
except ValueError:
|
||||
break
|
||||
yield term
|
||||
|
||||
|
||||
def main(get_terms):
|
||||
adder = adder_coro()
|
||||
next(adder)
|
||||
for term in get_terms:
|
||||
adder.send(term)
|
||||
try:
|
||||
next(adder)
|
||||
except StopIteration as exc:
|
||||
result = exc.value
|
||||
print(result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) > 1:
|
||||
get_terms = (float(n) for n in sys.argv[1:])
|
||||
else:
|
||||
get_terms = prompt()
|
||||
main(get_terms)
|
||||
43
attic/control/adder/coroadder0.py
Normal file
43
attic/control/adder/coroadder0.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""
|
||||
Closing a generator raises ``GeneratorExit`` at the pending ``yield``
|
||||
|
||||
>>> adder = adder_coro()
|
||||
>>> next(adder)
|
||||
0
|
||||
>>> adder.send(10)
|
||||
10
|
||||
>>> adder.send(20)
|
||||
30
|
||||
>>> adder.send(30)
|
||||
60
|
||||
>>> adder.close()
|
||||
-> total: 60 terms: 3 average: 20.0
|
||||
|
||||
|
||||
Other exceptions propagate to the caller:
|
||||
|
||||
>>> adder = adder_coro()
|
||||
>>> next(adder)
|
||||
0
|
||||
>>> adder.send(10)
|
||||
10
|
||||
>>> adder.send('spam')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
|
||||
|
||||
|
||||
"""
|
||||
|
||||
def adder_coro(initial=0):
|
||||
total = initial
|
||||
count = 0
|
||||
try:
|
||||
while True:
|
||||
term = yield total
|
||||
total += term
|
||||
count += 1
|
||||
except GeneratorExit:
|
||||
average = total / count
|
||||
msg = '-> total: {} terms: {} average: {}'
|
||||
print(msg.format(total, count, average))
|
||||
96
attic/control/adder/coroadder_deco.py
Normal file
96
attic/control/adder/coroadder_deco.py
Normal file
@@ -0,0 +1,96 @@
|
||||
"""
|
||||
|
||||
>>> adder = adder_coro()
|
||||
>>> next(adder)
|
||||
0
|
||||
>>> adder.send(10)
|
||||
10
|
||||
>>> adder.send(20)
|
||||
30
|
||||
>>> adder.send(30)
|
||||
60
|
||||
>>> try:
|
||||
... next(adder)
|
||||
... except StopIteration as exc:
|
||||
... result = exc.value
|
||||
...
|
||||
>>> result
|
||||
Result(sum=60, terms=3, average=20.0)
|
||||
|
||||
|
||||
Closing a coroutine:
|
||||
|
||||
>>> adder = adder_coro()
|
||||
>>> next(adder)
|
||||
0
|
||||
>>> adder.send(1)
|
||||
1
|
||||
>>> adder.send(10)
|
||||
11
|
||||
>>> adder.close()
|
||||
>>> try:
|
||||
... next(adder)
|
||||
... except StopIteration as exc:
|
||||
... exc.value is None
|
||||
...
|
||||
True
|
||||
|
||||
"""
|
||||
|
||||
import sys
|
||||
import collections
|
||||
|
||||
|
||||
def coroutine(func):
|
||||
def primed_coroutine(*args, **kwargs):
|
||||
coro = func(*args, **kwargs)
|
||||
next(coro)
|
||||
return coro
|
||||
return primed_coroutine
|
||||
|
||||
|
||||
Result = collections.namedtuple('Result', 'sum terms average')
|
||||
|
||||
|
||||
@coroutine
|
||||
def adder_coro(initial=0):
|
||||
total = initial
|
||||
num_terms = 0
|
||||
while True:
|
||||
try:
|
||||
term = yield total
|
||||
except GeneratorExit:
|
||||
break
|
||||
if term is None:
|
||||
break
|
||||
total += term
|
||||
num_terms += 1
|
||||
return Result(total, num_terms, total/num_terms)
|
||||
|
||||
|
||||
def prompt():
|
||||
while True:
|
||||
try:
|
||||
term = float(input('+ '))
|
||||
except ValueError:
|
||||
break
|
||||
yield term
|
||||
|
||||
|
||||
def main(get_terms):
|
||||
adder = adder_coro()
|
||||
for term in get_terms:
|
||||
adder.send(term)
|
||||
try:
|
||||
adder.send(None)
|
||||
except StopIteration as exc:
|
||||
result = exc.value
|
||||
print(result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) > 1:
|
||||
get_terms = (float(n) for n in sys.argv[1:])
|
||||
else:
|
||||
get_terms = prompt()
|
||||
main(get_terms)
|
||||
39
attic/control/adder/soma.py
Normal file
39
attic/control/adder/soma.py
Normal file
@@ -0,0 +1,39 @@
|
||||
|
||||
if 'raw_input' in dir(__builtins__):
|
||||
input = raw_input # para funcionar com Python 2
|
||||
|
||||
def ler_num():
|
||||
num = input('+: ')
|
||||
try:
|
||||
num = float(num)
|
||||
except ValueError:
|
||||
return 0
|
||||
return num
|
||||
|
||||
def somadora():
|
||||
qt_parcelas = 0
|
||||
total = 0
|
||||
try:
|
||||
while True:
|
||||
parcela = yield
|
||||
qt_parcelas += 1
|
||||
total += parcela
|
||||
print('parcelas: %d total: %d' % (qt_parcelas, total))
|
||||
|
||||
finally:
|
||||
print('parcelas: %d total: %d media: %d' % (qt_parcelas, total, total/qt_parcelas))
|
||||
|
||||
def main():
|
||||
coro = somadora()
|
||||
next(coro)
|
||||
while True:
|
||||
item = ler_num()
|
||||
if item:
|
||||
coro.send(item)
|
||||
else:
|
||||
print('Fechando corotina...')
|
||||
coro.close()
|
||||
break
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
47
attic/control/adder/soma_deco.py
Normal file
47
attic/control/adder/soma_deco.py
Normal file
@@ -0,0 +1,47 @@
|
||||
|
||||
if 'raw_input' in dir(__builtins__):
|
||||
input = raw_input # para funcionar com Python 2
|
||||
|
||||
def ler_parcela():
|
||||
parcela = input('+: ')
|
||||
try:
|
||||
parcela = float(parcela)
|
||||
except ValueError:
|
||||
return 0
|
||||
return parcela
|
||||
|
||||
# decorator
|
||||
def coro(func):
|
||||
def start(*args, **kwargs):
|
||||
g = func(*args, **kwargs)
|
||||
next(g)
|
||||
return g
|
||||
return start
|
||||
|
||||
@coro
|
||||
def somadora():
|
||||
qt_parcelas = 0
|
||||
total = 0
|
||||
try:
|
||||
while True:
|
||||
parcela = yield
|
||||
qt_parcelas += 1
|
||||
total += parcela
|
||||
|
||||
print('parcelas: %d total: %d' % (qt_parcelas, total))
|
||||
finally:
|
||||
print('parcelas: %d total: %d media: %d' % (qt_parcelas, total, total/qt_parcelas))
|
||||
|
||||
def main():
|
||||
coro = somadora()
|
||||
while True:
|
||||
parcela = ler_parcela()
|
||||
if parcela:
|
||||
coro.send(parcela)
|
||||
else:
|
||||
print('Fechando corotina...')
|
||||
coro.close()
|
||||
break
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
47
attic/control/adder/yetanother.py
Normal file
47
attic/control/adder/yetanother.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import sys
|
||||
import collections
|
||||
|
||||
Result = collections.namedtuple('Result', 'total average')
|
||||
|
||||
def adder():
|
||||
total = 0
|
||||
count = 0
|
||||
while True:
|
||||
term = yield
|
||||
try:
|
||||
term = float(term)
|
||||
except (ValueError, TypeError):
|
||||
break
|
||||
else:
|
||||
total += term
|
||||
count += 1
|
||||
return Result(total, total/count)
|
||||
|
||||
def process_args(coro, args):
|
||||
for arg in args:
|
||||
coro.send(arg)
|
||||
try:
|
||||
next(coro)
|
||||
except StopIteration as exc:
|
||||
return exc.value
|
||||
|
||||
|
||||
def prompt(coro):
|
||||
while True:
|
||||
term = input('+> ')
|
||||
try:
|
||||
coro.send(term)
|
||||
except StopIteration as exc:
|
||||
return exc.value
|
||||
|
||||
|
||||
def main():
|
||||
coro = adder()
|
||||
next(coro) # prime it
|
||||
if len(sys.argv) > 1:
|
||||
res = process_args(coro, sys.argv[1:])
|
||||
else:
|
||||
res = prompt(coro)
|
||||
print(res)
|
||||
|
||||
main()
|
||||
42
attic/control/adder/yield_from_input.py
Normal file
42
attic/control/adder/yield_from_input.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import sys
|
||||
|
||||
|
||||
def ask():
|
||||
prompt = '>'
|
||||
while True:
|
||||
response = input(prompt)
|
||||
if not response:
|
||||
return 0
|
||||
yield response
|
||||
|
||||
|
||||
def parse_args():
|
||||
yield from iter(sys.argv[1:])
|
||||
|
||||
|
||||
def fetch(producer):
|
||||
gen = producer()
|
||||
next(gen)
|
||||
yield from gen
|
||||
|
||||
|
||||
def main(args):
|
||||
if args:
|
||||
producer = parse_args
|
||||
else:
|
||||
producer = ask
|
||||
|
||||
total = 0
|
||||
count = 0
|
||||
gen = fetch(producer())
|
||||
while True:
|
||||
term = yield from gen
|
||||
term = float(term)
|
||||
total += term
|
||||
count += 1
|
||||
average = total / count
|
||||
print('total: {} average: {}'.format(total, average))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv[1:])
|
||||
Reference in New Issue
Block a user