Skip to content

Instantly share code, notes, and snippets.

@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)
#
@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 / 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 / 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 / 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 / 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 / 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 / collatz.py
Created July 20, 2021 00:27
Python Collatz Sequence as in OEIS A006577
#!/usr/bin/env python3
def collatz_sequence(n):
"""Generate the collatz sequence for n (see OEIS A006577)."""
c = int(n)
if c < 1 or c != n:
raise ValueError(f"Argument n ({n}) must be a positive integer")
while c != 1:
@outofmbufs
outofmbufs / pymroinpy.py
Created October 1, 2021 19:13
Rewrote the python MRO (C3 method resolution order) in python, just as an exercise in understanding it
# toy example re-implementing C3 method resolution order
# (MRO, as used for python multi-inheritance)
#
# Algorithm snippets in comments come from:
# https://www.python.org/download/releases/2.3/mro/
# and are notated as [2.3MRO]
#
# A Hier represents a class hierarchy, with a name, and a possibly-empty
# list of bases (each base itself also a Hier).
@outofmbufs
outofmbufs / A046253.py
Created February 21, 2022 16:42
Compute OEIS A046253 sequence. For each digit d in a number n, compute d**d; if they sum to n then n is in the sequence.
from functools import lru_cache
@lru_cache
def _powA046253(i, n):
"""Return i**n for two integers i and n. Defines 0**0 to be ZERO."""
r = i
while n > 1:
r *= i
n -= 1
return r