Skip to content

Instantly share code, notes, and snippets.

View thegamecracks's full-sized avatar

thegamecracks thegamecracks

View GitHub Profile
@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__(
@thegamecracks
thegamecracks / sqlite_authorizer.py
Last active February 26, 2024 22:05
A simple SQLite REPL with limited database control using SQLite's authorizer callbacks
"""
https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.set_authorizer
https://sqlite.org/c3ref/c_alter_table.html
"""
import contextlib
import re
import sqlite3
import textwrap
from typing import Iterator
@thegamecracks
thegamecracks / scrollable_frame.py
Last active March 25, 2024 15:37
Yet another scrollable frame in tkinter, but using Tk 8.5 themed widgets
from tkinter import Canvas, Event, Widget
from tkinter.ttk import Frame, Scrollbar, Style
from weakref import WeakSet
class ScrollableFrame(Frame):
__last_scrollregion: tuple[int, int, int, int] | None
def __init__(
self,
@thegamecracks
thegamecracks / main.py
Last active May 7, 2024 02:37
Pausing asyncio coroutines in a Tkinter application
# Requires Python>=3.10
import asyncio
import concurrent.futures
import itertools
import logging
import threading
from tkinter import Event, IntVar, Tk
from tkinter.ttk import Button, Frame, Label, Progressbar
from typing import Any, Awaitable, Callable, Coroutine, Generator, Generic, TypeVar
@thegamecracks
thegamecracks / 1-readme.md
Last active April 6, 2024 00:51
A short tutorial on persistent views in discord.py

Persistent Views in discord.py

In discord.py, persistent views allow a bot to handle interactions from message components (i.e. buttons and select menus) after the bot has restarted.

For a view to be persistent, all its components must have a custom ID and the view must have its timeout set to None. This can look something like:

@thegamecracks
thegamecracks / 1-main.py
Last active May 1, 2024 02:45
Side effects of improperly closing an asyncio event loop
import asyncio
USE_ASYNCIO_RUN = False
class ImaginaryResource:
def __enter__(self):
print("1. Opening resource")
return self
@thegamecracks
thegamecracks / 1-async_gen.py
Last active August 23, 2023 14:44
Manually handling awaits/yields from Python async generators
class Request:
def __init__(self, body):
self.body = body
def __await__(self):
response = yield self.body
return response
async def agen_func():