Skip to content

Instantly share code, notes, and snippets.

View cb109's full-sized avatar
💭
🍴 🐘

Christoph Bülter cb109

💭
🍴 🐘
View GitHub Profile
@cb109
cb109 / django_admin_autocomplete_field_label_as_link.js
Last active January 6, 2023 13:33
Turn Django admin autocomplete_field labels into links to the chosen model instance
@cb109
cb109 / setup_graylog_server.md
Last active January 3, 2023 10:21
Setup a self-hosted Graylog server

Setup a self-hosted Graylog server (e.g. for Django applications)

So other machines can send their logs to the central server for inspection/debugging.

For ease of setup we are going to use the official docker-compose setup here. The example below will be using Debian 11.

  1. Get a VPS with at least 4GB of RAM (less may crash as opensearch demands some resources).
  2. ssh to that VPS.
  3. Install Docker:
@cb109
cb109 / size_of_databases.sql
Created October 10, 2022 10:52
MySQL Size of Databases and Tables
SELECT
table_schema AS "Database",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (GB)"
FROM
information_schema.TABLES
GROUP BY
table_schema;
@cb109
cb109 / track_data.py
Last active July 25, 2022 08:08 — forked from dcramer/track_data.py
Tracking changes on properties in Django
# Tested against Django 3.2.14.
from django.db.models.base import DEFERRED
def track_data(*fields):
"""Tracks property changes on a model instance.
Simplified version of David Cramer's gist:
https://gist.github.com/dcramer/730765
@cb109
cb109 / stop_recursion.py
Created July 19, 2022 09:16
Stop infinite recursion
import inspect
import traceback
from typing import Callable
def stop_recursion(function: Callable):
"""Decorator to stop recursion of given function early.
Avoids 'RecursionError: maximum recursion depth exceeded' if
preventing the cause of the recursion is not possible.
@cb109
cb109 / gunicorn.sh
Created June 15, 2022 08:50
Set gunicorn workers based on available cores
# https://docs.gunicorn.org/en/stable/design.html#how-many-workers
let num_workers=2*$(nproc --all)+1
gunicorn --workers $num_workers
@cb109
cb109 / on_change_rerun_relevant_tests.sh
Last active July 25, 2022 12:26
On file changed, re-run relevant pytests only
pip install pytest-watch pytest-testmon
# Build db on first run.
pytest --testmon
# Watch for changes, rerun tests.
ptw -- <pytest-options>
# Together: Watch for changes, rerun tests relevant with changed file(s) only!
ptw -- --testmon <pytest-options>
@cb109
cb109 / conftest.py
Last active February 4, 2022 13:07
Allow pytest -k to match against tests with @pytest.mark.extra_keywords("match these", "as", "well")
def pytest_itemcollected(item):
"""Implement custom marker: @pytest.mark.extra_keywords
Using this we can assign extra keywords to match via the -k CLI
option, so that the test module/class/functionname are not the only
things matched. This makes sense if e.g. certain features should be
matched by name without having to change the test name necessarily.
"""
if not item.own_markers:
@cb109
cb109 / obs_beep_when_replay_buffer_saved.lua
Last active January 23, 2022 10:12
Beep when OBS replay buffer has been saved
-- Beep when the OBS (https://obsproject.com) replay buffer has been saved.
--
-- Slightly edited version of:
-- https://gist.github.com/upgradeQ/b2412242d76790d7618d6b0996c4562f
-- as mentioned in:
-- https://obsproject.com/forum/threads/id-love-some-feedback-when-a-replay-is-saved.129807/
-- with a less annoying sound and a different event.
--
-- Add to OBS via: Tools > Scripts > + Button
--