Skip to content

Instantly share code, notes, and snippets.

@mittenchops
mittenchops / inkscapeSVG2PNGafolder.sh
Created August 7, 2013 22:01
For each SVG in a directory, makes a faithful PNG copy.
for i in *.svg; do inkscape -f "$i" -e "$i.png"; done
@mittenchops
mittenchops / addweeks.py
Created August 5, 2013 18:54
Add weeks to time in MMM DDth YYYY format.
from dateutil import parser
from datetime import timedelta
date = "Mar 11th 2013"
def addweeks(date,week=0):
x = parser.parse(date)
new = x + timedelta(weeks=week)
return(new.strftime("%b %d %Y"))
@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)
@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 / unique.py
Created July 23, 2013 18:42
Return a list of only unique elements.
def unique(x):
return(list(set(x)))
@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 / 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 / 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 / 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 / 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())