Skip to content

Instantly share code, notes, and snippets.

View ramalho's full-sized avatar
🚄
operador de trem

Luciano Ramalho ramalho

🚄
operador de trem
View GitHub Profile
@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)
@ramalho
ramalho / yield_from_expansion.py
Created February 18, 2015 21:11
Expansion of the `yield from` statement, from PEP 380 -- Syntax for Delegating to a Subgenerator
# 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)
@ramalho
ramalho / coro_life.py
Last active December 6, 2024 10:20
John Conway's Game of Life implemented with coroutines, by Brett Slatkin
#!/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
#
@ramalho
ramalho / spinner_asyncio.py
Created March 15, 2015 12:36
Simple asyncio demo adapted from a multiprocessing demo by Michele Simionato
# 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
@ramalho
ramalho / blink1.py
Created March 28, 2015 11:52
Blink with Pingo
import pingo
from time import sleep
board = pingo.detect.MyBoard()
led = board.pins[13]
led.mode = pingo.OUT
while True:
led.toggle()
sleep(1)
@ramalho
ramalho / blink2.py
Created March 28, 2015 11:56
Blink with Pingo using a parts.Led
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'