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 / 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"}}}])
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
#import <Foundation/Foundation.h>
int main (int argc, char const *argv[])
{
@autoreleasepool {
id obj = [[nil alloc] init];
NSLog(@"%@ (%p)", obj, obj);
[obj release];
}
#import <Foundation/Foundation.h>
@interface NSString (Shift)
- (NSString *)shiftRight:(NSUInteger)places;
@end
@implementation NSString (Shift)
@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
##
##
@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 / 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 / .bash_logout
Created November 10, 2013 00:10
Removes the crap that SSH puts in your console title bar when SSH'ing into a machine. Drop this in the ~/.bash_logout file on the remote machine.
# ~/.bash_logout
printf '\e]0;\a'
def ensure_subscription(fn):
@wraps(fn)
def _ensure_subscription(user, other, trade):
unsubscribe_code = user.profile.get_unsubscribe_code('trade-notifications')
if unsubscribe_code is None:
return
fn_globals = {}
fn_globals.update(globals())
fn_globals['unsubscribe_code'] = unsubscribe_code
call_fn = FunctionType(fn.func_code, fn_globals)
@mdippery
mdippery / FizzBuzz.hs
Last active December 16, 2015 21:20
FizzBuzz in Haskell
fizzBuzz n
| n `rem` 5 == 0 && n `rem` 3 == 0 = "FizzBuzz"
| n `rem` 5 == 0 = "Buzz"
| n `rem` 3 == 0 = "Fizz"
| otherwise = show n
main = putStr $ unlines $ map fizzBuzz [1..100]