Skip to content

Instantly share code, notes, and snippets.

@mittenchops
mittenchops / tabcompletion.py
Created July 11, 2013 20:21
getting tab completion in ordinary python prompt
import rlcompleter, readline
readline.parse_and_bind('tab: complete')
@mittenchops
mittenchops / flattenlist.py
Last active December 19, 2015 16:29
Flattens a nested list into a single list
# from http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python
def flatten(l):
for el in l:
if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
for sub in flatten(el):
yield sub
else:
yield el
# DEPRECATED METHOD THAT WORKS REALLY WELL!
@mittenchops
mittenchops / flattendict.py
Created July 12, 2013 14:03
flatten a dictionary of lists
# http://feldboris.alwaysdata.net/blog/python-trick-how-to-flatten-dictionaries-values-composed-of-iterables.html
from itertools import chain
def flatten_dict_values(dictionary):
return chain(*dictionary.values())
@mittenchops
mittenchops / multiprocess.py
Last active December 19, 2015 18:39
How to use multiprocessing
from multiprocessing import Pool
from functools import partial
x = [1,2,3]
y = 10
# NOTE, f CANNOT be a lambda function: http://stackoverflow.com/questions/17657571/python-multiprocessing-map-function-error
def f(x,y):
return(x**2+y)
# ordinary map works:
@mittenchops
mittenchops / findbytheword.sh
Created July 17, 2013 20:09
Find all files that contain a particular word, recursively in the current directory. This is the version that doesn't use xargs, which I think would look more like this: http://www.cyberciti.biz/faq/howto-recursively-search-all-files-for-words/
find . -type f -exec grep -l "wordofinterest" {} +
@mittenchops
mittenchops / dontrepeatpubkey.sh
Created July 22, 2013 18:24
How to not have to repeat your pubkey in unix. This weakens your security, so only use it on throwaway keys. You're generally better off just typing your password again.
$ eval "$(ssh-agent)"
$ ssh-add ~/.ssh/id_rsa
@mittenchops
mittenchops / tagger.py
Last active December 20, 2015 03:19
Regexy thing for matching tags out of strings. You define a thing, ARTS, which is just a list of what you want to match, then follow syntax below to return either a dict of counts or a list of things with nonzero counts. I'm actually pretty pleased with the performance of this that it keeps working fairly quickly even as the string size approach…
import re
ARTS = ['dance','photography','art therapy']
def string_found(string1, string2):
if re.search(r"\b" + re.escape(string1) + r"\b", string2):
return 1
return 0
def string_count(string1, string2):
@mittenchops
mittenchops / unique.py
Created July 23, 2013 18:42
Return a list of only unique elements.
def unique(x):
return(list(set(x)))
@mittenchops
mittenchops / listNkeys.py
Created July 30, 2013 20:41
This lets you get all the keys in a weirdly nested json document. Very useful for working with mongodb. Works like variety.js but at the item-level.
def listNkeys(obj, prefix=''):
"""
This lets you list ALL the keys of large nested dictionaries a la mongodb documents.
from: http://stackoverflow.com/questions/17952981/return-a-list-of-all-variable-names-in-a-python-nested-dict-json-document-in-dot
Usage:
>>> x = {
'a': 'meow',
'b': {
'c': 'asd'
@mittenchops
mittenchops / nonemptykeypct.py
Created July 30, 2013 20:45
Count percentage of keys in a weirdly nested json document. Very useful for working with mongodb. Works like variety.js but at the item-level.
from __future__ import division
from functools import partial
def nonemptykeypct(x):
"""
Returns the percentage of keys that have non-null values in an object (as decimal).
Usage:
>>> x = {'a': 'meow', 'b': {'c': 'asd'}, 'd': [{'e': 'stuff', 'f': 1}, {'e': 'more stuff', 'f': 2}], 'g': ''}
>>> nonemptykeypct(x)