Skip to content

Instantly share code, notes, and snippets.

View harkabeeparolus's full-sized avatar

traal harkabeeparolus

View GitHub Profile
@harkabeeparolus
harkabeeparolus / strtobool.py
Created March 16, 2024 07:59
Convert a string representation of truth to True or False
# This used to exist in distutils, which is removed since Python 3.12
# https://github.com/pypa/distutils/blob/main/distutils/util.py#L340
def strtobool(val: str) -> bool:
"""Convert a string representation of truth to True or False.
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values are 'n', 'no',
'f', 'false', 'off', and '0'. Raises ValueError if 'val' is anything else.
"""
val = val.strip().casefold()
@harkabeeparolus
harkabeeparolus / bash_startup_rant.md
Last active February 4, 2024 11:11
My 2020 Rant About Bash Startup Files

(Source: ofek/userpath wrong shell #3)

bash Background

Rant mode ON. 🙀

Just FYI (Unix geek here), I think bash is broken by design when it comes to startup files. This is especially true in big university multiuser Unix installations, which is my own background. For proof, run man bash, look under the "INVOCATION" section, and prepare to have your mind blown. Compare and contrast this with man zsh and look under "STARTUP/SHUTDOWN FILES", for a much less insane approach to system and user configuration. IMHO. 👀

Red Hat Enterprise Linux, Ubuntu, and probably other OSes or distros attempt to work around the bash problem by providing clever dotfiles in "/etc/skel" -- these are dotfiles that are copied into a new user's home directories by default when the new user is created. These default dotfiles are supposed to create a complex structure where each user has a ~/.bash_profile (OR ~/.profile on Ubuntu) which always sources the use

@harkabeeparolus
harkabeeparolus / csv-display.py
Last active January 15, 2024 15:08
Try to autodetect and display CSV or TSV data in a terminal. Fall back to plain text if auto detection fails.
#! /usr/bin/env python3
"""Try to autodetect, parse and display CSV, but fallback to plain text otherwise."""
# Copyright 2024 Fredrik Mellström <https://github.com/harkabeeparolus>
# MIT License; SPDX short identifier: MIT
import argparse
import csv
import io
@harkabeeparolus
harkabeeparolus / plist2json.py
Created July 19, 2023 09:35
When `plutil --convert json` fails due to "invalid object in plist for destination format", use Python to serialize everything except for the binary data. Based on a Stack Overflow answer.
#! /usr/bin/env python3
"""Read Apple Plist files and output JSON data."""
# https://stackoverflow.com/a/72958435/5201675
import argparse as ap
import json
import plistlib
import sys
from typing import IO, Any
@harkabeeparolus
harkabeeparolus / Typer_cheat_sheet.md
Last active April 19, 2024 04:53
Typer — my attempt at reference documentation
@harkabeeparolus
harkabeeparolus / indent.py
Created March 15, 2023 15:45
Indent text in Rich, optionally with different indent on the first line.
"""Indent Rich text or renderable."""
from dataclasses import dataclass
from typing import Optional
from rich.abc import RichRenderable
from rich.console import Console, ConsoleOptions, RenderResult
from rich.segment import Segment
@harkabeeparolus
harkabeeparolus / robber.py
Last active November 3, 2022 13:08
Decode the Swedish "Robber Language" into plain text.
#! /usr/bin/env python3
"""Decode or encode the Swedish Robber Language.
Proof of concept by traal (Fredrik Mellström).
"""
import argparse
import re
import sys
@harkabeeparolus
harkabeeparolus / template.bash
Last active January 18, 2024 10:42
Standard bash script template with unofficial strict mode
#! /usr/bin/env bash
set -o errexit -o nounset -o errtrace -o pipefail
IFS=$'\n\t'
shopt -s nullglob
# shellcheck disable=SC2154
trap 's=$?; echo >&2 "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
warn() { [ $# -ge 1 ] && printf >&2 "%s\n" "$*"; }
main() {