Skip to content

Instantly share code, notes, and snippets.

View henriquebastos's full-sized avatar
😏

Henrique Bastos henriquebastos

😏
View GitHub Profile
@henriquebastos
henriquebastos / alunos.py
Created April 18, 2019 03:18
Refatoração do código do David Silveira no Live Coding Jam
import sys
from collections import namedtuple
from unittest import TestCase, main as runtests
from enum import Enum
from pprint import pprint
Aluno = namedtuple('Aluno', 'nome notas media conceito')
class Conceito(Enum):
@henriquebastos
henriquebastos / wordcount.py
Created March 28, 2019 17:28
wordcount.py
#!/usr/bin/python -tt
import argparse
import re
from collections import Counter
def read(f):
return re.split(r'\W+', f.read().lower())
def asc(counter):
@henriquebastos
henriquebastos / tudao.py
Created December 5, 2017 00:26
Esboço para o Antonio
# models.py
CREDIT = 'C'
DEBIT = 'D'
class CategoryStatementQuerySet(QuerySet):
def credit():
return self.filter(operacao__exact=CREDIT)
def debit():
@henriquebastos
henriquebastos / extenso.py
Created June 6, 2017 03:16
Exercício muito doido sugerido pelo River
def decomp(num):
"""decomp(120) == (100, 20)"""
base = 10 ** (len(str(num))-1)
divisor, resto = divmod(num, base)
return divisor * base, resto
def radicais(resto):
while resto > 0:
base, resto = decomp(resto)
@henriquebastos
henriquebastos / fizzbuzz.py
Created March 22, 2017 23:30
Código do Coding Dojo p/ Calouros do turno da manhã na Unilasalle
# coding: utf-8
# Código do Coding Dojo p/ Calouros do turno da manhã na Unilasalle
# 21/03/2017
def robo(num):
msg=num
if num==3:
msg='fizz'
if num==5:
@henriquebastos
henriquebastos / fizzbuzz.py
Last active March 22, 2017 23:31
Código do Coding Dojo p/ Calouros do turno da noite na Unilasalle
# coding: utf-8
# Código do Coding Dojo p/ Calouros do turno da noite na Unilasalle
# 21/03/2017
def robo(x):
msg = x
if x == 3 or x == 6:
msg = 'fizz'
return msg
@henriquebastos
henriquebastos / 00-detect-virtualenv-sitepackages.py
Last active October 17, 2022 04:22
IPython startup script to detect and inject VIRTUAL_ENV's site-packages dirs.
"""IPython startup script to detect and inject VIRTUAL_ENV's site-packages dirs.
IPython can detect virtualenv's path and injects it's site-packages dirs into sys.path.
But it can go wrong if IPython's python version differs from VIRTUAL_ENV's.
This module fixes it looking for the actual directories. We use only old stdlib
resources so it can work with as many Python versions as possible.
References:
http://stackoverflow.com/a/30650831/443564
@henriquebastos
henriquebastos / test_image_and_file_field.py
Last active March 24, 2020 19:30
This is a simple proof of concept demonstrating how I like to handle files during tests.
"""
This is a simple proof of concept demonstrating how I like to handle files during tests.
Setup
-----
wget http://hbn.link/1NoLHf0
virtualenv .venv
source .venv/bin/activate
pip install django django-inmemorystorage
def test_html(self):
expectations = [('<form', 1),
('<input', 6),
('type="text"', 3),
('type="email"', 1),
('type="submit"', 1)]
with self.subTest():
for text, count in expectations:
self.assertContains(self.resp, text, count)
@henriquebastos
henriquebastos / secret_gen.py
Created December 23, 2015 13:50
SECRET_KEY generator.
#!/usr/bin/env python
"""
Django SECRET_KEY generator.
"""
from django.utils.crypto import get_random_string
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
print(get_random_string(50, chars))