Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / remove_utf16.py
Created June 19, 2013 19:57
Remove all UTF-16 characters by regular expression
txt = u'\U0001f600'
r = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
r.sub('', txt)
@bcse
bcse / genpot.py
Last active December 21, 2015 19:58
Generate plexconnect.pot
#!/usr/bin/env python
import datetime
import glob
import os
import re
import sys
def main(base_path):
msgid = set()
@bcse
bcse / UnitBezier.py
Last active December 23, 2015 21:29
Python port of WebKit bezier solver
# Copyright (C) 2008 Apple Inc. All Rights Reserved.
# Copyright (C) 2013 Grey Lee. All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
@bcse
bcse / README.md
Last active December 24, 2015 05:19
iTunes Festival London 2014 Download Link Generator

Usage

  1. Execute iftgen.py
  2. Insert artist name, e.g. 5 Seconds of Summer
  3. 5 Seconds of Summer.txt and 5 Seconds of Summer.ac3.txt will be generated
  4. Download video from 5 Seconds of Summer.txt and combine them with cat *.ts > combined.ts
  5. (Optional) If you want to download 5.1 channel AC-3 audio as well. Download them from 5 Seconds of Summer.ac3.txt and combine them with cat *.ac3 > combined.ac3
@bcse
bcse / README.md
Last active December 25, 2015 21:59
Python OrderedSet

OrderedSet is a set-like class that also keeps inserting order like list.

Here are some test results compared with ABC-based OrderedSet:

Boost::MultiIndex-based
  My OrderedSet ABC-based
@bcse
bcse / .clang-format
Created March 21, 2014 03:46
My ClangFormat configuration
---
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignTrailingComments: false
ColumnLimit: 0
NamespaceIndentation: Inner
ObjCSpaceAfterProperty: true
IndentWidth: 4
BreakBeforeBraces: Stroustrup