Skip to content

Instantly share code, notes, and snippets.

View herberthamaral's full-sized avatar

Herberth Amaral herberthamaral

View GitHub Profile
@herberthamaral
herberthamaral / nats_to_sse.py
Last active January 3, 2024 14:51
Listen to a nats topic and write to a HTTP client listening via server-sent-events
import asyncio
from typing import AsyncGenerator, Dict
from fastapi import FastAPI
from nats.aio.client import Client as NATS
from nats.aio.client import Msg
from starlette.requests import Request
from starlette.responses import StreamingResponse
app = FastAPI()
@herberthamaral
herberthamaral / requirements.txt
Created October 24, 2011 11:33
spider receita
"""
Simple script to get the citzen name by its CPF (Brazil's SSN)
"""
from BeautifulSoup import BeautifulSoup as bs
import requests
import urllib2
with requests.session() as session:
url = 'http://www.receita.fazenda.gov.br/aplicacoes/atcta/cpf/ConsultaPublica.asp'
response = session.get(url)
@herberthamaral
herberthamaral / README.md
Last active March 24, 2020 14:35
Caso de uso e implementação básica

Pequeno exemplo de estória de usuário (user story) com implementação

Algumas observações a respeito dos arquivos deste gist:

  1. O arquivo Python pode ser executado diretamente para que os testes dentro dele sejam executados;
  2. Por questões de simplicidade, optou-se por deixar o código de produção junto com o código de testes, mas isso não é recomendado em um projeto real -- os dois tipos de código devem ficar separados;
  3. Também por questões de simplicidade, um framework de BDD (como o python-behave) não foi utilizado;
  4. Apenas os dois primeiros cenários foram implementados;
  5. A forma normal de utilizar o unittest é criar uma pasta tests e colocar todos os arquivos lá e utilizar o comando python -m unittest como consta na documentação [1].
@herberthamaral
herberthamaral / audio_thread.py
Created March 24, 2020 14:32
Simulação de escuta de microfone e geração de comando latex usando threads
import queue
import random
import threading
import time
def ouvir_microfone(fila_comandos: queue.Queue) -> None:
# Função produtora da fila
# Esta função simula alguém ditando comandos no microfone, colocando na
# fila de comandos o que foi dito. Gera um comando a cada 5 segundos.
@herberthamaral
herberthamaral / server.py
Created August 19, 2011 20:44
Django + Tornado example
#!/usr/bin/python
import django.core.handlers.wsgi
import os, signal, functools, time
import tornado.httpserver
import tornado.ioloop
import tornado.wsgi
from tornado.options import options, define
try:
define("port", default=8888, help="run on the given port", type=int)
@herberthamaral
herberthamaral / fizzbuzz.py
Created May 24, 2018 16:55
Dojo com fizzbuzz
import unittest
def fizzbuzz(num):
retorno = ''
if num == 0:
return retorno
if num % 3 == 0:
@herberthamaral
herberthamaral / troco-dojo.py
Created May 17, 2018 18:26
Dojo cujo problema é o do mínimo número possível de moedas de um troco
def troco(valor):
retorno = {50: 0, 25: 0, 10:0, 5: 0, 1: 0}
while valor >= 5:
if valor >= 50:
retorno[50] += 1
valor = valor - 50
elif valor >= 25:
retorno[25] = 1
valor = valor - 25
elif valor >= 10:
@herberthamaral
herberthamaral / debug-server.py
Created May 16, 2018 14:05
A simple (even lacking) web server for debugging clients (yes, really).
import json
from werkzeug.wrappers import Request, Response
@Request.application
def application(request):
print('HEADERS:')
for header, value in request.headers.items():
print('{header}: {value}'.format(header=header, value=value))
print('CONTENT:')
@herberthamaral
herberthamaral / install-mono-centos.sh
Last active March 15, 2018 11:31
Install mono-complete from xamarin repositories on CentOS 7.1 x64 (probably works for other versions)
echo "[xamarin]" > /etc/yum.repos.d/xamarin.repo
echo "name=Xamarin" >> /etc/yum.repos.d/xamarin.repo
echo "baseurl=http://download.mono-project.com/repo/centos/" >> /etc/yum.repos.d/xamarin.repo
echo "enabled=1" >> /etc/yum.repos.d/xamarin.repo
echo "gpgcheck=1" >> /etc/yum.repos.d/xamarin.repo
echo "gpgkey=http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF" >> /etc/yum.repos.d/xamarin.repo
yum update
yum install mono-complete
@herberthamaral
herberthamaral / keybase.md
Created March 9, 2018 17:34
keybase proof

Keybase proof

I hereby claim:

  • I am herberthamaral on github.
  • I am herberthamaral (https://keybase.io/herberthamaral) on keybase.
  • I have a public key ASB1HmxiGMufY0PlmTyzmFkHKm6Z1Vz65loVAmN47-Y5zAo

To claim this, I am signing this object: