Skip to content

Instantly share code, notes, and snippets.

View twidi's full-sized avatar
🕶️

Stéphane "Twidi" Angel twidi

🕶️
View GitHub Profile
@twidi
twidi / pyproject.toml
Created November 10, 2023 11:49
config mypy pour wisemap
# ...
[tool.mypy]
plugins = [
"mypy_django_plugin.main",
]
python_version = "3.10"
check_untyped_defs = true
no_implicit_optional = true
warn_redundant_casts = true
@twidi
twidi / score.sql
Last active September 16, 2022 23:07
Swoosh? Boink! (scoring)
CREATE OR REPLACE FUNCTION pg_temp.get_scores(skip_days int) RETURNS TABLE (
"user" varchar(60),
first_game_id int,
nb_games int,
first_game_day date,
nb_played_days int,
nb_expected_days int,
nb_missing_days int,
final_score float,
regularity_factor float,
@twidi
twidi / drf_utils.py
Created December 21, 2016 14:34
Make Django Rest Framework correctly handle Django ValidationError raised in the save method of a model
"""
Sometimes in your Django model you want to raise a ``ValidationError`` in the ``save`` method, for
some reason.
This exception is not managed by Django Rest Framework because it occurs after its validation
process. So at the end, you'll have a 500.
Correcting this is as simple as overriding the exception handler, by converting the Django
``ValidationError`` to a DRF one.
"""
from django.core.exceptions import ValidationError as DjangoValidationError
@twidi
twidi / timelapse-plant-script.sh
Created September 16, 2016 10:02
Script used to update each image for my plant timelapse https://www.youtube.com/watch?v=_WLxC3imytk
#! /bin/bash
# This script expect a image file as first argument, and a destination directory as second.
# It will:
# - rotate the image 0.4deg
# - crop to a ratio for HD (1920*1080)
# - add the timestamp in the top right corner
# This timestamp is in the format "xXh yYm", and computed from a time of the day extracted from the first image
# of the directory. All images are the same day, so it's easier.
# It's printed in white with a soft black shadow.
@twidi
twidi / clean_sky.py
Created September 14, 2016 21:44
Timelapse preparation: remove the birds and planes from the sky
# Clean the sky: remove birds, planes... only work if each images are "close", and there is no cloud moving fast
# by @twidi
import glob, os, shutil, sys
from gimpfu import *
def run(source_dir='.', extension='JPG', threshold=30, mask=None, dest_dir=None):
"""
@twidi
twidi / README
Last active June 13, 2018 12:50
Run tests for code example in README.rst (for django)
# Real name of this file is README.rst but I don't want github to render the rst ;)
Blah blah blah
.. testsetup::
# This prepare the django environment to run all the "testcode" in this file
# This is not visible in the rendered README
import os
@twidi
twidi / series.md
Last active August 15, 2016 14:46
series a voir
  • 24
  • Battlestar Galactica
  • Black Sails
  • Breaking Bad
  • Broadchurch
  • Bron/Broen
  • Community
  • Daredevil
  • Dead Like Me
  • Death Note
@twidi
twidi / true_percent.py
Created October 20, 2015 13:50
percentage of true values
In [1]: from itertools import chain
In [2]: from collections import Counter
In [3]: x = [[True, False, True], [True]]
In [4]: y = list(chain.from_iterable(x))
In [5]: c = Counter(y)
@twidi
twidi / __init__.py
Created September 17, 2015 15:54
Deferable unique constraints in django (for postgresql) by creating a specific django db backend
"""A DB backend, based on ``django.db.backends.postgresql_psycopg2`` to cover a specific needs.
The need is to add the "DEFERRABLE INITIALLY DEFERRED" when defining a unique constraint
on `sort_order` fields, to make the ``Orderable`` base model works correctly.
"""
class Hashable(models.Model):
hash_method_prefix = 'compute_hash_'
default_hash_method = 'compute_hash_default'
hashable_fields = []
class Meta:
abstract = True
def compute_hash_default(self, field_name, hash_function):