Skip to content

Instantly share code, notes, and snippets.

View Techcable's full-sized avatar
🇺🇸
Procrastinating

Techcable

🇺🇸
Procrastinating
View GitHub Profile
@Techcable
Techcable / urljoin.py
Created July 24, 2025 21:15
A small script to join URLs against a base path
#!/usr/bin/env python3
"""A command line interface to urllib.parse.urljoin"""
import argparse
from urllib.parse import urljoin
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("base", help="The base URL")
@Techcable
Techcable / strip_comments.py
Created July 18, 2025 22:10
Strip line comments from a file
#!/usr/bin/env python3
"""
Strip line comments from a file
By default, this requires whitespace to surround a comment for it to be valid.
This means that `a#a` is not a valid comment.
"""
from __future__ import annotations
@Techcable
Techcable / slowprint.py
Created May 26, 2025 03:18
Slowly print lines, for testing command line interfaces
#!/usr/bin/env python3
"""
Print lines slowly
Intended for testing command line interfaces
"""
import argparse
import itertools
import time
@Techcable
Techcable / extract_table.py
Created April 4, 2025 19:21
Parses a markdown table into a CSV file
#!/usr/bin/env -S uv run --script
# Extracts data from the first markdown table found in the input file,
# converting it into CSV
# /// script
# dependencies = [
# "marko~=2.1"
# ]
# ///
import csv
import sys
@Techcable
Techcable / makechawan.fish
Last active March 31, 2025 15:53
Build chawan browser on macOS (https://sr.ht/~bptato/chawan/)
#!/usr/bin/env fish
set --local openssl_prefix $(brew --prefix openssl)
set --local libssh_prefix $(brew --prefix libssh2)
set -gx CFLAGS "-I$openssl_prefix/include -I$libssh_prefix/include -lssl -lssh2"
set -gx LDFLAGS "-L$openssl_prefix/lib -L$libssh_prefix/lib -lssl -lssh2"
if not test -f ./nim.cfg; or not test -f ./README.md
echo "ERROR: Should be in chawan root directory" >&2;
@Techcable
Techcable / back2dir.fish
Created February 7, 2025 21:18
Return to the most recently used directory when starting the fish shell
# Return to the most recently used directory when starting fish shell
#
# Ported from this xontrib: https://github.com/anki-code/xontrib-back2dir
#
# Uses fish "universal variables" for storage
#
# TODO: Is using universal variables (as opposed to a dedicated file)
# going to slow things down or hurt SSD?
# Right before the first pompt is shown, we go back to the last directory
#!/usr/bin/env python3
# A nice little script to hash the contents of tarfiles
# Works in parallel
import shutil
import os
import re
import sys
import subprocess
from math import ceil
@Techcable
Techcable / magic_method_lookup.py
Created January 29, 2025 01:58
Examples of how `str(x)` differs from `x.__str__()`
"""
Examples of how `str(x)` differs from x.__str__`()
This behavior is documented here: https://docs.python.org/3.11/reference/datamodel.html#special-lookup
The reason for this is performance, so `x + y` can avoid a dictionary lookup and/or `__getattribute__` call
"""
def override_str(*args):
return "bar"
@Techcable
Techcable / state_population.2020
Created January 4, 2025 19:00
United States Population by State, According to 2020 Census (taken from Wikipedia)
state,census2020
California,"39,538,223"
Texas,"29,145,505"
Florida,"21,538,187"
New York,"20,201,249"
Pennsylvania,"13,002,700"
Illinois,"12,812,508"
Ohio,"11,799,448"
Georgia,"10,711,908"
North Carolina,"10,439,388"
@Techcable
Techcable / maybe_const_fn.rs
Created September 10, 2024 22:51
Defines the `maybe_const_fn` macro
//! Defines the [`maybe_const_fn`] macro.
/// Mark a function as `const` if a `cfg!(...)` attribute evaluates to true.
///
/// This has the same effect as using `#[rustversion::attr(version_test, const)]`,
/// but is implemented as a declarative macro instead of a procedural one.
///
/// It is subject to some minor limitations (see below).
///
/// If these are unacceptable,