Função genérica para converter valores de uma faixa de entrada para uma faixa de saída.
Conversão de temperatura agradável de Celsius para Fahrenheit:
>>> conv(22, 0, 100, 32, 212) 71.6
>>> import dis
>>> def f(x):
... return 1+2+3+4+x
...
>>> f(5)
15
>>> dis.dis(f)
2 0 LOAD_CONST 7 (10)
3 LOAD_FAST 0 (x)
6 BINARY_ADD| >>> def prange(n): | |
| ... for i in range(n): | |
| ... print('->', i) | |
| ... yield i | |
| ... | |
| >>> def f(a, b, c): | |
| ... print((a, b, c)) | |
| ... | |
| >>> f(*prange(3)) | |
| -> 0 |
| """ | |
| A ``send`` function to drive coroutines. | |
| Driving a coroutine that does not yield interesting values: | |
| >>> coro_printer = printer() | |
| >>> _ = send(coro_printer, 10) | |
| got -> 10 | |
| >>> _ = send(coro_printer, 20) |
| # Code below is the expansion of the statement: | |
| # | |
| # RESULT = yield from EXPR | |
| # | |
| # Copied verbatim from the Formal Semantics section of | |
| # PEP 380 -- Syntax for Delegating to a Subgenerator | |
| # | |
| # https://www.python.org/dev/peps/pep-0380/#formal-semantics | |
| _i = iter(EXPR) |
| #!/usr/bin/env python3 | |
| # Copyright 2014 Brett Slatkin, Pearson Education Inc. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # |
| # spinner_asyncio.py | |
| # credits: Example by Luciano Ramalho inspired by | |
| # Michele Simionato's multiprocessing example | |
| # source: | |
| # http://python-3-patterns-idioms-test.readthedocs.org/en/latest/CoroutinesAndConcurrency.html | |
| import sys | |
| import asyncio |
| import pingo | |
| from time import sleep | |
| board = pingo.detect.MyBoard() | |
| led = board.pins[13] | |
| led.mode = pingo.OUT | |
| while True: | |
| led.toggle() | |
| sleep(1) |
| import pingo | |
| from time import sleep | |
| board = pingo.detect.MyBoard() | |
| pin = board.pins[13] | |
| pin.mode = pingo.OUT | |
| led = pingo.parts.Led(pin) | |
| led.blink(10, .5, .1) # new thread |
| """ | |
| romanos.py - arabicos para romanos | |
| >>> ara2rom(1) | |
| 'I' | |
| >>> ara2rom(2) | |
| 'II' | |
| >>> ara2rom(3) | |
| 'III' |