Skip to content

Instantly share code, notes, and snippets.

@haakonn
haakonn / Logfile to CSV in Scala
Created October 7, 2013 19:00
Just a quick Scala script to convert a certain logfile into CSV. Instead of instinctively going to bash or python for these things, why not Scala? Just run with 'scala makecsv.scala', starts and finishes quickly. No build step or bytecode or support files, just a script.
println("date,msisdn,message")
scala.io.Source.fromFile("problem.log").getLines().foreach { line =>
val m = "^(.{19}).*msisdn='(.{10}).*msg='(.*)', status.*".r.findFirstMatchIn(line).get
println(s"""${m.group(1)},${m.group(2)},"${m.group(3)}"""")
}
@haakonn
haakonn / gist:3938834
Created October 23, 2012 13:45
Python function for partitioning a range of integers into X number of subranges
def partition(partition_count, id_range):
"""
Partitions an integer range into partition_count partitions of uniform size.
id_range specifies the integer range to partition (a tuple (from, to)).
"""
partition_size = ceil((id_range[1] - id_range[0]) / partition_count) + 1
for x in range(0, partition_count):
from_ = int(x * partition_size) + id_range[0]
to = min(int(from_ + partition_size - 1), id_range[1])
yield (from_, to)
@haakonn
haakonn / gist:2353416
Created April 10, 2012 18:22
Utility to concatenate an array of single digits ([1,2,3]) into an int (123)
public class ArrayToIntUtil {
public static int a2i(int... a) {
int result = 0;
int f = (int) Math.pow(10, a.length);
for (int i : a) result += i * (f /= 10);
return result;
}
public static void main(String... args) {