[Tutor] Compute data usage from log

Luke Paireepinart rabidpoobear at gmail.com
Mon Oct 26 12:12:18 CET 2009


On Mon, Oct 26, 2009 at 3:20 AM, Christian Witts <cwitts at compuscan.co.za>wrote:

> fInput = open('/path/to/log.file', 'rb')
> total_usage = 0
> for line in fInput:
>   total_usage += int(line.split(' ')[9].strip())
> print total_usage
>

It's actually bad to assign a variable to the file object in this case
(flinput = ....) because Python will automatically close a file after you're
done with it if you iterate over it directly, but if you include a reference
it will stay open until the python program ends or you explicitly call
flinput.close().  It doesn't matter much in this example but in general it
is good practice to either
1) call foo.close() immediately after you're done using a file object, or
2) don't alias the file object and just over it directly so Python will
auto-close it.

Therefore a better (and simpler) way to do the above would be:

total_usage = 0
for line in open('/path/to/log.file'):
    total_usage += int(line.split(' ')[9])

Also note you don't need to strip the input because int() coersion ignores
whitespace anyway. And additionally you shouldn't be opening this in binary
mode unless you're sure you want to, and I'm guessing the log file is ascii
so there's no need for the 'rb'.  (reading is default so we don't specify an
'r'.)


And since I like list comprehensions a lot, I'd probably do it like this
instead:

total_usage = sum([int(line.split(' ')[9]) for line in
open('/path/to/log.file')])

Which incidentally is even shorter, but may be less readable if you don't
use list comprehensions often.

Also, the list comprehension version is likely to be more efficient, both
because of the use of sum rather than repeated addition (sum is implemented
in C) and because list comprehensions in general are a tad faster than
explicit iteration, if i recall correctly (don't hold me to that though, I
may be wrong.)

>
> Of course this has no error checking and or niceties, but I will leave that
> up to you.

The same applies to my modifications.

Good luck, and let us know if you need anything else!

-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091026/43abdb7a/attachment.htm>


More information about the Tutor mailing list