public
Created

Format Mozilla's "Application" stats into usable columns.

  • Download Gist
format-app.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#!/usr/bin/env/python
 
"""Format Mozilla's "Application" stats into usable columns."""
 
import csv
import re
import sys
 
reader = csv.reader(open(sys.argv[1]))
writer = csv.writer(sys.stdout)
 
keys_in = reader.next()[2:]
key_map = []
for key in keys_in:
key_orig = key
key = re.sub(r'({.*}|unknown)/?', '', key)
if re.match(r'\d\.\d(\.|a|b|pre|$)', key):
key = re.sub(r'^(\d\.\d).*', r'\1', key)
else:
key = 'junk'
key_map.append(key)
 
writer.writerow(['date'] + sorted(list(set(key_map))))
 
for record in reader:
tots = {}
for i, value in enumerate(record[2:]):
key = key_map[i]
tots.setdefault(key, 0)
tots[key] += int(value)
writer.writerow([record[0]] + [v for (k, v) in sorted(tots.items())])

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.