Skip to content

Instantly share code, notes, and snippets.

@mittenchops
mittenchops / loadshp2mongo.py
Last active April 9, 2019 06:13
Loading shapefiles into mongodb. This all actually comes from here: http://geospatialpython.com/ But this is wrapped up a little nicer.
# http://geospatialpython.com/
import json, pymongo, shapefile
from json import dumps
database = 'geo'
connection = pymongo.MongoClient()
db = getattr(connection,database)
def renamesh2json(x):
d = x.split(".")
@mittenchops
mittenchops / add_pin_unpin.go
Created June 11, 2018 06:07 — forked from leerspace/add_pin_unpin.go
example of adding, pinning, and unpinning using IPFS as library
package main
import (
"context"
"log"
"net"
core "github.com/ipfs/go-ipfs/core"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
@mittenchops
mittenchops / to_acronym.sh
Last active January 4, 2016 01:58
sed to turn text to acronyms
# from http://ask.metafilter.com/255675/Decoding-cancer-addled-ramblings
sed 's/\B\w*//g;s/\s//g' full_file.txt > acronym_file.txt
@mittenchops
mittenchops / emacs.txt
Last active January 2, 2016 00:59
emacs review!
# navigation
C-a # beginning of line
C-e # end of line
M-> # eof
M-< # head of file
M-g g 100 # goto line 100
# general
C-h f thing-name # describe a function named thing-name
M-x load-library RET icicles RET
@mittenchops
mittenchops / defaultdictionaries.py
Created December 20, 2013 17:04
Function defaults done correctly in python
"""
# In addition, the use of mutable objects as default values may lead to unintended behavior:
def foo(x, items=[]):
items.append(x)
return items
foo(1) # returns [1]
foo(2) # returns [1, 2]
foo(3) # returns [1, 2, 3]
@mittenchops
mittenchops / phoneparser.py
Created October 11, 2013 20:13
Useful phone number parser. Clean to USA!
# all straight from here:
# https://github.com/daviddrysdale/python-phonenumbers
import phonenumbers
def numberizer(num):
z = phonenumbers.parse(num,"US")
return(phonenumbers.format_number(z,phonenumbers.PhoneNumberFormat.NATIONAL))
@mittenchops
mittenchops / argmax.py
Last active December 25, 2015 07:19
argmax function: return the entry from list X where the argument arg is at a maximum.
def argmax(arg, X):
"""
>>> zee = [{"cool":{"stuff":1,"things":0.5}},{"cool":{"stuff":2,"things":0.25}}]
>>> argmax("cool.stuff",zee)
{'cool': {'things': 0.25, 'stuff': 2}}
>>> argmax("cool.things",zee)
{'cool': {'things': 0.5, 'stuff': 1}}
"""
ranked = sorted(X, key=lambda x: -getByDot(x,arg))
leader = filter(lambda x: getByDot(x,arg) == reduce(max,[getByDot(r, arg) for r in ranked]),ranked)[0]
@mittenchops
mittenchops / unicodesolved.py
Created October 9, 2013 19:47
encoding fixed everywhere. Start every file with this to ensure that both interactive and script-mode always work.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
######################################
#### THIS IS IMPORTANT EVERYWHERE ####
import sys #
reload(sys) #
sys.setdefaultencoding("UTF-8") #
#sys.getdefaultencoding() #
######################################
@mittenchops
mittenchops / differentrepocommitmessage.sh
Created October 9, 2013 18:26
Quote a github issue in a different repo
$ git commit
mittenchops/differentreponame#issuenumberindifferentreponame
@mittenchops
mittenchops / fixgit.sh
Created October 7, 2013 16:49
How to fix a boneheaded commit message in git.
# git commit --amend
# git push --force # <-- WARNING, HOPEFULLY YOU DON'T HAVE TO DO THIS PART.