Skip to content

Instantly share code, notes, and snippets.

@jul
jul / get_magic.py
Created May 1, 2022
minimalist magic detection for fun
View get_magic.py
#!/usr/bin/env python3
from requests import get
import re
'''
sometimes, you want a minimalistic magic number detection for simple file types.
Like for instance, you scrap the web and you don't trust the web server when returning the mime type
(actually happened to me on bilbliothèque nationale de france more than once)
So, yo want to make sure that for instance, the returned content is atcually a jpeg, and you may not be
on a unix OS with libmagic and you may not want to add dependencies and ...
just comparing 3 bytes in a row in a raw byte stream is not THAT hard
@jul
jul / oneline.sh
Last active Jan 24, 2022
timing python oneliner by size of line
View oneline.sh
#!/usr/bin/env bash
export LANG=C
redadd() { python3 -c '(s,d,r)=(__import__("sys"),__import__("archery").mdict,__import__("functools").reduce);print(sorted(r(d.__add__,(d({l[:-1]:1}) for l in s.stdin.readlines())).items(),key=lambda k:k[1],reverse=True)[:10])' ; }
red() { python3 -c '(s,d,r)=(__import__("sys"),__import__("archery").mdict,__import__("functools").reduce);print(sorted(r(d.__radd__,(d({l[:-1]:1}) for l in s.stdin.readlines())).items(),key=lambda k:k[1],reverse=True)[:10])' ; }
function count() { python3 -c'(s,d)=(__import__("sys"),__import__("archery").mdict);print(sorted(sum((d({l[:-1]:1}) for l in s.stdin.readlines()),{}).items(),key=lambda k:k[1],reverse=True)[:10])'; }
cfl() { python3 -c'(s,t,d,f)=(__import__("sys"),__import__("tld"),__import__("archery").mdict,lambda l:t.get_fld(l[:-1],fix_protocol=True,fail_silently=True));print(sorted(sum((d({f(l):1}) for l in s.stdin.readlines()),{}).items(),key=lambda k:k[1],reverse=True)[:10])'; }
test_time() {
func="$1"
TIMEFORMAT="%U"
@jul
jul / freebsd_inst.sh
Last active Mar 22, 2022
qemu freeBSD on linux
View freebsd_inst.sh
#!/bin/bash
set -e
LOCATION=$(pwd)
ARCH=amd64
VERSION=13.1
FLAVOUR=BETA2
FROM_VM=https://download.freebsd.org/ftp/releases/VM-IMAGES/${VERSION}-${FLAVOUR}/${ARCH}/Latest/
FROM_CDROM=https://download.freebsd.org/ftp/releases/ISO-IMAGES/${VERSION}/
FILENAME=FreeBSD-${VERSION}-${FLAVOUR}-${ARCH}.qcow2
CDROM="$( basename $FILENAME .qcow2 )-bootonly.iso"
@jul
jul / todel.py
Last active Dec 13, 2021
jeu de la vie (à réécrire (kleenex)) en pavage triangulaire
View todel.py
import PySimpleGUI as sg
from operator import itemgetter
### uitliser ipython -i argv[0] pour être en interactif et faire joujou
can = sg.Canvas(size=(1412,998))
status=sg.Text('status')
win = sg.Window("titre",[[can,],[status,],], finalize=True)
c = can.TKCanvas
from time import sleep, time
from random import randint
from os import system
@jul
jul / goft.py
Created Dec 12, 2021
gof triangulaire on the way
View goft.py
import PySimpleGUI as sg
from operator import itemgetter
### uitliser ipython -i argv[0] pour être en interactif et faire joujou
can = sg.Canvas(size=(400,600))
status=sg.Text('status')
win = sg.Window("titre",[[can,],[status,],], finalize=True)
c = can.TKCanvas
from time import sleep
SZ=50
@jul
jul / gof_base.py
Created Dec 11, 2021
préparer un jeu de la vie en base triangulaire (les carrés sucent)
View gof_base.py
import PySimpleGUI as sg
### uitliser ipython -i argv[0] pour être en interactif et faire joujou
can = sg.Canvas(size=(400,600))
win = sg.Window("titre",[[can,],],finalize=True)
c = can.TKCanvas
from time import sleep
SZ=50
ORD=1
def save(canvas, name="save"):
@jul
jul / tr.py
Created Dec 11, 2021
dessiner un pavage triangulaire en python en moins de 100 sloc
View tr.py
import PySimpleGUI as sg
### uitliser ipython -i argv[0] pour être en interactif et faire joujou
can = sg.Canvas(size=(400,600))
win = sg.Window("titre",[[can,],],finalize=True)
c = can.TKCanvas
SZ=50
def tr(x,y, up=1,**kw):
""" créer un triangle équilatéral centré en x,y avec une homothétie /
symmétrie sur l'axe des y (up / down) exprimée en coordonnées
@jul
jul / test_fdj.py
Last active Jan 12, 2021
simulation de 100 tirages au hasard et mesure des fréquences des gains a Végas
View test_fdj.py
#!/usr/bin/env python3
from archery import mdict
from functools import reduce
from random import randint, seed
from json import dumps
from sys import argv
pp = lambda d:print(dumps(d, indent=4))
_seed = argv[1]
print(_seed)
seed(int(_seed))
@jul
jul / test.sh
Last active Nov 3, 2020
checking font substitution on firefox
View test.sh
#!/usr/bin/bash
MIN_FF_OPT=" --headless "
FF="firefox $MIN_FF_OPT"
die() { echo "$@"; exit 0;}
fc-match Andika || die "le test va pas marcher"
echo "basically if I could create profile from scratch I would put only this"
cat <<EOF
user_pref("font.default.x-western", "Andika");
@jul
jul / gnuplot_plot
Last active Nov 2, 2020
moving median
View gnuplot_plot
#!/usr/bin/env gnuplot
set timefmt '%Y-%m-%d'
set xdata time
set yrange [1<*:]
set format x '%Y-%m-%d'
set xrange ["2020-03-01"<*:]
set datafile sep ','