Skip to content

Instantly share code, notes, and snippets.

View ramalho's full-sized avatar
🏠
Working from home

Luciano Ramalho ramalho

🏠
Working from home
View GitHub Profile
@ramalho
ramalho / tombola.py
Last active August 29, 2015 13:58
Solução do Theo Alves
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
@ramalho
ramalho / tombola.py
Created April 8, 2014 23:28
Solução do Daniel Bastos
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
@ramalho
ramalho / tombola.py
Last active August 29, 2015 13:58
Solução do Leonardo Couy
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)
@ramalho
ramalho / gist:10420071
Last active August 29, 2015 13:58
Open issue with Dropbox support
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.
@ramalho
ramalho / statevalue.py
Last active August 29, 2015 14:00
Value object for Pingo.io pin states
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
@ramalho
ramalho / steps.txt
Last active August 29, 2015 14:01
Turtle graphics on the Raspberry Pi
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)
@ramalho
ramalho / gist:46fb0d693c3cf46e70eb
Last active August 29, 2015 14:02
Função para conversão linear de valores de uma faixa de entrada para uma faixa de saída

Função genérica para converter valores de uma faixa de entrada para uma faixa de saída.

Exemplos de uso

Conversão de temperatura agradável de Celsius para Fahrenheit:

>>> conv(22, 0, 100, 32, 212) 71.6

@ramalho
ramalho / constfold.rst
Created July 9, 2014 15:10
Constant folding in Python: think about why the bytecodes generated for f() and g() are different

>>> 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

@ramalho
ramalho / eager_star.py
Created November 17, 2014 10:03
Using *my_iterable when calling a function forces the Python interpreter to eagerly iterate over my_iterable
>>> 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
@ramalho
ramalho / send_builtin.py
Created February 16, 2015 17:59
A ``send`` function to drive coroutines
"""
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)