Skip to content

Instantly share code, notes, and snippets.

@Kmaschta
Kmaschta / pre-commit
Last active November 16, 2022 19:35
Pre-commit hook (warn if you have it.only, describe.only, fit or fdescribe)
#!/bin/bash
SRC=$(git rev-parse --show-toplevel)
EXCLUDE="--exclude-dir 'node_modules' --exclude-dir '.git'"
#==========================================================
# Check if I forgot to remove 'only' keyword from tests.
# To make sure that before commit run all tests
only_command="grep -c -h -r $EXCLUDE -E \"(describe|it)\.only\" $SRC | awk -F ':' '{x +=\$0}; END {print x}'"
fonly_command="grep -c -h -r $EXCLUDE -E \"f(it|describe)\(\" $SRC | awk -F ':' '{x +=\$0}; END {print x}'"
only=`eval $only_command`
@Kmaschta
Kmaschta / django_active_view_tag.py
Last active February 20, 2016 17:23
Django active view tag
from django import template
register = template.Library()
# custom tag "active_view" to return 'active' in the template when the view is active
# usage : {% active_view request name='view-name' %}
# usage : {% active_view request url='/view/abs/url' %}
# usage : {% active_view request re='^/view/ur.*' %}
@register.simple_tag(takes_context=True)
def active_view(context, name=None, url=None, re=None, cond=True):
@Kmaschta
Kmaschta / algo-dobble.py
Last active April 12, 2019 19:26
Algorithme Dobble
#!/usr/bin/python
# coding: utf-8
import sys
def plan_projectif(modulo):
"""Génération d'un plan projectif réel
Il est composé d'un plan affine (une matrice) et
des points à l'infini du plan réel.
@Kmaschta
Kmaschta / DRF-dynamic-fields.py
Last active September 5, 2020 10:31
Django Rest Framework - Dynamic Fields
# How to display only interesting fields for a Django Rest Framework API
# /?fields=field1,field2,field3
from rest_framework import serializers, pagination
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed.