Skip to content

Instantly share code, notes, and snippets.

@jsbueno
jsbueno / gist:ff293bf2b80fa02cb86cee574397d888
Created August 29, 2026 00:40
Python: decorator to transform a function into a compositable function using the `&` operator.
import typing
class Compositable:
def __init__(self, func):
self.func_chain = [func]
def __and__(self, other: typing.Callable | Compositable):
cls = type(self)
composite = cls.__new__(cls)
func_chain = composite.func_chain = self.func_chain.copy()
@jsbueno
jsbueno / gist:52d4098fcda613b48817d672cb60eac9
Created April 22, 2026 22:49
"red square moving" on screen: example during 2026-4-22 livestream
import pygame
tela = pygame.display.set_mode((800, 600))
class Quadrado:
larg = 50
alt = 50
cor = (255, 0, 0)
fundo = (255, 255, 255)
@jsbueno
jsbueno / gist:8aeaa5eea2e7e62d2390264ee8783dad
Created February 27, 2026 14:41
Python3.15 parse Json as deep-imutable data structure:
import json
frozen = json.loads(
json_str, object_hook=lambda dct: frozendict({k: (tuple(v) if isinstance(v, list) else v) for k, v in dct.items()}))
@jsbueno
jsbueno / snippets.py
Last active February 20, 2026 15:47
exploring exception handling and resuming with asyncio.
import random, asyncio, time, os
async def a(n):
global last_task
last_task = asyncio.current_task()
while True:
print(n)
await asyncio.sleep(0.5 + random.random() / 3)
@jsbueno
jsbueno / gist:2ecac2b151e1a0c026b615b331374f17
Created January 15, 2026 23:34
Snippet: uma view de fastapi que usa um middleware pra recuperar um valor do request em uma chamada usada pela view: a view nao precisa saber NADA sobre "quem_chamou".
# pip install fastapi, python-extracontext
from fastapi import FastAPI, Request
from extracontext import ContextLocal
app = FastAPI()
ctx = ContextLocal()
@jsbueno
jsbueno / tkfront.py
Created August 10, 2025 22:48
"Hello world" tkinter app with a fastapi backend
import tkinter, fastapi
import requests
port = 8003
localurl = f"http://localhost:{port}/"
def uimain():
def doit():
endpoint = "transform/"
@jsbueno
jsbueno / monitoring_example.py
Created May 19, 2025 15:33
Example for getting an exception callback even when the exception is handled in inner code:
# This makes use of sys.monitoring, described in PEP 669 and implemented in Python 3.12
import time
import sys
def aha(*args):
print(args)
def worker():
while True:
@jsbueno
jsbueno / orable_streenums.py
Last active March 28, 2025 15:10
Python STrEnum whose members can be joined together with `|` resulting in a set.
from enum import StrEnum
class OrableStrEnum(StrEnum):
"""neat hack: Members can be combined with the `|` operator
That will result in a set - and then the `in` operator
is made to work with a single member as wll, so that
`if CAN_REVIEW in permissions:` will work whether permissions content is
permissions = CAN_REVIEW
@jsbueno
jsbueno / async_copytree.py
Created February 24, 2025 21:49
ssincronous compatible "shutils.copytree" wrapper, allowing for progress indication
"""
This asynchronous version is really naive:
it will block the asyncio loop while walkign the file tree - TWICE -
and then, the defautl call will print the progress to the terminal
pass a callback as "progress" parameter which takes a dictionary
describing the current progress as its sole parameter:
the callback will be called for each file copied during execution.
@jsbueno
jsbueno / raise_as_expression.py
Created September 13, 2024 12:14
Python, raise exception as an expression!
# Usually an exception can be raised with the `raise` statement
#
# however, occasionally it can be useful to be able to do so as part
# of an expression (even if the occasion is code golphing).
# A workaround for that is achievable through a generator's
# `.throw` method:
(_ for _ in ()).throw(Exception())