Skip to content

Instantly share code, notes, and snippets.

View burkestar's full-sized avatar

Dustin Burke burkestar

View GitHub Profile
@burkestar
burkestar / ExtensibleAPIMulti.ipynb
Last active June 21, 2016 17:36
Extensible API
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# Finds the longest streak and the current streak within a sequence.
# The sequence could correspond to consecutive days of Github commit history.
# The assumptions is that it is ordered chronologically so the current streak is computed from the tail end of the sequence.
# reduce maintains the state as a tuple consisting of (longest streak count, current streak count)
# The int(bool(y)) trick is to convert a numeric into a binary 0 or 1 which will determine whether or not the current streak should continue.
# Time complexity: O(n) since reduce will traverse each item in the sequence exactly once.
# Space complexity: O(n) + k to store the original sequence plus the additional state tuple that propagates through reduce.
data = [0, 2, 4, 6, 4, 32, 24, 4, 4, 5, 0, 2, 3, 4, 0, 0, 0, 0, 2, 0, 0, 4, 7, 9]
longest, current = reduce(lambda state, y: (max(state[0], state[1] + 1 if state[1] and y else int(bool(y))), state[1] + 1 if state[1] and y else int(bool(y))), data, (0, 0))
class Timer:
"""
A ``Timer`` ContextManager to simplify timing sections of code.
"""
def __init__(self, name=None, logger=None, log_level=logging.DEBUG):
"""
Constructs a ``Timer``.
Parameters
----------
@burkestar
burkestar / waitForKeyElements.js
Created March 21, 2016 16:25
greasemonkey waitForKeyElements
/*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
that detects and handles AJAXed content.
Usage example:
waitForKeyElements (
"div.comments"
, commentCallbackFunction
);
@burkestar
burkestar / pytest test class with fixtures and patchers
Last active March 9, 2016 16:12
pytest test class that works with pytest fixtures and has some magic for scaffolding patchers that are reused across all class test methods
from collections import OrderedDict
import pytest
class MagicalPatchersMixin(object):
"""
A mixin for pytest xunit-style classes that provides scaffolding for setting up
patchers more easily that are setup for each class test method.
# Xcode convenience function
function xcode {
if (( $# == 0 )); then
if [ -f *.xcodeproj ]; then
open *.xcodeproj
else
open -a Xcode.app
fi
fi
open -a Xcode.app $1