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 / file_exception.py
Last active September 7, 2015 21:51
Simple file handling exception example
while True:
filename = input('Enter the name of a file to open: ')
try:
# open for reading in binary mode, so any file will do
fin = open(filename, 'rb')
except OSError as exc:
print('Something untoward happened [%r]' % exc)
else:
break
@ramalho
ramalho / simple_exception_example.py
Created September 7, 2015 21:29
Simple exception example
while True:
response = input('Enter a number: ')
try:
n = float(response)
except ValueError:
print('%r is not a number' % response)
else:
break
print('Nice number. Thanks!')
def backtracking_sort(input_list, output_list, level):
print(level * ' ', input_list, output_list);
# Reject - this path doesn't lead to any solution
if len(output_list) > 1 and output_list[-2] > output_list[-1]:
return False
# Accept - one solution
@ramalho
ramalho / objetojs.py
Last active August 29, 2015 14:21
ObjetoJS: emulação básica de um objeto JavaScript
"""
=========================================================
Classe ObjetoJS: imitação simples de um objeto JavaScript
=========================================================
Uma instância é construída passando argumentos nomeados:
>>> o = ObjetoJS(z=33, x=11, y=22)
A representação textual de uma instância parece a chamada do
"""
romanos.py - arabicos para romanos
>>> ara2rom(1)
'I'
>>> ara2rom(2)
'II'
>>> ara2rom(3)
'III'
@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
@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 / 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 / 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 / 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)