Skip to content

Instantly share code, notes, and snippets.

@bcse
bcse / request.py
Created March 18, 2013 02:20
Extending urllib2.Request, so we can assign HTTP verb manually.
from urllib2 import Request as RequestBase
class Request(RequestBase):
def __init__(self, url, data=None, headers=None, origin_req_host=None,
unverifiable=False, method=None):
if headers is None:
headers = {}
super(Request, self).__init__(url, data, headers, origin_req_host, unverifiable)
self.method = method
@bcse
bcse / geodesy.py
Last active December 14, 2015 20:08
from math import fmod, modf, sqrt, degrees, radians, sin, cos, atan2
# Mean Earth radius defined by IUGG. (Unit: meters)
# ref. https://en.wikipedia.org/wiki/Earth_radius#Mean_radius
EARTH_RADIUS = 6371009.
def distance((lat1, lng1), (lat2, lng2)):
"""Get approximate geographical distance between 2 coordinates in meters.
@bcse
bcse / README.markdown
Last active October 7, 2015 02:38
Python SortedList

SortedList is a list-like class that always keeps items sorted. heaplist.SortedList is faster than bisectlist.SortedList but with lesser functions.

Here are some test results:

bisectlist.SortedList

Test with 10000 random numbers...
Raw list takes 1.01116704941 seconds
Raw list, sort once takes 0.00338888168335 seconds
@bcse
bcse / locate.py
Created April 10, 2012 07:10
Recursively list files
import os, fnmatch
def locate(pattern, root=os.curdir):
'''Locate all files matching supplied filename pattern in and below
supplied root directory.'''
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
if __name__ == '__main__':
registry = {}
class MultiMethod(object):
def __init__(self, name):
self.name = name
self.typemap = {}
def __call__(self, *args):
types = tuple(arg.__class__ for arg in args) # a generator expression!
function = self.typemap.get(types)
if function is None:
@bcse
bcse / fetch-by-tag.py
Last active September 27, 2015 03:38
Fetch top 100 popular photos from Flickr
import re
import json
import os
from urllib.request import urlopen, urlretrieve
from glob import glob
config = {
'api_key': 'YOUR_API_KEY',
'sort': 'interestingness-desc',
'per_page': 100
@bcse
bcse / plurk-smiley-notifier.py
Created September 8, 2011 04:01
Automatic post latest smiley on your timeline (Plurk API 1.0)
#!/usr/bin/env python
APIKEY = 'YOUR_APIKEY'
USERNAME = 'YOUR_USERNAME'
PASSWORD = 'YOUR_PASSWORD'
#--- Setup ---------------------------------------------------
import sys, os, urllib, urllib2, cookielib
try:
import json
except:
@bcse
bcse / dbbackup.php
Created September 8, 2011 03:47
Backup all MySQL databases and send to my E-mail
<?php
$DBUSERNAME = 'YOUR_USERNAME';
$DBPASSWORD = 'YOUR_PASSWORD';
$EMAIL = 'YOUR_EMAIL';
$boundary = '==DBBACKUP-'.md5(uniqid(rand(), true));
$date = date('Ymd');
$header = "Content-Type: multipart/mixed; boundary=\"$boundary\"";
$message =
"This is a multi-part message in MIME format.\n".
@bcse
bcse / greyscale2alpha.py
Created March 8, 2011 18:39
Convert black to opaque and white to transparent. (Depends on pypng)
import png
def convert(fin, fout):
reader = png.Reader(filename=fin)
width, height, pixels, metadata = reader.asDirect()
#print metadata
out = list()
for row in pixels:
row_out = list()
@bcse
bcse / zoinkfic.py
Created January 29, 2011 21:55
streamlined zoink.it + torrific (Depends on suds and Python 2.5 or higher)
from os.path import expanduser, join
from base64 import b64encode
from glob import glob
import webbrowser
from suds.client import Client
url = 'https://zoink.it/api/torrage.wsdl'
client = Client(url)
files = glob(join(expanduser('~'), 'Downloads', '*.torrent'))