Skip to content

Instantly share code, notes, and snippets.

View ptmcg's full-sized avatar

Paul McGuire ptmcg

View GitHub Profile
@ptmcg
ptmcg / computer_thinking_spinners.py
Last active March 1, 2022 20:16
Rich spinners for "the computer is thinking"
import rich.progress
import rich.spinner
import time
import random
class RandomChars(rich.progress.ProgressColumn):
"""Simulation of computer 'thinking' by displaying random characters
Args:
chars (str): characters from which to choose for display. Defaults to 0-9A-F.
@ptmcg
ptmcg / mixed_type_hello.py
Last active February 13, 2022 18:44
Hello, World rendered in a variety of Unicode characters
def 𝚑𝓮𝖑𝒍𝑜():
try:
𝔥e𝗅𝕝𝚘︴ = "Hello"
𝕨𝔬r𝓵ᵈ﹎ = "World"
ᵖ𝖗𝐢𝘯𝓽(f"{𝗵e𝓵𝔩º_}, {𝖜ₒ𝒓lⅆ︴}!")
except 𝓣𝕪ᵖe𝖤𝗿ᵣ𝖔𝚛 as ⅇ𝗑c:
𝒑rℹₙₜ("failed: {}".𝕗𝗼ʳᵐªt(ᵉ𝐱𝓬))
# snippet from unittest/util.py
@ptmcg
ptmcg / bmp.py
Last active August 29, 2021 23:19
"""
bmp.py - module for constructing simple BMP graphics files
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
#
# THE PROBLEM
#
# create a generator and list out its items
a = (n**2 for n in range(10))
print(list(a))
# listing it out again creates an empty list because the generator has been consumed;
# generators in this state will continue to raise StopIteration, but caller will take
@ptmcg
ptmcg / rcu.py
Last active November 19, 2020 05:39
RCU class for read-copy-update synchronization to a shared resource
#
# rcu.py
#
# Paul McGuire - November, 2020
#
from contextlib import contextmanager
import copy
import threading
@ptmcg
ptmcg / py_rac.py
Last active November 5, 2020 06:32
A python racter-like
#
# py_rac.py
#
# Copyright 2020, Paul McGuire
#
names = ("Voltaire|Samuel Johnson|Valery|Dante|"
"Nietzsche|Kant|Lao Tzu|Macchiavelli|Einstein|"
"Russell|Leonardo|Plato|Aristotle|Socrates|Diderot|"
"Descartes|Thomas Aquinas|Hawking|Emerson|Faulkner|"
@ptmcg
ptmcg / auto_namedtuple.py
Last active August 19, 2021 07:43
DRY namedtuple
#
# auto_namedtuple.py
#
# I like my martinis and my Python DRY
#
# Copyright 2020, Paul McGuire
#
import traceback
from collections import namedtuple
@ptmcg
ptmcg / song.py
Last active October 21, 2020 13:41
A familiar song
import time, sys, base64
for i, v in enumerate((getattr, time, sys, "sleep",
"decode", "bytes", "stdout",
"write", "flush", 1.0, 6,
base64, "upper", 1000.0, "'",
print, 0), start=3):
vars()['_'*i] = v
_ = (
b'RGFpc3kgRGFpc3kgZ2l2ZSBtZSB5b3VyIGFuc3dlciBk'
b'bwpJJ20gaGFsZiBjcmF6eSBhbGwgZm9y\nIHRoZSBsb3'
@ptmcg
ptmcg / rk.py
Last active October 12, 2020 16:28
Runge-Kutta integrator in Python
# rk.py
#
# Copyright 2020, Paul McGuire
#
from typing import Callable, Sequence
from array import array
class RKIntegrator:
"""
@ptmcg
ptmcg / json_item_accessor.py
Last active March 9, 2020 05:18
Simple JSON item accessor
#
# simple JSON item accessor
#
import json
def json_accessor(fn):
def _inner(src, keypath, *args):
if not isinstance(keypath, (tuple, list)):
keypath = [keypath]
src_dict = json.loads(src)