Skip to content

Instantly share code, notes, and snippets.

@outofmbufs
outofmbufs / keithnum.py
Created April 7, 2021 17:56
Generate keith numbers - OEIS A007629
def iskeith(n):
"""Return True if n is a Keith number - https://oeis.org/A007629"""
if n < 10:
return False # by definition
# initial sequence seeding is the digits of n
kl = [int(d) for d in str(n)] # crude, but easy. Speed not relevant.
ksum = sum(kl)
@outofmbufs
outofmbufs / tuplify.py
Last active March 8, 2021 15:02
Python code to force something that *might* be a tuple (or a single thing) into a tuple. Useful(?) for processing arguments that can be a "thing" or multiple "things". Also defines a decorator (which seems like a horrible idea, but was fun!)
import functools
from collections import ChainMap
def tuplify(arg, *, keep=str):
"""Return tuple from an iterable or a naked single object.
If arg is a non-iterable object, returns: (arg,)
If arg is iterable, returns: tuple(arg)
@outofmbufs
outofmbufs / exwrap.py
Last active January 3, 2021 23:16
Python decorator to convert exception into function return value
import functools
__IGX_SENTINEL = object() # sentinel for a "default argument" see below
def IgnoreExceptionsDecorator(excepts, *, exception_result=__IGX_SENTINEL):
"""Decorator - catch exceptions and convert to a return value.
'excepts' - exceptions (as a tuple, or a single exception) to catch
@outofmbufs
outofmbufs / argtuple.py
Last active December 15, 2020 14:44
Python argparse helper for parsing comma-separated options and other list-like parameters. Enables things like "--blah 17,42"
# Python support for argument parsing of list-like things (usually comma separated).
# For example:
# def commapair(s):
# return argtuple(s, n=2)
# can then be used as:
# type=commapair
# in argparse for arguments like "--blah 17,42"
#
def argtuple(s, *dflts, n=None, seps=(',',), type=int):
"""Return tuple of 'type' from a,b,c,d... format.
@outofmbufs
outofmbufs / loadmultijson.py
Last active October 18, 2021 17:28
python function to decode multiple JSON representations in a single file (stream)
# The MIT License
#
# Copyright (c) 2019 Neil Webber
#
# 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:
@outofmbufs
outofmbufs / iftttmaker.ino
Last active May 21, 2021 21:44
Arduino code to trigger an IFTTT/Maker event
// The MIT License
//
// Copyright (c) 2015 Neil Webber
//
// 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:
@outofmbufs
outofmbufs / vsieve.py
Created August 5, 2015 19:33
Python class implementing a Sieve of Eratosthenes functionality for vector tuples
#!/usr/bin/env python3
#
# A "sieve of Eratosthenes" for integer vectors of length N.
#
# The original usage of this was for studying pythagorean triples and
# filtering out those that were simply multiples of smaller ones, e.g.
#
# (5, 12, 13) as a "fundamental" Pythagorean triple vs
# (10, 24, 26) as a multiple of (5, 12, 13)
#