Skip to content

Instantly share code, notes, and snippets.

@davidandrzej
davidandrzej / pandas-csv.py
Created October 25, 2012 23:30
"vertically" concat a bunch of csv files in Pandas
from pandas import *
import dateutil
import os, os.path
csvdir = os.getcwd()
filenames = [os.path.join(csvdir,csvfile)
for csvfile in os.listdir(csvdir)
if csvfile[-4:] == '.csv']
dfs = [io.parsers.read_csv(filename,
converters={0:dateutil.parser.parse})
@davidandrzej
davidandrzej / protocol-record.clj
Created October 25, 2012 23:22
Simple protocol / record example
(defprotocol Dateable
"Convertible to a Java Date"
(to-date [this] "Convert to Java Date"))
(defrecord TimeMs [timeInMs]
Dateable
(to-date [this] (new Date (long (:timeInMs this)))))
(defrecord TimeOffset [offsetString]
Dateable
@davidandrzej
davidandrzej / nesting.py
Created August 1, 2012 04:52
Find unexpected directory nesting patterns
"""
Script to find folders with bad nesting patterns
(eg, in an arist-album organized music collection)
"""
import os,os.path,sys,shutil,pdb
colroot = sys.argv[1]
skiplist = ['desktop.ini','.DS_Store','Thumbs.db','FINDER.DAT']
log_f = open('nest_log.txt','w')
@davidandrzej
davidandrzej / strict.scala
Created July 17, 2012 00:53
Map over a Strict Range
scala> val mutant = collection.mutable.ListBuffer[Int]()
mutant: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
scala> (0 until 4).map{idx => {
mutant.append(idx); idx > 0}}.exists(identity)
res0: Boolean = true
scala> mutant
res1: scala.collection.mutable.ListBuffer[Int] =
ListBuffer(0, 1, 2, 3)
@davidandrzej
davidandrzej / java-style-regex.scala
Created June 22, 2012 00:57
"Manually" extracting the captured groups
scala> val myRegex = """Foo=([0-9]+) Bar=([A-Z]+)""".r
myRegex: scala.util.matching.Regex = Foo=([0-9]+) Bar=([A-Z]+)
scala> val m = myRegex.findFirstMatchIn("Foo=123 Bar=ABC").get
m: scala.util.matching.Regex.Match = Foo=123 Bar=ABC
scala> val foo = m.group(1)
foo: String = 123
scala> val bar = m.group(2)
@davidandrzej
davidandrzej / regex-unapply.scala
Created June 22, 2012 00:23
Scala regex unapply magic
scala> val myRegex = """Foo=([0-9]+) Bar=([A-Z]+)""".r
myRegex: scala.util.matching.Regex = Foo=([0-9]+) Bar=([A-Z]+)
scala> "Foo=123 Bar=ABC" match {
| case myRegex(foo, bar) =>
| println("foobar looks like %s-%s".format(foo,bar))
| case _ => println("not a match")
| }
foobar looks like 123-ABC
@davidandrzej
davidandrzej / lazy.scala
Created May 25, 2012 03:36
Laziness, side-effects / mutability, and short-circuiting: a cautionary tale
scala> val mutant = collection.mutable.ListBuffer[Int]()
mutant: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
scala> (0 until 4).view.map{idx => {
mutant.append(idx); idx > 0}}.exists(identity)
res3: Boolean = true
scala> mutant
res4: scala.collection.mutable.ListBuffer[Int] =
ListBuffer(0, 1)
// record progress every second
val loggingInterval = 1000
var lastLogTime = System.currentTimeMillis()
// count how many elements we've processed
var lastCounter = 0
var counter = 0
elements.foreach { element => {
process(element)
@davidandrzej
davidandrzej / wma2mp3.sh
Created March 5, 2012 14:45
WMA-to-MP3 bash one-liner
# Assumes WMA files end in *.wma
for f in `ls | grep "\.wma" | rev | cut -d \. -f 2- | rev`; do ffmpeg -i "$f.wma" -acodec libmp3lame -ab 160k -ac 2 -ar 44100 "$f.mp3"; done
@davidandrzej
davidandrzej / GoogleContactCleanup.py
Created September 24, 2011 20:16
Script to help semi-automatically tidy up Google contacts
"""
Python script to help programmatically tidy up Google contacts
Example workflow:
1) export Google contacts and back this file up somewhere
(must use Outlook CSV format b/c Python csv chokes on Unicode)
2) run this script to generate a cleaned-up CSV contacts file
3) in Google contacts, delete all and re-import from this new CSV
4) use Google contacts merge/delete for finishing touches
"""