Skip to content

Instantly share code, notes, and snippets.

View mikelikespie's full-sized avatar

Mike Lewis mikelikespie

View GitHub Profile
@mikelikespie
mikelikespie / NSArray+enumerableStuff.h
Created January 15, 2011 21:50
Some fun stuff for enumerating
@interface NSArray (EnumerableStuff)
/**
* similar to a map
*/
- (NSArray *) objectsTransformedByBlock:(id (^)(id, NSUInteger, BOOL *))block;
/**
* filter the array
*/
@interface NSArray (EnumerableExtras)
/**
* similar to a map
*/
- (NSArray *) objectsTransformedByBlock:(id (^)(id obj, NSUInteger idx, BOOL *stop))block;
/**
* filter the array
*/
@mikelikespie
mikelikespie / NSIndexPath+Hax.m
Created February 10, 2011 18:29
UITableView performance hax
#import "NSIndexPath+RowSectinCache.h"
@interface PairIndexPath : NSObject {
@private
NSUInteger indexes[2];
}
- (NSUInteger)length;
@mikelikespie
mikelikespie / graceful_http_server.py
Created March 4, 2011 22:19
Graceful tornado http server shutdown
class MyHTTPServer(tornado.httpserver.HTTPServer):
_stopped = False
def _quit_if_ioloop_is_empty(self):
ioloop = tornado.ioloop.IOLoop.instance()
if len(ioloop._handlers) <= 1:
logger.info("Graceful shutdown complete. Exiting!")
exit(0)
else:
logger.info("Waiting for ioloop to be empty. has %d handlers left" % len(ioloop._handlers))
@mikelikespie
mikelikespie / redis_hot_standby.py
Created March 10, 2011 00:50
A subclass of Redis client redis-py that will deal with hot standbys. Each transaction should WATCH MASSTER_SERVER_KEY and if an exception is raised it should trigger _determine_master
import redis
import time
from contextlib import contextmanager
class _LockFailedError(Exception): pass
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
@mikelikespie
mikelikespie / memoizer.m
Created April 2, 2011 19:08
Fun way to memoize with blocks. It would be sweet if objc had decorators.
#define _MEMO(iVarName, block) ^{if (!iVarName) {iVarName = (block)();} return iVarName;}
- (UIButton *)eatCheeseButton;
{
return _MEMO(eatCheeseButton, ^{
return [[UIButton alloc] init];
})();
}
@mikelikespie
mikelikespie / pipes-client.py
Created April 19, 2011 08:18
Lazy Pipeline Wrapper for redis-py
import redis
from cStringIO import StringIO
import weakref
import contextlib
from collections import deque
def main():
c = PipesClient('localhost')
@mikelikespie
mikelikespie / CCBlockTableViewDataSource.h
Created May 4, 2011 18:55 — forked from omnivector/CCBlockTableViewDataSource.h
Block based data source / delegate
//
// CCBlockTableViewDataSource.h
//
// Created by Tristan O'Tierney on 1/24/11.
//
#import <UIKit/UIKit.h>
@interface CCBlockTableViewDataSource : NSObject <UITableViewDataSource, UITableViewDelegate> {
@mikelikespie
mikelikespie / parseobjcsig.py
Created August 18, 2011 20:23
Parsing and formatting Objective C signatures
# -*- coding: utf-8 -*-
"""
sphinx.domains.objc
~~~~~~~~~~~~~~~~~~~
The Objective C domain.
"""
import re
@mikelikespie
mikelikespie / PGGeometry.hh
Created September 25, 2011 20:28
Wrappers to overload operations with CGPoint, CGSize, and CGRect.
//
// PGGeometry.hh
// PonyGrid
//
// Created by Mike Lewis on 9/23/11.
// Copyright (c) 2011 Square, Inc. All rights reserved.
//
#ifndef PonyGrid_PGGeometry_hh
#define PonyGrid_PGGeometry_hh