Skip to content

Instantly share code, notes, and snippets.

@poros
Last active October 7, 2015 09:06
Show Gist options
  • Save poros/3902cc7000d628b3fc38 to your computer and use it in GitHub Desktop.
Save poros/3902cc7000d628b3fc38 to your computer and use it in GitHub Desktop.
Process a file in a functional style with generators (no temporary lists!)
wwwlog = open("access-log")
total = 0
for line in wwwlog:
bytestr = line.rsplit(None,1)[1]
if bytestr != '-':
total += int(bytestr)
print "Total", total
# more functional
wwwlog = open("access-log")
bytecolumn = (line.rsplit(None,1)[1] for line in wwwlog)
bytes = (int(x) for x in bytecolumn if x != '-')
print "Total", sum(bytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment