Skip to content

Instantly share code, notes, and snippets.

View henriquebastos's full-sized avatar
😏

Henrique Bastos henriquebastos

😏
View GitHub Profile
@henriquebastos
henriquebastos / python-guide.sh
Last active March 27, 2023 19:23
The definitive guide to setup my Python workspace
# The definitive guide to setup my Python workspace
# Author: Henrique Bastos <henrique@bastos.net>
# Updated: 2022-07-14 for reference.
PY3=3.10.4
PY3TOOLS="youtube-dl s3cmd fabric pytest poetry requests"
VENVS=~/.ve
PROJS=~/workspace
@henriquebastos
henriquebastos / __main__.py
Created December 8, 2022 21:23
Wordcount Reference Implementation
import sys
from wordcount.cli import cli
from wordcount.gui import gui
if len(sys.argv) >= 2:
cli()
else:
gui()
from requests_futures.sessions import FuturesSession
def response_for_urls(urls):
"""Asynchronously get response for many urls."""
with FuturesSession() as session:
futures = [
session.get(url) for url in urls
]
@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 / 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))
"""
13. wordcount
Este desafio é um programa que conta palavras de um arquivo qualquer de duas
formas diferentes.
A. Lista todas as palavras por ordem alfabética indicando suas ocorrências.
Ou seja...
"""Geometry"""
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
@henriquebastos
henriquebastos / abcmeta.py
Last active May 27, 2022 00:22
Example of using abc.ABC to enforce implementation of abstractmethods.
import pytest
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def emit_sound(self):
pass
@abstractmethod
@henriquebastos
henriquebastos / surrbyplus0.py
Created May 25, 2022 02:12
Simple exercise to introduce the concept of FSM.
"""
Write a function that determines if all alpha characters in a string
are surrounded (the characters immediately before and after) by a plus sign.
Function should return false if any alpha character present in the string isn't
surrounded by a plus sign. Otherwise the function should return true.
"""
def symbols(input_str: str) -> bool:
"""Fill your code bellow to make all tests pass."""
@henriquebastos
henriquebastos / money.py
Created February 18, 2022 01:31
Uma classe simples para representar dinheiro.
from decimal import Decimal, ROUND_HALF_UP
class Money(Decimal):
def __new__(cls, value=0, decimal_precision=2):
number = super().__new__(cls, value)
if not number._is_precise(decimal_precision):
number = number._round(decimal_precision)
return number