{ Snipperize } /Python

Python snippets

Here are the latest Python snippets. Please choose your favorite one or add a new one.

Rsync Algorithm In Python

An implementation of the rsync algorithm in Python. As my rolling checksum, I just summed all of the ascii byte values in a given window. Even with this simple weak rolling checksum, computing and comparing the produced rolling checksums is still terribly slow. I'm fairly certain the speed could be reduced a fair amount but I haven't decided what the most efficient manner of doing this in Python alone would be.

Python / algorithm, delta, diff, rsync / by ThePeppersStudio (6 days, 17.97 hours ago)

Efficient Algorithm for computing a Running Median

Maintains sorted data as new elements are added and old one removed as a sliding window advances over a stream of data. Running time per median calculation is proportional to the square-root of the window size.

Python / algorithm, indexable, median, running, skiplist, statistics / by ThePeppersStudio (6 days, 18.08 hours ago)

Browser history data structure

The BrowserHistory class encapsulates the history of moving from location to location, as in Web browsing context; the recipe is not restricted to Web browsing though. See docstrings for more details and usage. The current implementation requires Python 2.6.

Python / browser, history, track, web / by ThePeppersStudio (29 days, 16.38 hours ago)

Advanced Directory Synchronization module

This program is an advanced directory synchronization and update tool. It can be used to update content between two directories, synchronize them, or just report the difference in content between them. It uses the syntax of the 'diff' program in printing the difference.

Python / robocopier, file, synchronization, diff, filecmp / by ThePeppersStudio (42 days, 19.90 hours ago)

The goto decorator

This is the recipe for you if you are sick of the slow speed of the existing goto module http://entrian.com/goto/. The goto in this recipe is about 60x faster and is also cleaner (abusing sys.settrace seems hardly pythonic). Because this is a decorator, it alerts the reader which functions use goto. It does not implement the comefrom command, although it is not difficult to extend it to do so (exercise for the reader). Also, computed gotos aren't supported; they're not pythonic. Use dis.dis(fn) to show the bytecode disassembly of a function. The bytecodes of a function are accessed by fn.func_code.co_code. This is readonly so: The decorated function is created exactly the same as the old one, but with the bytecode updated to obey the goto commands. This is 2.x only; the new module is not in python 3.x (another exercise for the reader!)

Python / bytecodes, decorator, goto, label / by ThePeppersStudio (52 days, 23.28 hours ago)

Sort List By Similar Group

This function takes a list of string and sorts them based on their similarity. The percent at which they match can be defined in the second parameter (default 75%). # Example: random = [ 'bob1', 'frank2', 'bob3', 'joe2', 'frank1', 'bob2', 'joe1', 'joe3' ] groups = sortByGroup(random) for g in groups: for i in g: print i print '-' * 30 # Example Output: bob1 bob2 bob3 ------------------------------ frank1 frank2 ------------------------------ joe1 joe2 joe3 ------------------------------

Python / sort, list, group, similarity / by ThePeppersStudio (62 days, 21.00 hours ago)

Check twitter friends who don't follow back

Check twitter friends who don't follow back

Python / twitter, api, simplejson / by ThePeppersStudio (70 days, 3.85 hours ago)

Autofollow and DM twitter using imap on gmail

Autofollow and DM twitter using imap on gmail

Python / gmail, imap, simplejson, twitter, api / by ThePeppersStudio (70 days, 3.88 hours ago)

get mysql run info

get mysql running status

Python / mysql, status / by ThePeppersStudio (70 days, 4.33 hours ago)

Relative path from A to B

>>> base_path_relative("/usr/bin", "/tmp/foo/bar") ../../tmp/foo/bar >>> base_path_relative("/usr/bin", "/usr/lib") ../lib >>> base_path_relative("/tmp", "/tmp/foo/bar") foo/bar

Python / relative, path, getcwd / by ThePeppersStudio (70 days, 4.37 hours ago)