This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from abc import ABC | |
from typing import ClassVar, Generic, Literal, NoReturn, Protocol, TypeVar, overload | |
_OutputT = TypeVar("_OutputT") | |
class HasStdout(Protocol): | |
"""Protocol for objects that store the stdout buffer in memory.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import pyaudio | |
import wave | |
class AudioRecorder: | |
FORMAT = pyaudio.paInt32 | |
CHANNELS = 1 | |
RATE = 44100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lru_cache é um decorador que faz um cache das suas chamadas a funções. | |
# Isso quer dizer que quando você chama uma função, ele memoriza quais | |
# argumentos foram passados. Se você chama a mesma função com os mesmos | |
# argumentos, ao invés de recalcular a função, ele imediatamente retorna | |
# o resultado da última chamada à função com os mesmos argumentos. | |
# Isso torna algumas funções muito, muito mais rápidas. | |
# Vale a pena usar o lru_cache quando uma função é chamada várias vezes com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
waiting_time = 3 | |
to_be_reversed = input('O que você quer inverter?\n') # Meu cachorro foi na missa | |
print('Processando', end='') | |
for i in range(10): | |
print('.', end='', flush=True) | |
time.sleep(waiting_time/10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s1 = 'abacate' | |
s2 = 'palmeiras' | |
def g(s1, s2): | |
d = [dict(zip([i for i in range(len(f))], [x for x in f])) for f in [s1, s2]] | |
for i in range(max([len(r) for r in d])): | |
for k in [False, True]: | |
yield d[k].get(i, '') | |
print(''.join([f for f in g(s1, s2)])) # apbaalcmaetieras |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ok(): | |
print('hello, world!') | |
ok() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
txt = r""" | |
import machine | |
import time | |
led = machine.Pin(2, machine.Pin.OUT) | |
while True: | |
led.value(0) | |
time.sleep(0.5) | |
led.value(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s1 = '13579' | |
s2 = '24680' | |
s3 = ''.join([s1[i//2] if i % 2 == 0 else s2[i//2] for i in range(len(s1 + s2))]) | |
print(s3) # 1234567890 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bs4 | |
soup = bs4.BeautifulSoup(r.text, 'html.parser') # Criar uma "sopa" com o HTML que buscamos | |
# Aqui procuramos o elemento table, que contém os tr em que estamos interessados | |
# A função find acha só o primeiro elemento que corresponde ao indicado | |
table = soup.find(lambda tag: tag.name == 'table') | |
# O find_all, em contraste, retorna uma lista com todos os elementos que correspondem | |
# ao que pedimos e retorna uma lista com eles. Note que agora ao invés de procurarmos | |
# na sopa (página toda), procuramos só entre os elementos do table que achamos |