Skip to content

Instantly share code, notes, and snippets.

@liyanage
liyanage / .gitconfig
Created August 16, 2010 17:51
.gitconfig
[core]
excludesfile = /Users/liyanage/.gitignore.global
[log]
date = local
[user]
name = Marc Liyanage
email = foo@example.com
[github]
user = liyanage
token = xxxxx
@liyanage
liyanage / AppDelegate.h
Created September 23, 2012 22:26
retain/autorelease demo
//
// AppDelegate.h
// ThreadTest
//
// Created by Marc Liyanage on 9/23/12.
//
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate> {
@liyanage
liyanage / gist:4069272
Created November 14, 2012 00:03
Python cmd.Cmd completion workaround
# OS X-specific fix based on http://bugs.python.org/issue9033
readline.parse_and_bind('bind {} rl_complete'.format('^I'))
@liyanage
liyanage / gist:4102191
Created November 18, 2012 00:54
Example for a mini web app that keeps its state in a custom class instance
#!/usr/bin/env python
# Example for a mini web app that keeps its state in a custom class instance
import bottle
class Foo:
def main(self, value=0):
self.sum += value
@liyanage
liyanage / email-test.py
Created November 18, 2012 02:00
Python email sending example
#!/usr/bin/env python
import email.mime.text
import email.header
import smtplib
email.Charset.add_charset('utf-8', email.Charset.QP, email.Charset.QP, 'utf-8')
me = u'Foo Bar <test@example.com>'
you = me
@liyanage
liyanage / repr-fallback.py
Created December 1, 2012 02:13
__repr__ implementation handling non-ascii characters.
class Foo:
def __unicode__(self):
return u'xx \u2192 xx'
def __repr__(self):
return unicode(self).encode('ascii', 'backslashreplace')
foo = Foo()
@liyanage
liyanage / copy_items.py
Created December 14, 2012 07:18
Copy files or directories
import shutil
import os
def copy_item(source_path, destination_parent):
if os.path.isdir(source_path):
destination_path = os.path.join(destination_parent, os.path.basename(source_path))
shutil.copytree(source_path, destination_path, ignore=shutil.ignore_patterns('.DS_Store', '*.pyc'))
else:
shutil.copy(source_path, destination_parent)
@liyanage
liyanage / gist:4627093
Created January 24, 2013 20:00
Bookmarklet to clean up clutter on infoq.com video viewing web pages.
javascript:$('#rightbar,%20.vendor-content-box,%20#relatedContent,%20#problemsVideo,%20#relatedResearchWidget,%20#header,%20#footer,%20#textlinks').remove()
def merge(left, right):
nl = len(left)
nr = len(right)
merged = []
il = 0
ir = 0
while il < nl and ir < nr:
l = left[il]
r = right[ir]
@liyanage
liyanage / ruby-cheat-sheet.md
Created September 2, 2013 06:52
Ruby cheat sheet

Retry helper

def with_retry(retry_limit=3)
  exception = nil
  retry_limit.times do |i|
    begin
      return yield()
    rescue Exception => ex

print "Attempt #{i + 1} of #{retry_limit} failed: #{ex}\n"