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__':
@bcse
bcse / scrdec18-VC8.exe
Created February 15, 2012 10:13
Windows Script Decoder 1.8 (Decoding JScript.Encoded)
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 / build-ffmpeg.sh
Created August 22, 2011 17:01
Compile FFmpeg/Libav for iOS
# configure for armv7 build
./configure \
--cc=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc \
--as='/usr/local/bin/gas-preprocessor.pl /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc' \
--sysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk \
--target-os=darwin \
--arch=arm \
--cpu=cortex-a8 \
--extra-cflags='-arch armv7' \
--extra-ldflags='-arch armv7 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk' \