Skip to content

Instantly share code, notes, and snippets.

@adeel
adeel / gist:947223
Created April 28, 2011 20:15
A subclass of dict that looks for attributes if the key lookup fails.
class MyDict(dict):
def get(self, key):
return self.get_key_or_attr(key)
def __getitem__(self, key):
return self.get_key_or_attr(key)
def get_key_or_attr(self, key):
if self.has_key(key):
return dict.__getitem__(self, key)
def iterate_in_steps(seq, n, increment):
if len(seq) >= n:
return [tuple(seq[:n])] + iterate_in_steps(seq[increment:], n, increment)
else:
return []
# >>> iterate_in_steps([1, 2, 3, 4, 5], 2, 1)
# [(1, 2), (2, 3), (3, 4), (4, 5)]
#
# >>> iterate_in_steps([1, 2, 3, 4, 5], 3, 2)
@adeel
adeel / UILabel+withString.h
Created July 21, 2011 22:36
UILabel category that adds a method to easily make labels on the fly.
//
// UILabel+withString.h
//
#import <Foundation/Foundation.h>
@interface UILabel (withString)
+ (UILabel *)labelWithString:(NSString *)string
font:(UIFont *)font
@adeel
adeel / push-notifications.clj
Created July 26, 2011 01:03
Example of sending Apple push notifications with Clojure.
; Depends on [com.notnoop.apns/apns "0.1.6"].
(:import ('com.notnoop.apns APNS))
(defn send-push-notification [device-token message]
(let [service (.build (.withSandboxDestination
(.withCert (APNS/newService) "resources/apns-dev-cert.p12" "password")))
payload (.build (.alertBody (APNS/newPayload) message))]
(.push service device-token payload)))
@adeel
adeel / UITextField+withFrame.h
Created July 28, 2011 19:00
UITextField category for conveniently adding text fields inside frames with padding.
//
// UITextField+withFrame.h
//
#import <Foundation/Foundation.h>
@interface UITextField (withFrame)
+ (UITextField *)textFieldWithFrame:(CGRect)frame
@adeel
adeel / gist:1164481
Created August 23, 2011 06:30
Example: Heroku + Node.js + Express.js + connect-mongodb
var mongo = require('mongodb'),
mongoStore = require('connect-mongodb');
var mongo_config = {
host: "staff.mongohq.com",
port: 10004,
dbname: "appXXX",
username: "username",
password: "password"};
@adeel
adeel / gist:1240589
Created September 25, 2011 13:11
Null-route Facebook netblocks
#!/usr/bin/env python2
# http://bgp.he.net/AS32934#_prefixes
import os
netblocks = [
"31.13.24.0/21",
"66.220.144.0/21",
"66.220.152.0/21",
"69.63.176.0/21",
@adeel
adeel / veneer.css
Created November 16, 2011 00:24
veneer: a lightbox (uses MooTools)
.veneer_overlay {
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
background-color: black;
z-index: 1001;
-moz-opacity: 0.5;
opacity: 0.5;
var fun = (function() {
var self = {};
self.element = (function() {
var self = {};
self.events = (function() {
// Based on http://dean.edwards.name/my/events.js.
var self = {};