Skip to content

Instantly share code, notes, and snippets.

View henriquebastos's full-sized avatar
😏

Henrique Bastos henriquebastos

😏
View GitHub Profile
@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()
@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."""
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 / 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
@henriquebastos
henriquebastos / auth.py
Created February 18, 2022 01:30
Implementação de um BaseAuth complexo como exemplo.
from datetime import datetime
from requests.auth import AuthBase
class EduzzAuth(AuthBase):
AUTH_PATH = "/credential/generate_token"
TOKEN_EXPIRED_ERROR_CODE = "#0029"
def __init__(self, email, publickey, apikey, session_class):
@henriquebastos
henriquebastos / code-review-2022-02-17.diff
Created February 18, 2022 00:19
Code Review do Betapp
diff --git a/betapp/betapp/greyhound/utils/racingpost.py b/betapp/betapp/greyhound/utils/racingpost.py
index db9da89..4f976f1 100644
--- a/betapp/betapp/greyhound/utils/racingpost.py
+++ b/betapp/betapp/greyhound/utils/racingpost.py
@@ -4,7 +4,7 @@ import requests
from dataclasses import dataclass
from dateutil import tz
-from typing import List
+from typing import List, get_type_hints
@henriquebastos
henriquebastos / test_session.py
Created February 17, 2022 23:57
Uso do fixture do httpretty
def test_session_uses_custom_json_decoder(httpretty):
httpretty.register_uri(
httpretty.GET, URL, serialize({"datetime": datetime(2021, 12, 4)})
)
response = EduzzSession().get("/")
data = response.json()
assert isinstance(data["datetime"], datetime)
@henriquebastos
henriquebastos / conftest.py
Created February 17, 2022 23:57
Demonstração do uso de fixtures com httpretty.
import pytest
@pytest.fixture
def httpretty(allow_net_connect=False, verbose=False):
import httpretty
httpretty.reset()
httpretty.enable(allow_net_connect=allow_net_connect, verbose=verbose)
import pytest
class IntervalMap:
def __init__(self):
self.limits = []
self.map = {}
def __setitem__(self, upper_bound, value):
self.limits.append(upper_bound)