Skip to content

Instantly share code, notes, and snippets.

@nitori
nitori / rollback_with_exitstack.py
Last active September 3, 2024 22:12
Simple rollback on error with contextlib.ExitStack
import os
import shutil
from contextlib import ExitStack
def create_folder(stack: ExitStack, path):
try:
os.mkdir(path)
except FileExistsError:
pass
@nitori
nitori / wfc.py
Last active August 17, 2024 23:59
very simple wave function collapse thingy
from collections.abc import Iterable
import math
import heapq
import random
from collections import deque
import time
type WeightedOptions = list[tuple[int, int]]
UP = 0b1000
@nitori
nitori / sample_app.py
Last active July 10, 2024 17:35
Sample App with routes and route parameters
import re
def extract_variables(endpoint: str):
"""
Very simple route "parsing" using regex:
The route:
"/item/{item_id}"
<?php
namespace Vendor\MyExt\Xclass;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\Routing\SiteMatcher;
use TYPO3\CMS\Core\Site\Entity\NullSite;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class Colors:
COLOR_NAMES = [
'black',
'red',
'green',
'yellow',
'blue',
'purple',
'cyan',
'white',
import math
import random
from typing import NamedTuple, Literal, Callable
import pygame
from PIL import Image
import numpy as np
type Pos = tuple[int, int]
type Color = tuple[int, int, int] | str
@nitori
nitori / pygame_plus_async.py
Last active May 24, 2024 16:42
Possible way to combine pygame and asyncio
import asyncio
import threading
import pygame
USER_EV1 = pygame.event.custom_type()
def pygame_loop(async_stopper):
pygame.init()
@nitori
nitori / gfxc.py
Last active April 21, 2024 11:16
from dataclasses import dataclass, field
import struct
from typing import BinaryIO
import zlib
import typing
"""
TFG1 Data files desired to be extracted (data directory)
https://gitea.xanax.lol/root/tfg-client
@nitori
nitori / test.py
Created April 6, 2024 15:27
sample of how to quantize an image with a predefined palette of colors
import random
from PIL import Image, ImageDraw
COLORS = [
(0x5e, 0x1f, 0x1d), (0xaa, 0x38, 0x37), (0x41, 0x2c, 0x33), (0xb3, 0x5a, 0x48), (0xfd, 0x63, 0x65),
(0xa0, 0xa5, 0xa9), (0x6c, 0x6e, 0x68), (0xb2, 0x00, 0x2a), (0xc0, 0x12, 0x26), (0xe1, 0x53, 0x85),
(0x93, 0x37, 0x4e), (0xe6, 0xa0, 0xc5), (0xe3, 0xb4, 0xa4), (0x00, 0x00, 0x00), (0x24, 0x27, 0x28),
(0x3f, 0x41, 0x41), (0x67, 0x1b, 0x56), (0x90, 0x00, 0x45), (0x34, 0x21, 0x50), (0x83, 0x58, 0x8b),
(0xa1, 0x89, 0xae), (0xd1, 0xb4, 0xc8), (0x87, 0x8e, 0x90), (0xa2, 0xa8, 0xab), (0xff, 0xff, 0xff),
(0x0a, 0x1a, 0x19), (0x23, 0x28, 0x1c), (0x34, 0x3b, 0x22), (0x61, 0x5c, 0x3d), (0x4f, 0x73, 0x6e),
@nitori
nitori / choose.py
Created March 29, 2024 08:52
Simple linux cli style user choice function
def choose(text, options="Yn"):
"""
text: prompt to the user.
options: set of characters. one upper case may be
used to indicate a default value.
Examples:
answer = choose("Continue?")
# outputs: "Continue? (Y/n): "