Skip to content

Instantly share code, notes, and snippets.

View durden's full-sized avatar

Luke Lee durden

View GitHub Profile
@durden
durden / fuzzy_float_dict.py
Created December 7, 2012 21:09
Compare dicts with fuzzy float comparison
def fuzzyDictEqual(d1, d2, precision):
"""
Compare two dicts recursively (just as standard '==' except floating point
values are compared within given precision.
"""
if len(d1) != len(d2):
return False
for k, v in d1.iteritems():
@durden
durden / dump_stats.py
Created December 12, 2012 23:02
Dump Python pstats file to stdout sorted by cumulative stat
if __name__ == "__main__":
import sys
import pstats
stats = pstats.Stats(sys.argv[1])
stats.strip_dirs()
stats.sort_stats('cumulative')
stats.print_stats()
@durden
durden / dont_use_this_decorator.py
Created December 14, 2012 14:47
Python gives you a lot of power, like the ability to write code that will drive your static language programmer friends crazy...
def call_only_once(func):
def new_func(*args, **kwargs):
if not new_func._called:
try:
return func(*args, **kwargs)
finally:
new_func._called = True
else:
# We already called the real func once, so let's pick a completely
# different one from the globals and call it instead
@durden
durden / overriding_generator_method_oop.py
Created January 7, 2013 20:31
Little experiment with ways to override generator method and the caveats associated with it in Python.
class base(object):
def gen(self):
for ii in [1,2,3]:
yield ii
class child(base):
def gen(self):
for ii in [4,5,6]:
yield ii
@durden
durden / sanitize.py
Last active April 26, 2023 11:13
Different ways to 'sanitize' a list of strings to ensure they don't contain any of the contents of another 'ignored' list
"""
Demonstration of ways to implement this API:
sanitize(unsanitized_input, ignored_words)
Related discussions:
- Modifying a list while looping over it:
- http://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating-in-python
- Remove all occurences of a value in a list:
- http://stackoverflow.com/questions/1157106/remove-all-occurences-of-a-value-from-a-python-list
@durden
durden / enable_man_page_colors.sh
Created January 10, 2013 19:14
Enable colors for viewing man pages in the terminal
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking-mode
export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold-mode
export LESS_TERMCAP_me=$'\E[0m' # end (blinking/bold)-mode
export LESS_TERMCAP_so=$'\E[38;5;246m' # begin standout-mode - info box
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline
export LESS_TERMCAP_ue=$'\E[0m' # end underline
# Copied from: http://www.quora.com/Unix/What-are-some-hacks-when-reading-man-pages/answer/Igor-Pozgaj?srid=5axS&st=ns
@durden
durden / compare_lists_without_ordering.py
Last active December 10, 2015 23:55
Snippet of timing/brainstorming multiple solutions to compare two lists without worrying about ordering
# See the following for a discussion I later found on stackoverflow, should
# have searched there first ;)
# http://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches
from timeit import timeit
# Different ways to implement a function that compares lists without
# taking into account their order
compare_lists_without_order = lambda x, y: sorted(x) == sorted(y)
compare_lists_without_order = lambda x, y: True if not len(set(x) - set(y)) else False
# YOU NEED TO INSERT YOUR APP KEY AND SECRET BELOW!
# Go to dropbox.com/developers/apps to create an app.
app_key = 'YOUR_APP_KEY'
app_secret = 'YOUR_APP_SECRET'
# access_type can be 'app_folder' or 'dropbox', depending on
# how you registered your app.
access_type = 'app_folder'
@durden
durden / import_searcher.py
Last active December 12, 2015 05:38
Hack attempt at building something that searches module for a given term to determine where to import it from.
#!/usr/bin/env python
import argparse
import inspect
import logging
import pydoc
import pkgutil
import sys
log = logging.getLogger("import_searcher")
@durden
durden / qt_memory_usage.py
Created February 14, 2013 21:58
Script to see the memory usage of each individual PyQt4 module.
"""
Script to see the memory usage of each individual PyQt4 module.
Meant to be run with the memory_profiler:
- http://pypi.python.org/pypi/memory_profiler
Usage: python -m memory_profiler qt_memory_usage.py
"""
@profile