Skip to content

Instantly share code, notes, and snippets.

View ptmcg's full-sized avatar

Paul McGuire ptmcg

View GitHub Profile
@ptmcg
ptmcg / shuffled.py
Last active October 26, 2019 15:53
shuffled - one-line lambda to shuffle any sequence, not just shuffling lists in place
# shuffled.py - faster than random.shuffle, and will accept any sequence, not just indexables (like lists)
shuffled = lambda seq, rnd=random.random: sorted(seq, key=lambda _: rnd())
print(shuffled(range(20))
@ptmcg
ptmcg / argparse_demo.py
Last active November 9, 2023 22:06
Demo of creating custom argument types for argparse (percent and Enum)
#
# argparse_demo.py
#
# Paul McGuire - April, 2019
#
# Presented as a lightning talk at PyTexas 2019
#
# Gist url: https://gist.github.com/ptmcg
#
import sys
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ptmcg
ptmcg / orbit_with_multi_dispatch.py
Last active November 16, 2019 17:57
classmethod to validate mix-and-match kwargs
"""
https://chat.stackoverflow.com/transcript/message/47865711#47865711
Well it's mainly "utility functions", ie I need to define a (keplerian) orbit. Which is normally done by giving
semi-major axis, eccentricity, inclination, argument periapsis and longitude ascending node. However there are lots
of "variations": ie for parabolic orbits the semi major axis is replaced by semi latus rectum. Or you can just use
state variables at a point (position + velocity in 3 dimensions). Or you could replace the semi major axis +
eccentricity by peri + apo apsis.
"""
@ptmcg
ptmcg / graph_algebra.py
Last active February 11, 2020 10:29
Graph algebra parser/evaluator
#
# graph_algebra.py
#
# A rough implementation of a graph algebra.
#
# References:
# https://statagroup.com/articles/an-algebra-for-graph-structures
# https://www.youtube.com/watch?v=EdQGLewU-8k
#
# Copyright 2020, Paul McGuire
@ptmcg
ptmcg / corinthians_I.py
Last active April 2, 2021 08:19
Recreate popular verses on love from I Corinthians
#
# corinthians_I.py
#
# Class definitions to use when recreating I Corinthians 13:4-8 from
# the Python prompt.
#
# Copyright 2020, Paul McGuire
#
import sys
sys.excepthook = lambda *args: print(args[0].__name__)
@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)
@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 / 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 / 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