Skip to content

Instantly share code, notes, and snippets.

@outofmbufs
outofmbufs / keithnum.py
Created Apr 7, 2021
Generate keith numbers - OEIS A007629
View keithnum.py
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 Mar 8, 2021
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!)
View tuplify.py
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 Jan 3, 2021
Python decorator to convert exception into function return value
View exwrap.py
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 Dec 15, 2020
Python argparse helper for parsing comma-separated options and other list-like parameters. Enables things like "--blah 17,42"
View argtuple.py
# 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 Apr 10, 2019
python function to decode multiple JSON representations in a single file (stream)
View loadmultijson.py
# 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
Arduino code to trigger an IFTTT/Maker event
View iftttmaker.ino
// 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 Aug 5, 2015
Python class implementing a Sieve of Eratosthenes functionality for vector tuples
View vsieve.py
#!/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)
#