Skip to content

Instantly share code, notes, and snippets.

View durden's full-sized avatar

Luke Lee durden

View GitHub Profile
@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 / article.md
Last active October 26, 2015 09:06
Python versioning

Python spelunking

Motivation

I recently spent a little time reviewing a Python bug report and determining if I was running with a fixed version of the Python interpreter. I think this is a useful exercise for someone who is overly curious and not a core developer of the language.

This post is a rundown of my thought process while trying to figure this out.

@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 / 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
@durden
durden / tricks.py
Created March 8, 2013 22:27
Funny python 'tricks'
# Turn list of rows into columns
>>> x = [(1,2,3), (4,5,6)]
>>> zip(*x)
[(1, 4), (2, 5), (3, 6)]
# Reverse key-value mapping in a dict
>>> x = {'a': 1, 'b': 2}
>>> dict([reversed(kv) for kv in x.iteritems()])
" Searches Dash for the word under your cursor in vim, using the keyword
" operator, based on file type. E.g. for JavaScript files, I have it
" configured to search j:term, which immediately brings up the JS doc
" for that keyword. Might need some customisation for your own keywords!
function! SearchDash()
" Some setup
let s:browser = "/usr/bin/open"
let s:wordUnderCursor = expand("<cword>")