Skip to content

Instantly share code, notes, and snippets.

@nitori
nitori / if_then_else_to_if_expr.py
Last active November 12, 2022 02:48
Convert if_then_else function call to if expression.
import ast
class ConvertIfExpr(ast.NodeTransformer):
def visit_Call(self, node: ast.Call):
'''
Assumes the form if_then_else(cond)(then_case)(else_case)
All calls are expected to have exactly one argument.
'''
@nitori
nitori / anyclock.py
Last active February 4, 2023 21:43
Allows showing what the current time would be if we used different conversion from seconds to minutes to hours.
from __future__ import annotations
import pygame
import math
from datetime import datetime
HOURS_PER_DAY = 10
MINUTES_PER_HOUR = 100
SECONDS_PER_MINUTE = 100
@nitori
nitori / survive.py
Created December 16, 2022 19:23
small survival game
from __future__ import annotations
import pygame
import random
import pathlib
HERE = pathlib.Path(__file__).absolute().parent
class BaseSprite(pygame.sprite.Sprite):
_image_res: tuple[int]
import sys
def int_succeeds(c: str):
try:
int(c)
except ValueError:
return False
return True
chars = [chr(n) for n in range(sys.maxunicode + 1)]
from __future__ import annotations
from PIL import Image, ImageDraw
def neighbours(x, y):
return [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1), (x - 1, y - 1), (x + 1, y - 1), (x - 1, y + 1),
(x + 1, y + 1)]
def find_ocean_coords(filename, testsize=(128, 128)):
from __future__ import annotations
import math
import colorsys
from PIL import Image, ImageDraw, ImageFont
RowTuple = tuple[float, float] | None
def calc_lines(t_start: int, t_end: int) -> tuple[RowTuple, RowTuple, RowTuple]:
"""
import socket
import threading
import queue
import time
from dataclasses import dataclass
from typing import Any
@dataclass
class Client:
@nitori
nitori / make_fancy_code.py
Last active October 28, 2024 10:54
Convert python identifiers to some preselected Unicode characters
import ast
import os
import random
import string
import sys
import unicodedata as ud
class IdentifierTransformer(ast.NodeTransformer):
def __init__(self, char_sets: dict[str, dict[str, list]]):
from PIL import Image, ImageDraw
import math
def main():
im = Image.open('city.png').convert('RGB')
out = Image.new('RGB', im.size)
dotw = 16
doth = 16
from multiprocessing import Process, Queue
import os
import time
from urllib.parse import urlencode, urljoin
import requests
from dotenv import load_dotenv
load_dotenv()
STREAMLABS_API_BASE = 'https://streamlabs.com/api/v1.0/'