Last active
October 7, 2015 09:06
-
-
Save poros/3902cc7000d628b3fc38 to your computer and use it in GitHub Desktop.
Process a file in a functional style with generators (no temporary lists!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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