Skip to content

Instantly share code, notes, and snippets.

View kennethreitz's full-sized avatar
🌙

Kenneth Reitz kennethreitz

🌙
View GitHub Profile
import time
class Retry(object):
default_exceptions = (Exception)
def __init__(self, tries, exceptions=None, delay=0):
"""
Decorator for retrying function if exception occurs
tries -- num tries
exceptions -- exceptions to catch
@coderanger
coderanger / tree.py
Created July 8, 2010 04:35
A simple function to print a tree in response to a reddit question.
from StringIO import StringIO
tree1 = ("Root", ("Child 1", ("Child 2", None)))
get_label1 = lambda x: x[0]
def get_children1(node):
if node[1] is not None:
yield node[1]
class Node(object):
@bobthecow
bobthecow / .gitconfig
Created July 6, 2010 00:56
`git todo` alias is the hotness.
[alias]
# install t first: http://github.com/sjl/t
todo = !python ~/path/to/t.py --task-dir "$(git rev-parse --show-toplevel)" --list TODO
bug = !python ~/path/to/t.py --task-dir "$(git rev-parse --show-toplevel)" --list BUGS
@kennethreitz
kennethreitz / helpers.py
Created June 24, 2010 18:38
Various Python helper functions
# encoding: utf-8
""" Python General Helpers
Copyright (c) 2010 Kenneth Reitz. Creative Commons Attribution 3.0 License.
"""
import urllib, re, time, sys
import paramiko
@kennethreitz
kennethreitz / console_progress.py
Created June 23, 2010 21:35
Awesome iterator-based CLI progress bar for Python.
import sys
import time
def progressbar(it, prefix = "", size = 60):
count = len(it)
def _show(_i):
x = int(size*_i/count)
sys.stdout.write("%s[%s>%s] %i/%i\r" % (prefix, "="*x, "-"*(size-x), _i, count))
sys.stdout.flush()
@brantfaircloth
brantfaircloth / py2app.bash
Created June 14, 2010 17:10
Steps to get a working py2app
## Roll your own python from source:
wget http://www.python.org/ftp/python/2.6.5/Python-2.6.5.tgz
tar -xzvf Python-2.6.5.tgz && cd Python-2.6.*
./configure --enable-framework=/Library/Frameworks --with-universal-archs=intel --enable-universalsdk=/
make
sudo make install
# Update the symlink for `python` (and check others)
merge.tool=chdiff
mergetool.chdiff.cmd=/usr/bin/env chdiff --wait $LOCAL $REMOTE
mergetool.chdiff.keepbackup=false
mergetool.chdiff.trustexitcode=false
diff.tool=Kaleidoscope
difftool.Kaleidoscope.cmd=ksdiff-wrapper git "$LOCAL" "$REMOTE"
difftool.prompt=false
#!/usr/bin/env python
"""xml2json.py Convert XML to JSON
Relies on ElementTree for the XML parsing. This is based on
pesterfish.py but uses a different XML->JSON mapping.
The XML->JSON mapping is described at
http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html
@kennethreitz
kennethreitz / gist:432002
Created June 9, 2010 19:08
Pushing the limits.
Hardware:
Hardware Overview:
Model Name: MacBook Pro
Model Identifier: MacBookPro6,2
Processor Name: Intel Core i7
Processor Speed: 2.66 GHz
Number Of Processors: 1
Total Number Of Cores: 2
@kennethreitz
kennethreitz / backup.php
Created April 5, 2010 07:27
mediatemple-to-s3.php
#!/usr/bin/env php
<?php
set_time_limit(0);
$nDays = 300; // How many days of backups to keep?
// MySQL variables
$bBackupMySQL = true; // Do you want to backup MySQL databases?
$mysql = '/usr/bin/mysql';
$mysqldump = '/usr/bin/mysqldump';
$mysql_server = 'xxxx.com';
$mysql_username = 'xxxx';