Skip to content

Instantly share code, notes, and snippets.

View thegamecracks's full-sized avatar

thegamecracks thegamecracks

View GitHub Profile
@thegamecracks
thegamecracks / map_future.py
Last active May 17, 2024 13:23
Wrapping concurrent.futures.Future to support .map() method chaining
from __future__ import annotations
from concurrent.futures import Future, InvalidStateError, ThreadPoolExecutor
from contextlib import suppress
from typing import Any, Callable, TypeVar
T = TypeVar("T")
R_co = TypeVar("R_co", covariant=True)
@thegamecracks
thegamecracks / wrap_label.py
Last active May 12, 2024 12:35
A tkinter label with automatic text wrapping
import sys
from tkinter import Event, Tk
from tkinter.ttk import Frame, Label
if sys.platform == "win32":
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(2)
LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce malesuada ipsum enim, feugiat venenatis sapien tempus et. Morbi auctor egestas metus vel faucibus. Nullam in nunc nisi. Maecenas eget sagittis ante. Sed ut turpis turpis. Morbi suscipit massa ac efficitur iaculis. Suspendisse risus nisi, tempor non pulvinar sed, vehicula a sapien. Pellentesque iaculis ligula sed sapien faucibus, eget mollis magna volutpat. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi porttitor luctus tellus, nec tincidunt lacus. Nunc dui eros, laoreet sed pretium id, porttitor eget nunc."
@thegamecracks
thegamecracks / 1-readme.md
Last active May 1, 2024 20:34
A persistent job scheduler built on SQLite
@thegamecracks
thegamecracks / event.py
Last active April 19, 2024 04:48
A thread-safe event compatible with asyncio
# Requires Python>=3.11
import asyncio
import contextlib
import threading
import unittest
class Event(threading.Event): # TODO: any better name?
"""A thread-safe event compatible with asyncio."""
@thegamecracks
thegamecracks / composite.py
Last active April 17, 2024 16:40
A basic command-line program for compositing two images using Pillow
"""
Dependencies:
Pillow>=10.3.0
usage: composite.py [-h] [-s SCALE] [-o OFFSET] image
Create a composite of the given image and display it.
The window can be clicked to toggle it as an overlay.
You can also right click it to save the image.
@thegamecracks
thegamecracks / bands.py
Created April 11, 2024 23:34
A collection of some random scripts I wrote on Termux
#!/usr/bin/env python3
"""Converts resistor parameters into their corrresponding color bands."""
import argparse
from decimal import Decimal
# list of colors ordered by their corresponding digit (0-9)
DIGIT_BANDS = [
"black",
"brown",
"red",
@thegamecracks
thegamecracks / 1-readme.md
Last active April 15, 2024 00:32
A simple image batch resizer using Pillow
@thegamecracks
thegamecracks / context.py
Created April 2, 2024 15:40
Contextvar hijinks with CPython's asyncio internals
# Requires Python 3.11-3.12
import asyncio, contextlib, contextvars
from asyncio.tasks import _PyTask
x = contextvars.ContextVar("x", default=1)
def create_pure_python_task(loop, coro, context=None):
return _PyTask(coro, loop=loop, context=context)
@thegamecracks
thegamecracks / event_thread.py
Last active May 15, 2024 14:55
Running tkinter and asyncio event loops in separate threads
import asyncio
import concurrent.futures
import threading
from tkinter import Event, Tk
from tkinter.ttk import Button
from typing import Self
class EventThread(threading.Thread):
"""Runs an asyncio event loop in a separate thread.
@thegamecracks
thegamecracks / flexbox.py
Created March 24, 2024 16:12
An attempt to make a flexbox-like manager in Tkinter
import sys
from tkinter import Event, Tk
from tkinter.ttk import Button, Frame, Widget
from typing import Literal
FlexboxMode = Literal["horizontal", "vertical"]
class Flexbox(Frame):
def __init__(