Skip to content

Instantly share code, notes, and snippets.

View Tobi-De's full-sized avatar
👁️‍🗨️
Trade-offs everywhere!

Tobi Tobi-De

👁️‍🗨️
Trade-offs everywhere!
View GitHub Profile
import http.server
import json
import os
import uuid
from datetime import datetime as dt
from pathlib import Path
from typing import Callable
import time
BUILD_DIR = Path("_build")
@Tobi-De
Tobi-De / install-hatch.sh
Last active August 2, 2024 08:25
Install hatch script by @cofin
#!/bin/sh
# Original https://github.com/litestar-org/pytest-databases/blob/main/scripts/install-hatch.sh
# --- Constants ---
BASE_URL="https://github.com/pypa/hatch/releases/latest/download"
EXTRACT_CMD="tar -xzf"
# --- Handle Optional Installation Directory ---
INSTALL_DIR="$1" # Default: current directory

Media Setup for CapRover NGINX and Django

Use NGINX to serve the media files on CapRover for a Django project.

To give a brief overview, everything in CapRover is in a container, including NGINX, so no container can access other container data. But if you want to serve your media files (this will work the same for static files), NGINX needs to be able to access these files. Fortunately, the NGINX CapRover container provides a folder on the host that it can read and can be shared with other containers. So, basically, what you need to do is tell your Django app to write the media files in this shared folder so that NGINX can read them.

# Django media settings
STORAGES = {
    "BACKEND": "django.core.files.storage.FileSystemStorage",
from __future__ import annotations
import argparse
import logging
import platform
import shutil
import stat
import subprocess
import sys
import tempfile
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "pytesseract",
# "pillow",
# "rich"
# ]
# ///
import pytesseract
@Tobi-De
Tobi-De / gh_mirrors_to_gitea.py
Last active September 18, 2024 07:56
backup github repos to gitea
# requirements: httpx, python-dotenv
import os
import httpx
from dotenv import load_dotenv
from datetime import datetime as dt
import pytz
from dateutil import parser
load_dotenv()
@Tobi-De
Tobi-De / bulk.py
Last active April 3, 2024 08:36
Account Update without explicit lock
from django.db.models import Case, Value, When, F, Q
def transfer(from_account: Account, to_account: Account, amount: int):
with transaction.atomic():
count = Account.objects.filter(
Q(pk=from_account.pk, balance__gte=amount) | Q(pk=to_account.pk)
).update(
balance=Case(
@Tobi-De
Tobi-De / sse_litestar.py
Created December 8, 2023 11:30
sse litestar
import time
from litestar import Litestar, get
from litestar.channels.backends.redis import RedisChannelsPubSubBackend
from litestar.channels.plugin import ChannelsPlugin
from litestar.response.sse import ServerSentEvent
import redis.asyncio as Redis
from typing import Generator, AsyncGenerator
import json
from contextlib import asynccontextmanager
sse-starlette
starlette
redis
psycopg[c]
from typing import TYPE_CHECKING
import structlog
from .config import get_redis_url, get_client_tracking_enabled
from functools import wraps
import threading
import redis
import asyncio
if TYPE_CHECKING:
from .brokers import Broker