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 / 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 / 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 / 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 / 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)" ;;
@gwerbin
gwerbin / multiprocessing_logging.py
Created August 4, 2021 23:57
Demo of logging in a multi-processing context.
"""Demo of logging in a multi-processing context.
Based on the Python "Logging Cookbook":
https://docs.python.org/3/howto/logging-cookbook.html#logging-to-a-single-file-from-multiple-processes
Note on `QueueListener`:
The main difference between the Cookbook example and this demo is that I use
the `QueueListener` convenience class, whereas they write a bespoke
`listener_process` function which does more or less the same thing as
@gwerbin
gwerbin / TaxExample.idr
Last active July 6, 2021 05:48
Example of dependent types and "proof-carrying code" in a simple, practical programming task.
module TaxExample
import Data.So
-- Can't use `total` as a variable name! Boo :(
%default total
-- NOTE: Weird things happen when you use the same name in both a record
-- parameter and a record field name. Don't do it.