Skip to content

Instantly share code, notes, and snippets.

class CaseMatched(Exception):
pass
class Switch:
def __init__(self, expression, *, no_match_raise=True):
self.expression = expression
self._no_match_raise = no_match_raise
self._any_case_called = False
import math
from noise import snoise4
from PIL import Image, ImageShow
import colorsys
def quint(x: float) -> float:
# https://easings.net/#easeInOutQuint
return 16 * x * x * x * x * x if x < 0.5 else 1 - pow(-2 * x + 2, 5) / 2
import math
from perlin_noise import PerlinNoise
class Easings:
# https://easings.net/
@staticmethod
def ease_in_cubic(x: float) -> float:
return x * x * x
@nitori
nitori / twitcher.py
Last active November 6, 2023 12:35
twitch chat lib?
from __future__ import annotations
import asyncio
import re
from typing import NamedTuple, AsyncGenerator, Callable, TypedDict
class Command(TypedDict):
name: str
aliases: list[str]
callback: Callable
@nitori
nitori / simple_evade_pygame.py
Created November 21, 2023 15:12
Very simple game to evade 25 enemies, with simple health bar
import random
from typing import NamedTuple
from pathlib import Path
import pygame
HERE = Path(__file__).absolute().parent
FONT = HERE / 'Quicksand/static/Quicksand-SemiBold.ttf'
@nitori
nitori / producer_consumer.py
Created January 5, 2024 20:42
Consumer producer example with queues and threads
import threading
import time
from queue import Queue
def producer(q: Queue):
number = 0
while True:
q.put(number)
number += 1
@nitori
nitori / parse_with_ast.py
Last active January 7, 2024 00:05
Replace names with constants
import ast
import json
class NameTransformer(ast.NodeTransformer):
def __init__(self, player):
self.player = player
def visit_Name(self, node: ast.Name):
condition = node.id.split('__')
import json
import ast
class ValidateExpression(ast.NodeVisitor):
allowed = {
ast.Expression,
ast.BoolOp, ast.UnaryOp,
ast.And, ast.Not, ast.Or,
ast.Call,
@nitori
nitori / truck_maybe.py
Last active January 7, 2024 20:48
truck maybe?
import math
import pygame
def bezier(points: list[pygame.Vector2], t: float):
"""calculate a point on a bezier curve with any number of points"""
if len(points) == 1:
return points[0]
else:
return (1 - t) * bezier(points[:-1], t) + t * bezier(points[1:], t)
@nitori
nitori / foo.py
Last active February 2, 2024 09:14
from collections import Counter
strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
d = {}
for s in strs:
c = frozenset(Counter(s).items())
d.setdefault(c, []).append(s)