Skip to content

Instantly share code, notes, and snippets.

@zorky
zorky / fieldoperator_filter.py
Last active January 4, 2019 10:20
Generic filter with operators (gt, lt, eq, ne) on model's field
import django_filters
import datetime
from django.db.models import Q
class FilterOperatorBase(django_filters.FilterSet):
"""
base filter for field_operator
field_operator is formatted as : field||type||operator||value
@zorky
zorky / reduce.ts
Last active August 24, 2019 10:48
Reduce d'une liste de dates, renvoie une chaîne de concaténation des dates: date1, date2, ..., dateN
/**
Exemple de reduce d'une liste de dates (dates_obj : [{date: 'date'}, ...]
*/
reduce(row): string {
dates = row.dates_obj.reduce<string>((previousValue, currentValue, currentIndex) => {
let currentDate = `${dp.transform(currentValue.date, 'dd/MM/yyyy')}`;
const { hh, mm } = { hh: dp.transform(currentValue.date, 'HH'), mm : dp.transform(currentValue.date, 'mm')};
if (hh !== '00') {
currentDate = `${currentDate} à ${hh}h${mm}`;
}
"""
Django ORM Optimization Tips
Caveats:
* Only use optimizations that obfuscate the code if you need to.
* Not all of these tips are hard and fast rules.
* Use your judgement to determine what improvements are appropriate for your code.
"""
# ---------------------------------------------------------------------------
@zorky
zorky / psql_useful_stat_queries.sql
Created March 19, 2021 08:50 — forked from anvk/psql_useful_stat_queries.sql
List of some useful Stat Queries for PSQL
--- PSQL queries which also duplicated from https://github.com/anvk/AwesomePSQLList/blob/master/README.md
--- some of them taken from https://www.slideshare.net/alexeylesovsky/deep-dive-into-postgresql-statistics-54594192
-- I'm not an expert in PSQL. Just a developer who is trying to accumulate useful stat queries which could potentially explain problems in your Postgres DB.
------------
-- Basics --
------------
-- Get indexes of tables
@zorky
zorky / _docker-migrate-aufs.md
Created March 3, 2023 14:57 — forked from marcelrv/_docker-migrate-aufs.md
Docker migrate to overlay2 from aufs script

Docker migrate to overlay2 from aufs script

Crazy that this is pretty much forced change without proper transition script

note. Based on https://www.sylvaincoudeville.fr/2019/12/docker-migrer-le-stockage-aufs-vers-overlay2/ NOT WORKING!!!! IF FOLLOWING THE ABOVE.. SOMEHOW THE CONTAINERS DO NOT RE-APPEAR!

The only way I found that is somewhat automated is the the docker-compose way..

Which is still not 100% and require manual fixing of stupid errors of the docker-compose tool (mainly things that ere not interpreted right, like dates & userIds that need to manually surrounded by quotes etc)

@zorky
zorky / debounce-dictinct-filter.ts
Last active March 8, 2024 13:11
search input TS Rxjs Angular : debounceTime distinctUntilChanged
fromEvent(this.filter.nativeElement, 'keyup').pipe(
debounceTime(400),
distinctUntilChanged(),
filter(() => this.filter.nativeElement.value.length >= this.minFilter || this.filter.nativeElement.value.length === 0))
@zorky
zorky / mutable-immutable.py
Created December 17, 2024 17:08
list / tuple / namedtuple : mutable immutable
from collections import namedtuple
BOLD = "\033[1m"
RESET = "\033[0m"
# Liste : mutable
print(f"\n{BOLD}1-{RESET} changement valeur élément d'une {BOLD}liste{RESET} possible : {BOLD}mutable{RESET}")
fruits = ["pomme", "banane", "cerise", "orange"]
fruits.append("orange") # Liste modifiable
print(f"liste : {fruits} - set(liste) : {set(fruits)}\n")
@zorky
zorky / clean-git.sh
Created December 21, 2024 10:39
clean-git.sh
#!/bin/bash
PWD=`pwd`
TRUNK="master"
mode=""
if [[ $# -eq 2 ]]; then # -D : tentera une suppression soft (-d) puis hard (-D)
mode="$2"
fi
echo -e "$PWD/$1 \r\n"
cd "$1"
@zorky
zorky / cache-django.py
Last active December 23, 2024 12:18
Redis cache avec Django
class CacheHelper:
"""
Cache de données
low-level cache api https://docs.djangoproject.com/fr/4.2/topics/cache/#the-low-level-cache-api
"""
def get_queryset_cache(self, key, queryset) -> QuerySet:
CACHE_TTL = 60 * 60 * 24 * 7
objects = cache.get(key)
if objects is None:
logger.debug(f"init cache {key}")
@zorky
zorky / index.html
Created February 15, 2025 14:57
Simple Container Queries
<div class="@container grid place-items-center w-screen h-screen">
<button class="w-max flex items-center gap-2 p-4 @[9em]:px-6 rounded-lg text-sm bg-emerald-200 text-emerald-950">
<svg viewBox="0 0 24 24" fill="none" stroke-width="1.5" stroke="currentColor" class="size-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5M16.5 12 12 16.5m0 0L7.5 12m4.5 4.5V3" />
</svg>
<span class="sr-only @[9em]:not-sr-only">
Download <span class="sr-only @[13em]:not-sr-only">PDF file</span>
</span>
</button>
</div>