Skip to content

Instantly share code, notes, and snippets.

View alexandre's full-sized avatar
🎯
Focusing

Alexandre Souza alexandre

🎯
Focusing
View GitHub Profile
@alexandre
alexandre / haskell_drop.py
Created October 4, 2014 23:32
Avaliando o tempo de execução de funções alternativas a função drop do Haskell
#!/usr/bin/python3
import time
def test(fun, *args):
t1 = time.time()
fun(*args)
t2 = time.time()
return 'Exec. time: {}'.format(t1-t2)
def drop_loop(n, _list):
import asyncio
def get_result(result, loop):
print("Que bom que a função me mandou um '{}'".format(result))
loop.call_later(2, print_and_repeat, loop)
def print_and_repeat(loop):
result = 'Como vai?'
@alexandre
alexandre / virtualenv.md
Last active August 26, 2016 18:18
Apenas uma receita de bolo para utilizar virtualenvwrapper em uma distribuição GNU/Linux...

Por que usar?

Com o virtualenv, nós temos um ambiente isolado para cada projeto. E com isso, podemos ter diversas versões diferentes, seja do Python ou do pacote (e.g. Flask). E você precisa instalar o virtualenv apenas para uma versão do Python, já que pode especificar qual versão o seu projeto (ambiente virtual) utilizará. =]

Mas por que o virtualenvwrapper?

Ele facilita bastante tanto a criação quando o acesso ao seu projeto:

  • Ao criar o projeto ele já ativa o seu ambiente virtual;
  • A função workon facilita bastante para acessar o seu "venv" de qualquer diretório
@alexandre
alexandre / sbotools.md
Created October 19, 2014 20:19
receita de bolo para instalar o sbotools no Slackware - testado com slackware64 14.1

Obter pacotes

Todos os pacotes necessários podem ser obtidos pelo http://slackbuilds.org. Para a versão 14.1, por exemplo:

http://slackbuilds.org/repository/14.1/system/sbotools/?search=sbotools

Montando um pequeno shell script

  • "Ctrl + c & Ctrl + v"
@alexandre
alexandre / imm_num.py
Last active August 29, 2015 14:08
numeric types are immutable
>>> x = 10
>>> id(x)
140612933020512
>>> x += 1
>>> id(x)
140612933020544
>>> # numeric types are immutable...
...
>>> id(10)
140612933020512
@alexandre
alexandre / my_own_shuffle.py
Created October 30, 2014 06:50
my own [ugly] shuffle
from random import randint
def my_own_shuffle(seq):
'''Python’s random module includes a function shuffle(data) that accepts a
list of elements and randomly reorders the elements so that each possi-
ble order occurs with equal probability. The random module includes a
more basic function randint(a, b) that returns a uniformly random integer
from a to b (including both endpoints). Using only the randint function,
implement your own version of the shuffle function.
@alexandre
alexandre / sec_ord_arith.py
Created October 30, 2014 06:54
math and python
'''
Demonstrate how to use Python’s list comprehension syntax to produce
the list [0, 2, 6, 12, 20, 30, 42, 56, 72, 90].
cp = current position
list[cp + 1](2) - cp(0) == 2
list[cp +1](6) - cp(2) == 4
@alexandre
alexandre / norm.py
Last active August 29, 2015 14:08
norm...
from math import sqrt
def norm(v, p=None):
'''
The p-norm of a vector v = (v 1 , v 2 , . . . , v n )
in n-dimensional space is de-fined as
||v|| = sqrt(v1**p + v2**p + v3**p + vn**p).
For the special case of p = 2, this results in the traditional Euclidean
norm, which represents the length of the vector. For example, the Eu-
@alexandre
alexandre / word_counter.py
Last active August 29, 2015 14:08
word counter
from itertools import groupby
def word_counter(*words):
'''Write a Python program that inputs a list of words, separated by white-
space, and outputs how many times each word appears in the list.
'''
return {word: len(list(word_group)) for word, word_group in
groupby(sorted(words), key=lambda x: x)}
@alexandre
alexandre / exemplo_testes.md
Last active August 29, 2015 14:08
um exemplo de como [atualmente] eu organizo os meus testes

Exemplo pensando em uma API simples para gerenciar usuários.

Eu tenho uma interface que recebe os requests (e.g. GET, POST) e chama uma função que executa a operação

Eu começo com um teste unitário pensando na função que executará X tarefa.

def test_create_user_invalid_age():
 assert create_user('Foo Jr', 'Test!') == {'error': 'Invalid name'}