Skip to content

Instantly share code, notes, and snippets.

@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 / 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
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 / 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)
@adeel
adeel / The middleware that could have been
Created April 15, 2011 13:53
pack/middleware/session.py
from pack.middleware.cookies import *
def wrap_session(app, options={}):
store = options.get("store", "memory_store")
cookie_name = options.get("cookie_name", "pack-session")
session_root = options.get("root", "/")
cookie_attrs = dict(options.get("cookie_attrs", {}),
**{"path": session_root})
def wrapped_app(req):
@adeel
adeel / guillemets.tex
Created February 15, 2011 17:36
LaTeX + guillemets
% This is necessary to use, for example, guillemets («, ») in your LaTeX documents.
\usepackage[utf8]{inputenc}
\usepackage[T2A]{fontenc}
@adeel
adeel / wdiff-html
Created February 12, 2011 02:13
How to get a word-by-word diff of two text files, and format it as html
#!/bin/sh
FILE1=$1
FILE2=$2
# http://www.gnu.org/software/wdiff/
wdiff -w "<span style='color:red; text-decoration: line-through;'>" \
-x "</span>" \
-y "<span style='color:green'>" \
-z "</span>" \
@adeel
adeel / recursive_dict_update.py
Created December 29, 2010 07:03
Like dict.update, but recursive.
def _recursive_dict_update(x, y):
for key, val in y.iteritems():
if isinstance(val, dict):
if not x.has_key(key):
x[key] = {}
x[key] = _recursive_dict_update(x[key], val)
elif isinstance(val, list):
if not x.has_key(key):
x[key] = []
x[key].extend(val)
# How to get the password hint for lib.homelinux.org
import httplib
conn = httplib.HTTPConnection("lib.homelinux.org")
conn.request("HEAD", "/")
print conn.getresponse().getheaders()[3][1].decode("cp1251")