Skip to content

Instantly share code, notes, and snippets.

@dirkgr
dirkgr / gist:20a00d30522c1381f0e2
Created July 3, 2014 23:12
Print class path in Scala
def urlses(cl: ClassLoader): Array[java.net.URL] = cl match {
case null => Array()
case u: java.net.URLClassLoader => u.getURLs() ++ urlses(cl.getParent)
case _ => urlses(cl.getParent)
}
val urls = urlses(getClass.getClassLoader)
println(urls.mkString("\n"))
private class CaseInsensitiveString(string: String) {
def =~(other: String): Boolean =
string equalsIgnoreCase other
def startsWithI(other: String) : Boolean =
string.toLowerCase startsWith other.toLowerCase
}
implicit def makeCaseInsensitiveString(string: String) =
new CaseInsensitiveString(string)
@dirkgr
dirkgr / FormattableString
Last active August 29, 2015 14:04
Enrichment for Scala strings that enables Python-like string formatting
class FormattableString(string: String) {
def %(args: Any*) = string.format(args:_*)
}
object FormattableString {
implicit def formattableString(string: String):FormattableString = new FormattableString(string)
}
@dirkgr
dirkgr / pre-commit
Created July 29, 2014 17:29
sbt format checker git hook
#!/bin/sh
trap 'exit 1' ERR
/usr/local/bin/sbt -Dsbt.log.noformat=true warn compile formatCheckStrict
@dirkgr
dirkgr / StringImplicits
Created September 2, 2014 19:40
Unicode-to-ASCII
object StringImplicits {
/** Maps weird unicode characters to ASCII equivalents
* This list comes from http://lexsrv3.nlm.nih.gov/LexSysGroup/Projects/lvg/current/docs/designDoc/UDF/unicode/DefaultTables/symbolTable.html */
val unicodeCharMap = Map(
'\u00AB' -> "\"",
'\u00AD' -> "-",
'\u00B4' -> "'",
'\u00BB' -> "\"",
'\u00F7' -> "/",
'\u01C0' -> "|",
@dirkgr
dirkgr / PatternMatchingOnInt
Created November 20, 2014 23:56
Pattern matching on Int
// pattern matching on Int
object Int {
def unapply(s: String): Option[Int] = try {
Some(s.toInt)
} catch {
case _: java.lang.NumberFormatException => None
}
}
@dirkgr
dirkgr / json-canon.py
Created February 4, 2015 00:02
Canonicalizes input JSON files
#!/usr/bin/python
import sys
import json
for f in sys.argv[1:]:
for line in open(f):
j = json.loads(line.strip())
print json.dumps(j, sort_keys=True)
@dirkgr
dirkgr / textify.py
Created May 15, 2015 20:52
Parse queries out of query logs
import re
import sys
import urllib
for line in sys.stdin:
m = re.search("""GET /search?\S*q=([^& ]*)""", line)
if m:
s = urllib.unquote_plus(m.groups(1)[0])
try:
print s.decode('utf-8')
@dirkgr
dirkgr / lock.sh
Created September 24, 2015 20:14
How to lock in bash
#!/usr/bin/env bash
set -e
set -x
LOCKFILE=~/LOCK
if [ $1 -a $1 = "lockAcquired" ]; then
echo "I'm in."
sleep 10
@dirkgr
dirkgr / fixJson.sh
Created November 23, 2015 19:36
In a JSON file with run-together brackets ("}{"), this inserts newlines between them, so you have one JSON expression on one line.
perl -pe 'BEGIN{ $/="}{" } s/}{/}\n{/g'