Skip to content

Instantly share code, notes, and snippets.

@ox
Created December 17, 2012 23:05
Show Gist options
  • Save ox/4323280 to your computer and use it in GitHub Desktop.
Save ox/4323280 to your computer and use it in GitHub Desktop.
import sys
from os.path import exists
import subprocess
import re
# Calls the R system specifying that commands come from file commands.R
# The commands.R provided with this assignment will read the file named
# data and will output a histogram of that data to the file pageshist,pdf
def runR( ):
res = subprocess.call(['R', '-f', 'commands.R'])
# log2hist analyzes a log file to calculate the total number of pages
# printed by each user during the period represented by this log file,
# and uses R to produce a pdf file pageshist.pdf showing a histogram
# of these totals. logfilename is a string which is the name of the
# log file to analyze.
#
def log2hist(logfilename):
log = open(logfilename)
users = {}
print_regex = re.compile('user:\s*(?P<user>.*)\s*printer:.*?pages:\s*(?P<pages>\d+)')
for line in log:
match = print_regex.search(line)
if match:
user = match.group('user').strip()
pages = int(match.group('pages'))
if user not in users:
users[user] = pages
else:
users[user] += pages
runR()
data = open('data', 'w+')
for pages in users.values():
data.write("%d\n" % pages)
data.close()
if __name__ == '__main__':
script, logfilename = sys.argv
if not exists(logfilename):
print "%r does not exist" % logfilename
sys.exit()
log2hist(logfilename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment