Skip to content

Instantly share code, notes, and snippets.

View mdippery's full-sized avatar
💭
Have you seen an older me go by in a time machine?

Michael Dippery mdippery

💭
Have you seen an older me go by in a time machine?
View GitHub Profile
@mdippery
mdippery / random_subclass.py
Created February 1, 2014 01:31
Random subclass, Python style
#!/usr/bin/env python
import random
cls = random.choice([list, dict, str, int, float])
class RandomSubclass(cls):
def __str__(self):
return "MRO: %s" % (RandomSubclass.__mro__,)
@mdippery
mdippery / brew-less.rb
Last active August 29, 2015 13:56
Like `brew cat`, but automatically displays the output in a pager. Drop it in a directory on your $PATH and chmod +x it. Now you can run `brew less <formula>`!
# Thanks to <http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby>
def run_pager
return if PLATFORM =~ /win32/
return unless STDOUT.tty?
read, write = IO.pipe
unless Kernel.fork
STDOUT.reopen(write)
STDERR.reopen(write) if STDERR.tty?
@mdippery
mdippery / py3-ready
Last active August 29, 2015 13:57
Quick script to determine if a set of Python packages work with Python 3
#!/usr/bin/env python
## Accepts a list of packages on stdin and prints whether each
## package is compatible with Python 3. The easiest way to
## use this script is by piping the output of `pip freeze`
## into it:
##
## $ pip freeze | py3-ready
##
##
#import <Foundation/Foundation.h>
@interface NSString (Shift)
- (NSString *)shiftRight:(NSUInteger)places;
@end
@implementation NSString (Shift)
#import <Foundation/Foundation.h>
int main (int argc, char const *argv[])
{
@autoreleasepool {
id obj = [[nil alloc] init];
NSLog(@"%@ (%p)", obj, obj);
[obj release];
}
actual = [['0:00.762', '0:01.435'], ['2:01.374', '2:07.423'], ['3:01.412', '3:07.314']]
expected = [['0.762', '1.435'], ['121.374', '127.423'], ['181.412', '187.314']]
def convert(s):
mins, secs = s.split(':')
mins = int(mins)
secs = float(secs)
secs = 60 * mins + secs
return secs
@mdippery
mdippery / mongo-groupby-date.js
Last active August 29, 2015 14:01
Group a set of MongoDB documents by day, finding the maximum value for each day
db.samples.aggregate([ {$match: {user: 28804}}, {$group: {_id: {year: {$year: "$timestamp"}, month: {$month: "$timestamp"}, day: {$dayOfMonth: "$timestamp"}}, reputation: {$max: "$reputation"}}}])
@mdippery
mdippery / scope.py
Created August 26, 2014 22:54
Python's scopes are weird
# This totally works, even though `i` isn't passed into the
# lambda, nor is it defined prior to the lambda declaration.
calculate_efp = lambda v: v - i
[calculate_efp(v) for i, v in enumerate([10,100,1000])]
@mdippery
mdippery / Zodiac.hs
Last active August 29, 2015 14:16
Calculates Chinese zodiac sign for a given year
import System.Console.GetOpt (ArgOrder(..), getOpt)
import System.Environment (getArgs)
offset = (flip mod) 12
animal 4 = "Rat"
animal 5 = "Ox"
animal 6 = "Tiger"
animal 7 = "Rabbit"
animal 8 = "Dragon"
@mdippery
mdippery / Blood.hs
Created February 27, 2015 00:47
Blood pressure calculator
import System.Console.GetOpt (ArgOrder(..), getOpt)
import System.Environment (getArgs, getProgName)
data BloodPressureCategory = Normal
| Prehypertension
| Hypertension1
| Hypertension2
| HypertensiveCrisis