Skip to content

Instantly share code, notes, and snippets.

@bsolomon1124
bsolomon1124 / game_of_life.py
Last active March 20, 2024 11:09
Conway's game of life in NumPy
"""NumPy/SciPy implementation of Conway's Game of Life.
https://bitstorm.org/gameoflife/
"""
import argparse
import curses
import getopt
import time
from itertools import repeat

Keybase proof

I hereby claim:

  • I am bsolomon1124 on github.
  • I am bsolomon1124 (https://keybase.io/bsolomon1124) on keybase.
  • I have a public key ASAigX8YlDtlWX5TcCVax6Ys_FdC3CO0YBHzsu9ZId56WAo

To claim this, I am signing this object:

@bsolomon1124
bsolomon1124 / toc.py
Created May 13, 2018 04:56
Quick and dirty TOC generator for Markdown files.
"""Generate TOC from Markdown (.md) file headings."""
import io
import re
import string
import sys
# We need .*? to avoid greediness.
code_block = re.compile(r'^```.*?```', re.M|re.S)
heading = re.compile(r'^#+ .*?$', re.M)
@bsolomon1124
bsolomon1124 / structures.py
Created June 20, 2018 20:07
Dictionary subclasses supporting dot access for keys.
"""Dictionary subclasses supporting dot access for keys."""
__all__ = ['AccessDict', 'SafeAccessDict']
__author__ = 'Brad Solomon <bsolomon@protonmail.com>'
class AccessDict(dict):
"""Dictionary with dot access, setting, and deletion of values.
Otherwise, behaves the same. (KeyError is still possible.)
@bsolomon1124
bsolomon1124 / timeit.py
Created June 21, 2018 14:40
Decorator version of `timeit.repeat()`
import functools
import gc
import itertools
from timeit import default_timer as _timer
_repeat = functools.partial(itertools.repeat, None)
def timeit(repeat=3, number=1000):
"""Decorator: prints time from best of `repeat` trials.
@bsolomon1124
bsolomon1124 / mapto.py
Created June 22, 2018 16:21
`os.urandom()` maps to ints in [0, 255]
# Informal proof
from math import inf
mn, mx = inf, -1 * inf
for _ in range(100000):
b = os.urandom(10)
new_min = min(b)
new_max = max(b)
@bsolomon1124
bsolomon1124 / bytes_to_int.py
Last active June 26, 2018 14:10
Demonstration of `int.from_bytes()`
# Demonstration of how `int.from_bytes()` converts a Python bytes obj to int.
# Used in random module's `SystemRandom.random()` to generate a float in [0.0, 1.0) from `os.urandom()`
# https://github.com/python/cpython/blob/c6040638aa1537709add895d24cdbbb9ee310fde/Lib/random.py#L676
#
# An example: the number 1984 can be "decomposed" in the decimal system (base 10)
# as (1 * 10 ** 3)
# + (9 * 10 ** 2)
# + (8 * 10 ** 1)
# + (4 * 10 ** 0)
#
@bsolomon1124
bsolomon1124 / urlsafe.py
Created June 28, 2018 14:45
Demonstration of `secrets.token_urlsafe()`
import binascii
import math
import os
# binascii.b2a_base64(data, *, newline=True)
# Convert binary data to a line of ASCII characters in base64 coding.
# The return value is the converted line, including a newline char if
# newline is true. The output of this function conforms to RFC 3548.
@bsolomon1124
bsolomon1124 / basic.py
Created July 11, 2018 15:49
Demonstrates different LogRecord attributes (formatters)
"""Demonstrates different LogRecord attributes (formatters).
https://docs.python.org/3/library/logging.html#logrecord-attributes
"""
import logging
# Notes
# -----
# pathname: this is relative!
# created: time in seconds since the epoch as a float
@bsolomon1124
bsolomon1124 / rolling.pyx
Last active August 7, 2018 20:29
Rolling window stats with frequency offset and centered window
# cython: profile=False
from cython cimport boundscheck, wraparound
# Py_ssize_t is the proper C type for Python array indices.
from cython cimport Py_ssize_t
import numpy as np
cimport numpy as cnp
from numpy cimport ndarray, double_t, int64_t