Skip to content

Instantly share code, notes, and snippets.

@svenk
svenk / Gendernsternchen und Markdown in Python3
Last active November 12, 2019 10:24
Gendernsternchen und Markdown
Das Gendersternchen funktioniert nicht gut zusammen mit Markdown. Folgender
Regexp liefert ein Preprocessing, was es ermöglicht, ohne einen anderen
Markdown-Dialekt zu wählen (Regulärer Ausdruck in Python):
> for x in ["Gender*nnen","foo *bar* baz", "foo *bar*baz", "Und *das* ist wirklich Mensch*nnen *und* toll."]:
> print(re.sub(r"(^|\s)(\w+)\*([^.\s]*\w+)($|\s)", r"\1\2\*\3\4", x))
Gender\*nnen
foo *bar* baz
foo *bar*baz
@svenk
svenk / vprintf.cpp
Created July 3, 2019 08:19
Convenient C's printf in C++
// this is a buffer-overflow-safe version of sprintf
// source: http://stackoverflow.com/a/69911
// if you need something more mature, consider https://github.com/fmtlib/fmt
// the advantage of this snippet is that it can be really easily added to smallish
// codes/libraries. Usage is like C's vprintf et al. but with a C++ std::string as a result.
std::string vformat (const char *fmt, va_list ap) {
// Allocate a buffer on the stack that's big enough for us almost
// all the time. Be prepared to allocate dynamically if it doesn't fit.
size_t size = 1024;
char stackbuf[1024];
@svenk
svenk / mbox-to-csv.py
Last active September 2, 2021 04:08
Email inboxes (mbox) to CSV
#!/usr/bin/env python3
# A python3 script to read an mbox file (extension to Maildir is trivial thanks to
# mailbox module) and writes out certain headers as CSV file.
# On large mbox files (say 5GB) it is slow because... it's Python.
# The script deals with some cornercases (encoding, newlines, ...) which I found in
# 100K emails. Otherwise, the builtin python mail libraries provide robustness.
#
# It processes roughly 110K (5GB mbox) mails in 2:35mins on my laptop and produces
# a 19MB CSV file.
@svenk
svenk / snippet.py
Created September 10, 2018 21:37
Python: Display scientific number with 10 power something (suitable for LaTeX) instead of the "e" suffix
import re
sci = lambda d: re.sub(r'e\+?(-?)?0?(\d+)', r" x 10^{\1\2}" ,"%.2e"%d)
"""
### Examples:
In [34]: sci(1)
Out[34]: '1.00 x 10^{0}'
@svenk
svenk / resolver.c
Created November 21, 2017 12:26
ViewSourceWith.cpp
/**
* A Firefox Quantum ViewSourceWith replacement for Windows.
* When compiled, this behaves like an editor, i.e. you call
* it with a file as an argument. It tries to extract then
* the real file to open.
*
* Todo:
*
* 1) Read editor path from INI file with GetPrivateProfileString
* 2) hide the console when opening the real notepad
@svenk
svenk / ViewSourceWith.ps1
Created November 17, 2017 11:59
A Firefox Quantum ViewSourceWith extension workaround with Firefox's source.editor.external
# A Firefox Quantum ViewSourceWith replacement for Windows
#
# Usage:
# (1) Save as C:\path\to\script.ps1
# (2) In order to run your own powershell scripts, start PowerShell
# as Administrator and run the command
#
# Set-ExecutionPolicy RemoteSigned
#
# (2) You can test the proper running (from a user cmd):
@svenk
svenk / yaml-reader.php
Created November 3, 2017 17:53
Multi document YAML reader for PHP
<?php
/*
* There is currently no pure PHP yaml parser capable of reading
* multi documents (--- separated). I have tested both spyc and Symfony's YAML.
*
* Therefore I parse YAML with Python, encode it to json and read the json in
* PHP. The json is cached on disk.
*
* Public Domain by http://github.com/svenk
*/
@svenk
svenk / extract.c
Created February 6, 2017 20:55
Convert a GIMP exported Header file back to a PNM Image which can be viewed with any program
#include "The-GIMP-exported-file.h"
#include <stdio.h>
/* This code converts a GIMP C export back to a readable PNM Image. Such
header file looks like
static const struct {
unsigned int width;
unsigned int height;
unsigned int bytes_per_pixel; // 2:RGB16, 3:RGB, 4:RGBA
@svenk
svenk / Makefile
Created December 15, 2016 22:16
Some Fortran/C linking test
CC=gcc
F90=gfortran
all:
${CC} -Wall -c main.c
${F90} -c aux.f90
${CC} -o test main.o aux.o -lgfortran
clean:
rm *.o test
@svenk
svenk / 3FarbenLED.cpp
Last active November 2, 2016 18:40
3Farben LED Arduino für Heribert
/**
* Kommentierte Fassung fuer ein Arduino-Testprogramm
**/
// Anschlussbelegung:
#define PIN_ROT 2
#define PIN_GRUEN 3
#define PIN_BLAU 4
// Taste (Button) an Pin 7 anschließen!