Skip to content

Instantly share code, notes, and snippets.

View ChrisTM's full-sized avatar

Christopher Mitchell ChrisTM

  • Google
  • Seattle, WA
View GitHub Profile
@ChrisTM
ChrisTM / ip.py
Last active April 20, 2022 09:35
Convert IPv4 addresses to and from integers
"""
Convert between various representations of an IPv4 address.
`ip` refers to a dotted string, like: `'127.0.0.1'`.
`octets` are indexables, like `(127, 0, 0, 1)`.
`int` is the integer representation, like `2130706433`.
Written for Python 2.7.
"""
@ChrisTM
ChrisTM / block-grid.less
Last active August 25, 2022 19:53
Foundation 5's block grids for Bootstrap 3.
// # Block Grid
//
// Technique adapted from Foundation 5 for Bootstrap 3.0.3 to at least 3.3.1.
// https://github.com/zurb/foundation/blob/f755d8704123f86c281ede0b171881e2672f150d/scss/foundation/components/_block-grid.scss
//
// # Example Usage
//
// To produce a grid of 2 items per row on an extra-small screen, and 3 items
// per row on a small screen:
//
@ChrisTM
ChrisTM / throttle.py
Created June 21, 2013 21:33
Python decorator for throttling function calls.
class throttle(object):
"""
Decorator that prevents a function from being called more than once every
time period.
To create a function that cannot be called more than once a minute:
@throttle(minutes=1)
def my_fun():
pass
@ChrisTM
ChrisTM / gist:89b40995409318e3c52c
Created October 31, 2012 02:16
duration between two Date objects
/* Return a delta object whose days, hours, minutes, seconds property add up to
* the amount of time between timeA and timeB. Assumes A happens before B. */
var duration = function (timeA, timeB) {
var msPer = // milliseconds per various times
{ second: 1000
, minute: 1000 * 60
, hour: 1000 * 60 * 60
, day: 1000 * 60 * 60 * 24
};
var delta = {};
@ChrisTM
ChrisTM / cartesian-product.js
Last active October 10, 2015 12:47
N-Ary/Multiple-List Cartesian Product
/* N-ary cartesian product.
* Input: any number of lists
* Output: the cartesian product of the above lists as list of lists
* Example: cartesianProduct([1, 2], ['cat']) -> [ [1, 'cat'], [2, 'cat'] ]
*/
var cartesianProduct = function () {
function addNextList (tuples, list) {
var result = [];
tuples.forEach(function (tuple) {
list.forEach(function (item) {