Skip to content

Instantly share code, notes, and snippets.

@nitori
nitori / oauth2twitchexample.py
Last active February 5, 2023 12:37
Uses requests and flask to make a simple quick example for a webbased oauth2 user authentication. Note that this expects the webpart to actually be a proper server in any real scenario. For pure client-side applications currently only "Implicit grant flow" can be used (PKCE is currently not available afaik).
from multiprocessing import Process, Queue
from urllib.parse import urlparse, urlunparse, urlencode, parse_qs
import os
import random
import time
class TwitchAuthError(Exception):
pass
@nitori
nitori / linear_and_squared.py
Last active May 28, 2023 11:07
Typical example of finding pairs of ints adding to target with a slow and fast algorithm
from contextlib import contextmanager
import time
import random
def find_two_slow(lst, target):
result = set()
for a in lst:
for b in lst:
if a + b == target:
from __future__ import annotations
from PIL import Image
import numpy as np
def ease_in_out_quad(t):
return t * t * (3.0 - 2.0 * t)
def find_coeffs(pa, pb):
from __future__ import annotations
import asyncio
from textual.app import App, ComposeResult
from textual import events
from textual.widgets import Header, Footer, Input, TextLog
class Prompt(Input):
app: 'Terminal'
@nitori
nitori / returning_thread.py
Created June 15, 2023 11:43
Simple subclass of threading.Thread that returns the targets return value on join
from typing import Callable, Any
from threading import Thread
from concurrent.futures import Future
class ReturnThread(Thread):
_target: Callable
_args: tuple[Any, ...]
_kwargs: dict[str, Any]
_fut: Future
@nitori
nitori / cccc.py
Created July 18, 2023 18:36
convert image to provided palette
from itertools import chain
import sys
from PIL import Image
# Connler's Cool Content Canvas
COLORS = {
0: (0, 0, 0), # 000000
1: (34, 34, 34), # 222222
2: (85, 85, 85), # 555555
3: (136, 136, 136), # 888888
@nitori
nitori / fsm_simple_example.py
Last active August 14, 2023 11:57
Simple pygame example with a basic state management
from typing import TypeAlias, Type
import pygame
import sys
StateDict: TypeAlias = 'dict[str, StateBase | Type[StateBase]]'
class StateMachine:
"""Handles all the states and delegates to the correct one."""
screen: pygame.Surface
@nitori
nitori / oauth2discord.py
Last active September 2, 2023 14:03
Get access token from discord api via oauth2
from multiprocessing import Process, Queue
from urllib.parse import urlencode
import os
from dotenv import load_dotenv
load_dotenv()
def run_local_app(q: Queue, state: str):
from flask import Flask, request
@nitori
nitori / xplnedsf.py
Last active September 5, 2023 19:29
import struct
import os
import hashlib
def read_dsf(filename):
size = os.stat(filename).st_size
footer_start = size - 16 # 16 byte (128bit) for md5 hash
digest = hashlib.md5()
with open(filename, 'rb') as f:
@nitori
nitori / mathexpr.py
Created September 7, 2023 18:16
Evaluate math expression using ast
import ast
class NodeVisitor(ast.NodeVisitor):
allowed = {
ast.Add, ast.Sub,
ast.Mult, ast.Div,
ast.FloorDiv, ast.Mod,
ast.Pow, ast.BitXor,
ast.USub, ast.UAdd,