Skip to content

Instantly share code, notes, and snippets.

View tiagocoutinho's full-sized avatar

Jose Tiago Macara Coutinho tiagocoutinho

View GitHub Profile
@tiagocoutinho
tiagocoutinho / game_of_life.zig
Last active August 5, 2026 09:34
Game of life in zig
// zig build-exe -O ReleaseSmall -fno-unwind-tables -fsingle-threaded game_of_life.zig
const std = @import("std");
const ROWS: usize = 20;
const COLS: usize = 20;
fn step(board: *[ROWS][COLS]u8) void {
var next: [ROWS][COLS]u8 = undefined;
for (0..ROWS) |y| {
@tiagocoutinho
tiagocoutinho / game_of_life.py
Created August 5, 2026 05:53
Game of life in python
import time
import numpy as np
def step(board):
padded = np.pad(board, 1, mode="wrap")
neighbors = (
padded[:-2, :-2]
+ padded[:-2, 1:-1]
+ padded[:-2, 2:]
+ padded[1:-1, :-2]
@tiagocoutinho
tiagocoutinho / webrtc.html
Created October 29, 2025 13:42
WebRTC browser, no signaling
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>WebRTC Demo</title>
<style>
textarea { width: 100%; height: 100px;}
.video-player { border: 2px solid greenyellow; background-color: gray; width: 100%; }
</style>
@tiagocoutinho
tiagocoutinho / webrtc.py
Created October 28, 2025 16:57
Example WebRTC http app
# pip install aiortc aiohttp opencv-python
import argparse
import asyncio
import logging
from aiohttp import web
from aiortc import RTCPeerConnection, RTCSessionDescription
from aiortc.contrib.media import MediaPlayer, MediaRelay
@tiagocoutinho
tiagocoutinho / yield.py
Created October 15, 2025 15:09
TCP client CLI with yield example in python
import socket
def make_stream(addr):
c = socket.create_connection(("0",5001))
print("Connected!")
reply = c
while True:
request = yield reply
if not request.strip():
break
@tiagocoutinho
tiagocoutinho / fastapi_htmx.py
Created June 11, 2025 08:18
HTMX + FastAPI
from functools import partial
from typing import Annotated
from fastapi import responses, FastAPI, Form
FormField = Annotated[str, Form()]
app = FastAPI()
get_html = partial(app.get, response_class=responses.HTMLResponse)
put_html = partial(app.put, response_class=responses.HTMLResponse)
@tiagocoutinho
tiagocoutinho / client.py
Last active September 25, 2024 08:06
read temp periodically
import time
import socket
def temp_stream():
with socket.create_connection(("localhost", 10_123)) as sock:
sockfd = sock.makefile("rwb", buffering=1)
while True:
sockfd.write(b"TEMP?\n")
payload = sockfd.readline()
yield float(payload)
@tiagocoutinho
tiagocoutinho / echo_server_single_client.py
Last active August 29, 2024 14:43
python single client echo server accept
import socket
import select
serv = socket.create_server(("", 10_010))
readers = {serv}
while True:
ready, _, _ = select.select(readers, (), ())
if serv in ready:
client, addr = serv.accept()
@tiagocoutinho
tiagocoutinho / random_name.py
Last active April 5, 2024 04:45
Generate random name in python
import random
import string
ascii_alphanumeric = string.ascii_letters + string.digits
def random_name(min_length=32, max_length=32):
if not (k := random.randint(min_length, max_length)):
return ""
first = random.choice(string.ascii_letters)
return first + "".join(random.choices(ascii_alphanumeric, k=k-1))
@tiagocoutinho
tiagocoutinho / test_fixture_run_once_xdist.py
Created May 18, 2023 12:59
Ensure a fixture runs once when using pytest-xdist and other workers wait for it to finish
"""
$ pip install posix_ipc pytest pytest-xdist
$ pytest -s -v -n 4 test_fixture_run_once_xdist.py
"""
import inspect
import logging
import time