Skip to content

Instantly share code, notes, and snippets.

@outofmbufs
outofmbufs / driftadjuster.py
Created August 30, 2024 21:48
Dynamically calculate delay needed to execute code at a desired constant interval
# MIT License
#
# Copyright (c) 2024 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 / decoclass.py
Created August 19, 2024 22:27
DecoClass: base class for implementing decorators as class objects in python
# MIT License
#
# Copyright (c) 2024 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 / adaptivedelay.py
Created June 1, 2024 20:31
Python Adaptive delay - tracks cumulative delay and adjusts the sleep interval accordingly
import time
from collections import deque
from itertools import pairwise, cycle
class AdaptiveDelay:
def __init__(self, interval, /, *, window_seconds=5):
self.interval = interval
self.window_seconds = window_seconds
@outofmbufs
outofmbufs / tokutil.py
Created January 23, 2024 04:26
add unget, peek, mark/accept functionality to a stream of tokens
# MIT License
#
# Copyright (c) 2024 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 / gist:2117ba3435eaf75003af6b4d9ce0441a
Created July 4, 2023 19:17
OEIS A109732 and (related) A261414
# MIT License
#
# Copyright (c) 2023 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 / OEIS_A059405.py
Last active June 4, 2023 02:38
Python search algorithm for OEIS integer sequence A059405
# MIT License
#
# Copyright (c) 2023 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 / nytdigits.py
Last active April 23, 2023 03:38
Solver for NYTimes Digits games. Use with my puzzlesolver gist. EDIT: also handles chain mode now.
# MIT License
#
# Copyright (c) 2023 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 / keyed_lru_cache.py
Created February 16, 2023 21:51
enhance python lru_cache with key capability so it will only use a subset of the arguments for cache lookup
# MIT License
#
# Copyright (c) 2023 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 / nistrandomtests.py
Created February 5, 2023 22:51
small subset of NIST random number tests; see NIST 800-22; NOTE: requires scipy
import math
import random
import functools
import itertools
from collections import Counter
try:
from scipy.special import gammaincc
except ModuleNotFoundError:
def gammaincc(x1, x2):
@outofmbufs
outofmbufs / optionalarg.py
Created November 19, 2022 22:08
Python decorator to call a function and omit arguments that need to take on their default values, but only sometimes, so they want to be present in the call syntax but sometimes should be conditionally defaulted (useful if the function sets a non-trivial default value)
import functools
# Given a function such as this:
#
# def example(a, b=17):
# pass
#
# If code needs to conditionally call example() sometimes with b supplied
# and sometimes with b defaulted, that ends up looking like this:
#