Skip to content

Instantly share code, notes, and snippets.

View Stefan-Code's full-sized avatar

Stefan Kuntz Stefan-Code

View GitHub Profile
@Stefan-Code
Stefan-Code / stopwatch.py
Created January 1, 2023 21:17
Python stopwatch context manager to measure the time of operations
import time
from contextlib import ContextDecorator
class Stopwatch(ContextDecorator):
"""
Context manager to time the duration of operations.
Example:
>>> import time
>>> with Stopwatch(timer='wall') as sw:
@Stefan-Code
Stefan-Code / si_parse.py
Created December 31, 2022 17:17
Convert strings with SI prefixes to a floating point number
import re
def si_parse(s):
"""
Convert a string `s` with SI prefix scaling to a float.
Example:
>>> si_parse('1k')
1000.0
>>> si_parse('1000 mm')
@Stefan-Code
Stefan-Code / si_format.py
Last active July 23, 2022 17:15
Format numbers with the appropriate SI prefix
def si_format(number, unit=''):
"""
Formats a number with the appropriate SI prefix.
Example:
>>> si_format(500000, unit='B')
'500 KB'
>>> si_format(2000000, unit='B')
'2 MB'
>>> si_format(1e-6, unit='m')
# Edit or create /etc/apt/preferences
Package: firefox
Pin: origin "archive.ubuntu.com"
Pin-Priority: 999
#!/bin/bash
exitcode=1
mountpoints="/run/user/1000/gvfs"
#do check if the correct usb mtp device is mounted
for mountpoint in $mountpoints/mtp*/;
do
echo "Testing $mountpoint"
sync_folder="$mountpoint/Internal shared storage"
#if the device contains a .sync_boox file in internal storage, proceed
if test -e "$sync_folder/.sync_boox";then
# choco list --local-only --id-only
# Chocolatey v0.10.11
7zip
7zip.install
adb
adobereader
android-sdk
astrogrep
audacity
autohotkey.portable
# creates a LaTeX representation of the math for a scipy linregress straight line fit object
def fit_latex(fit, precision=2, xname='x', yname='y', xunit='', yunit='', stats_precision=4, show_R2=True, show_standard_error=True):
return '${y}={slope}{x}{unit_conversion} {sign} {intercept}{yunit}${stats}'.format(slope=round(fit.slope, precision),
intercept=round(abs(fit.intercept), precision),
x=xname,
y=yname,
sign='+' if fit.intercept >= 0 else '-',
yunit=r'\,\mathrm{{{}}}'.format(yunit) if yunit else '',
unit_conversion=r'\,\frac{{\mathrm{{{yunit}}}}}{{\mathrm{{{xunit}}}}}'.format(yunit=yunit, xunit=xunit) if xunit or yuni
# systemctl edit getty@tty1
[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin user --noclear %I $TERM
@Stefan-Code
Stefan-Code / ubuntu-provisioning.py
Last active December 10, 2017 20:17
Provisioning script to setup a Ubuntu VM from a template. Most importantly, this will ensure SSH keys are unique.
#! /usr/bin/env python3
import os
import locale
import subprocess
import sys
import shutil
from dialog import Dialog
#removes first audio stream without transcoding
ffmpeg -i file.mp4 -map 0:0 -map 0:2 -acodec copy -vcodec copy new_file.mp4
#!/bin/bash
for name in *.mkv; do
ffmpeg -i "$name" -map 0:0 -map 0:2 -acodec copy -vcodec copy "converted/$name"
done