Skip to content

Instantly share code, notes, and snippets.

@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())
@jsbueno
jsbueno / sync_async.py
Created June 14, 2024 04:45
POC: wrapper for a function that can either run sychrnously or be awaited in an asyncio context:
# here, the function that can work "either way" is the nested "payload".
# The external ^morphix_in_thread" can be a decorator, and "payload" the decorated function
def morphix_in_thread():
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
def payload():