Skip to content

Instantly share code, notes, and snippets.

View gwerbin's full-sized avatar
💭
I might be slow to respond.

Greg Werbin gwerbin

💭
I might be slow to respond.
View GitHub Profile
@gwerbin
gwerbin / indent_lines.py
Created November 7, 2022 21:14
Indent lines of text in a string
def indent_lines(text: str, spaces: int | str = 4) -> str:
r"""Indent lines of text by some fixed amount.
:param text: The text to indent.
:param spaces: The amount of spaces to indent by, or an arbitrary string to use as a
line prefix.
:returns: The indented text.
"""
if isinstance(spaces, int):
spaces = " " * spaces
@gwerbin
gwerbin / timer.py
Last active November 7, 2022 21:14
Basic start/stop timer context manager
# TODO: Thread & async safety
import sys
import time
from collections.abc import Callable
if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self
@gwerbin
gwerbin / lazy_string.py
Last active November 7, 2022 21:14 — forked from mypy-play/main.py
Wraps an object with a arbitrary str conversion, for logging.
import sys
from collections import UserString
from collections.abc import Callable, Mapping
from typing import Any, ParamSpec, TypeVar, overload
if sys.version_info >= (3, 11):
from typing import Self, Unpack
else:
from typing_extensions import Self, Unpack
@gwerbin
gwerbin / easylog.py
Created November 7, 2022 21:09
Easy logging setup
r"""Quickly configure a single logger.
Based on: https://docs.python.org/3/library/logging.html#logging.basicConfig
Adapted from my rejected PR:
https://github.com/python/cpython/compare/main...gwerbin:cpython:gwerbin/basicconfig-any-logger
"""
import logging
from collections.abc import Callable, Sequence
@gwerbin
gwerbin / geodesics.py
Last active November 4, 2022 18:56
Geodesic calculations
# -*- coding: utf-8 -*-
# Copyright © 2022 by Gregory Werbin
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED “AS IS” AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
# TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
@gwerbin
gwerbin / whitespace.py
Last active April 13, 2022 00:05
Python whitespace definition
import unicodedata
def is_whitespace(c: str) -> bool:
"""Detect if a character is "whitespace".
As of 3.10, this is how CPython defines "whitespace" for string operations like `str.split`.
Sources:
* https://github.com/python/cpython/blob/v3.10.4/Objects/unicodeobject.c#L311-L340
* https://github.com/python/cpython/blob/v3.10.4/Tools/unicode/makeunicodedata.py#L420-L422=
@gwerbin
gwerbin / python-module-name
Last active December 22, 2021 21:33
Convert a filename into a Python module name
#!/usr/bin/env zsh
setopt \
err_exit \
pipe_fail \
warn_create_global \
warn_nested_var \
no_unset
version='1.0.0'
@gwerbin
gwerbin / DoubleStuff.idr
Last active November 1, 2021 14:29
Proposal for `%foreignImport`
module DoubleStuff
%foreignImport "scheme,chez:(double-stuff utils)"
"python:double_stuff"
-- "python:double_stuff,*"
namespace Exact
%foreign "scheme,chez:idris-fl=-2"
"python:double_stuff.idris_fleq2"
-- "python:idris_fleq2"
@gwerbin
gwerbin / install_idris2.sh
Last active November 1, 2021 04:01
Build and install Idris 2: https://www.idris-lang.org/
#!/bin/sh
### Bootstrap build from an existing Idris 2 compiler.
set -eux
cd idris2
## Build and install to final location, using whatever `idris2` is already in PATH.
install_prefix="${XDG_MISC_HOME:-$HOME/.local/opt}/idris2"
#!/usr/bin/env zsh
emulate zsh
setopt err_exit pipe_fail warn_create_global warn_nested_var no_unset
if (( ${#@} == 0 )); then
data="$(cat)"
elif (( ${#@} == 1)); then
case $1 in
-) data="$(cat)" ;;