Skip to content

Instantly share code, notes, and snippets.

View egregius313's full-sized avatar

Edward Minnix III egregius313

View GitHub Profile
@egregius313
egregius313 / unpackable.py
Created December 23, 2017 18:31
Allow tuple unpacking on classes defined with @attr.s
# Allow tuple unpacking on classes defined with
# @attr.s decorator. Uses the attribute information
# from the definition of the class using attrs
#
# See also:
# http://www.attrs.org
# https://github.com/python-attrs/attrs
import hashlib
import linecache
@egregius313
egregius313 / stop_writing_classes_game_of_life.py
Created August 25, 2017 01:15
Adaptation of the "Conway's Really Simple Game" code from Jack Diederich's talk "Stop Writing Classes".
# stop_writing_classes_game_of_life.py
"""
Adaptation of the "Conway's Really Simple Game" code
from Jack Diederich's talk "Stop Writing Classes".
Meant to help make some of the logic more explicit.
"""
from itertools import chain
@egregius313
egregius313 / maybe.py
Last active August 25, 2017 23:33
Basic implementation of the "Maybe a" types from Haskell in Python
# maybe.py
"""
Basic implementation of Haskell's Data.Maybe module.
The changes in the API are as follows:
- `maybe` is replaced with the `Maybe` class constructor
- `isJust` and `isNothing` are replaced with the builtin `isinstance`
- `listToMaybe` is replaced with the class method `Maybe.from_iterable`
- `maybeToList` is replaced with an implementation of `__iter__`,
so calls to `list()` will work
@egregius313
egregius313 / blob.clj
Created March 23, 2017 19:06
Basic Clojure implementation of the Blob problem for solving mazes
(ns blob)
(defn abnormal? [color]
(= color :abnormal))
(defn color [grid x y]
(get-in grid [x y]))
@egregius313
egregius313 / cross-product.clj
Last active March 9, 2017 15:46
Basic implementation of cross product using reduce.
(defn product [a & bs]
"The cross product of sets."
(reduce
(fn [xss ys]
(mapcat (fn [xs] (map (partial conj xs) ys)) xss))
(map vector a)
bs))
@egregius313
egregius313 / init_repr.py
Created March 5, 2017 05:15
A simple class decorator to define the __repr__ method based on the signature of the __init__ method.
"""
A simple class decorator to define the __repr__ method
based on the signature of the __init__ method.
"""
__author__ = 'egregius313'
_repr_template = """
def __repr__(self):
'Return repr(self)'
@egregius313
egregius313 / timethis.py
Last active February 27, 2017 15:29
Simple context manager for timing Python code
#!/usr/bin/env python
from contextlib import contextmanager
import time
@contextmanager
def timethis(timer=time.perf_counter):
"""
Small timing function. It is meant to be used as a
context manager, and it returns a lambda that when