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
from random import shuffle | |
class Tombola(object): | |
'''Sorteia itens sem repetir''' | |
def carregar(self, seq): | |
self.itens = list(seq) | |
def misturar(self, misturadora=None): | |
if misturadora: # LR: isso funciona mas é melhor sempre testar explicitamente argumento is None |
from random import shuffle | |
class Tombola(object): | |
'''Sorteia itens sem repetir''' | |
def carregar(self, seq): | |
self.itens = list(seq) | |
def misturar(self, pairswap=None): | |
pairswap(self.itens) if pairswap else shuffle(self.itens) # LR: funciona mas é melhor testar explicitamente arg is None |
from random import shuffle | |
class Tombola(object): | |
from inplace import pairswap # LR: esse import é desnecessário, e este é um lugar estranho para fazer imports | |
'''Sorteia itens sem repetir''' | |
def carregar(self, seq): | |
self.itens = list(seq) |
I am a happy paying customer of Dropbox, but the news that you have | |
appointed Condoleezza Rice to your board is extremely disturbing and | |
I will ditch your service if this decision is not reversed. | |
Dr. Rice defends warrantless wiretapping. How am I supposed to trust | |
Dropbox with my personal files if your management cannot understand | |
the implications of this appointment? And if they do understand | |
-- which may well be the case -- then I am very sorry I ever shared | |
any of my files with you. |
class State(object): | |
def __init__(self, value): | |
self.value = value | |
def __repr__(self): | |
return '%s(%r)' % (self.__class__.__name__, self.value) | |
def __str__(self): | |
return state.value |
Double-click LXTerminal and type: | |
$ idle -n | |
(the IDLE window appears) | |
>>> from turtle import * | |
>>> fd(100) | |
(a window with a line appears) | |
How to draw a square: | |
>>> fd(100) | |
>>> rt(90) | |
>>> fd(100) |
>>> 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) |