Skip to content

Instantly share code, notes, and snippets.

View thegamecracks's full-sized avatar

thegamecracks thegamecracks

View GitHub Profile
@thegamecracks
thegamecracks / find_and_replace.py
Last active October 14, 2020 03:02
A python script for finding text across several files and optionally replacing that text
"""Find and (optionally) replace over multiple files using a glob pattern.
Give a glob pattern to specify which files to iterate over.
Example glob patterns:
*.txt - Iterate over all .txt files just in the directory
**/*.txt - Iterate over all text files in the directory
and subdirectories.
project_* - Iterate over all files just in the directory
starting with project_.
@thegamecracks
thegamecracks / building_sqlcipher.md
Last active January 14, 2024 10:43
Personal notes on building SQLCipher

Building SQLCipher

This guide was written for personal documentation and does not comprehensively cover the process in detail, but for someone who needs help, this will hopefully have enough detail to build SQLCipher without excessive googling.

Operating System:

@thegamecracks
thegamecracks / 1-README.md
Last active August 14, 2023 03:16
Python recipes for custom unsigned bigint types in sqlite3

https://sqlite.org/datatype3.html#storage_classes_and_datatypes

As SQLite does not natively support storing unsigned 64-bit integers, there are different workarounds to choose from, such as:

  1. Storing unsigned integers in a TEXT column

    This is the easiest choice as it can easily be written by hand, printed, and through SQLite's dynamic typing, supports arithmetic within queries (for example, bit shifting).

@thegamecracks
thegamecracks / README.md
Last active May 8, 2024 01:20
Using python -m to invoke modules as scripts

Using python -m to invoke modules as scripts

Note

Hey there! This gist has been rewritten in my GitHub blog. You should read that instead!

Prologue

You might have seen the -m flag used in various python commands online,

@thegamecracks
thegamecracks / tree.py
Created August 4, 2023 01:10
discord.py CommandTree cache based on Soheab's gist
"""
A command tree that maintains a cache of AppCommand objects,
helpful for mentioning slash commands in messages.
Reference: https://gist.github.com/Soheab/fed903c25b1aae1f11a8ca8c33243131
"""
from discord import AppCommandType
from discord.abc import Snowflake
from discord.app_commands import (
AppCommand,
@thegamecracks
thegamecracks / async_to_sync.py
Last active August 12, 2023 17:21
Decorators for making asynchronous functions callable in a synchronous context
import asyncio
import concurrent.futures
import contextlib
import functools
import threading
from contextvars import ContextVar
from typing import Callable, Coroutine, ParamSpec, TypeVar
P = ParamSpec("P")
Y = TypeVar("Y")
@thegamecracks
thegamecracks / sqlite_transactions.py
Created August 18, 2023 14:25
Experiments with SQLite transaction handling from Python
"""
Demonstration 1
===============
This shows a write transaction temporarily blocking another
write transaction while read transactions are being performed
concurrently. This is the typical behaviour expected from SQLite
using the default configuration.
DATABASE_PATH = 'main.db'
N_READERS = 3
@thegamecracks
thegamecracks / 1-coroutine_utopia.py
Last active August 23, 2023 21:14
Directly consuming Python generators, coroutines, and async generators
from typing import Any, AsyncGenerator, Coroutine, Generator, Generic, TypeVar
T = TypeVar("T")
U = TypeVar("U")
class FutureLike(Generic[T, U]):
def __init__(self, yield_value: T, return_value: U):
self.yield_value = yield_value
self.return_value = return_value
@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():
@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