Skip to content

Instantly share code, notes, and snippets.

Michael Dippery mdippery

View GitHub Profile
@mdippery
mdippery / singleton.py
Last active Jan 22, 2019
Creating a Singleton in Python
View singleton.py
class Singleton(type):
def __init__(cls, name, bases, dict):
super(Singleton, cls).__init__(name, bases, dict)
cls.instance = None
def __call__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls.instance
@mdippery
mdippery / byte.c
Created Oct 10, 2018
Read one byte at a time and print the value
View byte.c
#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
int g = 0;
unsigned char ch;
if (argc > 1) {
@mdippery
mdippery / trees.py
Last active Apr 29, 2016
Demonstrating depth-first search and breadth-first-search in Python
View trees.py
import random
class TreeNode(object):
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def __hash__(self):
@mdippery
mdippery / group.m
Created Jun 30, 2015
Get file's group name in Cocoa
View group.m
#import <Foundation/Foundation.h>
#include <grp.h>
#include <string.h>
#include <sys/stat.h>
@interface FileOwnerManager : NSObject
- (BOOL)isWheelPresent:(NSString *)path;
@end
@implementation FileOwnerManager
@mdippery
mdippery / Blood.hs
Created Feb 27, 2015
Blood pressure calculator
View Blood.hs
import System.Console.GetOpt (ArgOrder(..), getOpt)
import System.Environment (getArgs, getProgName)
data BloodPressureCategory = Normal
| Prehypertension
| Hypertension1
| Hypertension2
| HypertensiveCrisis
@mdippery
mdippery / Zodiac.hs
Last active Aug 29, 2015
Calculates Chinese zodiac sign for a given year
View Zodiac.hs
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 / evil_decorator.py
Created Nov 5, 2014
Python decorator that inserts variables into a function
View evil_decorator.py
import sys
from functools import wraps
from types import FunctionType
def is_python3():
return sys.version_info >= (3, 0)
def more_vars(**extras):
@mdippery
mdippery / scope.py
Created Aug 26, 2014
Python's scopes are weird
View scope.py
# 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 / mongo-groupby-date.js
Last active Aug 29, 2015
Group a set of MongoDB documents by day, finding the maximum value for each day
View mongo-groupby-date.js
db.samples.aggregate([ {$match: {user: 28804}}, {$group: {_id: {year: {$year: "$timestamp"}, month: {$month: "$timestamp"}, day: {$dayOfMonth: "$timestamp"}}, reputation: {$max: "$reputation"}}}])
View conv.py
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
You can’t perform that action at this time.