Skip to content

Instantly share code, notes, and snippets.

View cb109's full-sized avatar
💭
🍴 🐘

Christoph Bülter cb109

💭
🍴 🐘
View GitHub Profile
@cb109
cb109 / ffmpeg_overlay_progressbar.py
Created April 23, 2024 07:48
Overlay a simple growing/moving progressbar on the bottom of a video using ffmpeg
import subprocess
def add_progressbar_on_top_of_video(
input_video_filepath: str,
output_video_filepath: str,
width: int,
height: int,
duration: float,
fps: float = 25.0,
progressbar_height: int = 8,
@cb109
cb109 / class_attribute_knows_its_name_on_parent_class.py
Created April 5, 2024 08:41
Give class attributes knowledge about how they are used on their parent class
class Bar:
def __init__(self):
self.parent_class_attr_name = None
class LetClassAttributesKnowTheirName(type):
def __new__(mcs, name, bases, attributes):
for attr_name, attr_value in attributes.items():
if isinstance(attr_value, Bar):
attr_value.parent_class_attr_name = attr_name
@cb109
cb109 / libraries.md
Last active March 1, 2024 12:04
Debugging Django SQL Queries

Debugging Django SQL Queries

There are all kinds of libraries to gain insight on queries (which, how many, how performant) inside your Django application, the most popular being django-silk and the django-debug-toolbar.

Alternatives

Those tools above however may be a bit overkill for some use cases, which is why I would like to mention some slim alternatives that produce console output only:

  • shell_plus –print-sql: To simply print all SQL queries as they are executed
  • django-debug-query: This will print out formatted and colored SQL statements into the console. Good to get a detailled look into the actual queries being done. Not so good for views that produce lots of queries, as it prints all of them. Great for playing around with the ORM in a shell and see what the query looks like.
@cb109
cb109 / symbolic_expr.py
Last active February 26, 2024 13:42
Use booleano to evaluate simple symbolic expressions.
"""Use booleano to evaluate simple symbolic expressions.
This is useful to provide a mini DSL to define conditions in
application logic for script-savvy users or as an intermediate
for actual UI elements. The main benefit is that it does not
involve the use of python's eval() and therefore no security
concerns.
booleano supports a number of useful operators out of the box,
like equality, belongs-to, greater-than etc. which frees us
@cb109
cb109 / base_site.html
Last active February 22, 2024 12:51
Django Admin custom clientside Search for Index (tested with Django 3.2)
{% extends "admin/base_site.html" %}
{% block usertools %}
{% url 'admin:index' as admin_index_url %}
{% if request.get_full_path == admin_index_url %}
<input
type="text"
id="admin_base_search"
placeholder="Search..."
autofocus
@cb109
cb109 / remove_custom_fk_uniqueconstraint_django_mysql.md
Last active February 9, 2024 18:42
Fix: Cannot remove UniqueConstraint on ForeignKey with Django/MySQL

Django allows you to define custom UniqueConstraints to specify which combinations of values are allowed in a row, but removing these later can be problematic when some ForeignKey is involved, at least with MySQL it may throw a Cannot drop index '...': needed in a foreign key constraint at you.

The example below shows you how to resolve such a situation in 3 small individual migrations:

  • We start with an existing UniqueConstraint.
class MyModel(models.Model):
    other_model = models.ForeignKey("OtherModel", on_delete=models.CASCADE)
    name = models.CharField(max_length=128)
@cb109
cb109 / google_chrome_ignores_disable_web_security_flag.md
Created February 6, 2024 13:54
Google Chrome Launch Argument Order may matter

Google Chrome Launch Argument Order may matter

--disable-web-security VS --user-data-dir

Also see: https://peter.sh/experiments/chromium-command-line-switches/#disable-web-security

I came across an issue with running google-chrome to render some HTML file, where we are loading fonts from an external URL and to avoid CORS screaming at us we configured it like:

google-chrome --user-data-dir=/tmp/ --disable-web-security

@cb109
cb109 / django_migrate_m2m_add_through_model.py
Last active January 15, 2024 11:38
Fix: ValueError: Cannot alter field <model>.<field> into <model>.<field> - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)
# Changing the through= part of a models.ManyToManyField() fails to migrate,
# as Django won't let us do this in a single step. We can manually workaround
# with several migration steps though, as shown below.
#
# Based on Django 3.2
# 1) Initial state of models.py
from django.db import models
@cb109
cb109 / wow_chroma.py
Created January 21, 2018 15:18
Razer Chroma WoW Health and Mana
"""
Displays WoW health and mana status on a Razer Chroma keyboard.
Installation (Windows):
$ virtualenv venv
$ pip install requests
$ easy_install pillow
Usage:
- Run WoW client fullscreen on main monitor
@cb109
cb109 / gist:4039770a33ead38fb77789c107dd080f
Created September 1, 2023 10:53
Django FileField / File .path VS .name
# Django's models.FileField stores FieldFile objects. These have both .name
# and .path attributes, which however are not exactly intuitive:
my_instance.my_filefield.name
# E.g. whatever/123/foo.png
# Returns the path relative to MEDIA_ROOT, not the filename!
# For the filename do e.g. os.path.basename(my_instance.my_filefield.name)
my_instance.my_filefield.path
# E.g. /var/www/media/whatever/123/foo.png