Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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
(* Allow for nicer print output *)
SetOptions[SelectedNotebook[],
PrintingStyleEnvironment -> "Printout", ShowSyntaxStyles -> True]
@svenk
svenk / scripd-scraping.py
Created January 29, 2020 11:49
Scribd JPG scraping
#!/usr/bin/env python3
# Scraping non-downloadable books/documents
# by downloading them as JPG files
# Public Domain.
import requests, json, sys
# Attention, token changes regularly... You should copy all ~10 minutes from Firebug
@svenk
svenk / index.php
Created February 10, 2020 09:56
Share secrets public over the web
<?php
// Enforce HTTPS
if($_SERVER["HTTPS"] != "on") { header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); exit(); }
?>
<!doctype html>
<html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<body style="padding:5%; font-family: sans-serif">
<h1>Some secret</h1>
@svenk
svenk / spin-the-bottle.php
Last active December 31, 2020 15:40
"Spin the bottle" with Zoom using the Zoom Webhook API
<html>
<meta charset="utf-8">
<title>Genie in a Bottle</title>
<meta author="SvenK">
<meta date="31.12.2020">
<?php
# First of all, this is supposed only to work for a specific
# room. Fix the room id here:
@svenk
svenk / pomodoro-report.py
Created January 13, 2021 20:33
Simple logging
#!/usr/bin/env python
import os, re, sys
from os import path
from pathlib import Path
from datetime import datetime, timedelta
from prompt_toolkit import prompt # bundled with iPython
from prompt_toolkit.completion import WordCompleter
@svenk
svenk / README.md
Created September 16, 2021 10:38
Mediawiki backup to Git Repository

Automatic backup to some SECURE git space for a MediaWiki

This is carried out with two shell scripts: One responsible for gathering the wiki contents in various formats as single files, and one responsible for uploading to a git repository.

Directory structures