How to read such file and sumarize the data?

MRAB python at mrabarnett.plus.com
Wed Nov 17 23:32:38 CET 2010


On 17/11/2010 21:45, huisky wrote:
> Say I have following log file, which records the code usage.
> I want to read this file and do the summarize how much total CPU time
> consumed for each user.
> Is Python able to do so or say easy to achieve this?, anybody can give
> me some hints, appricate very much!
>
>
> Example log file.
> **************************************************************************************
> LSTC license server version 224 started at Sun Dec  6 18:56:48 2009
> using configuration file /usr/local/lstc/server_data
> xyz 15424 at trofast3.marin.ntnu.no LS-DYNA_971 NCPU=1 started Sun Dec  6
> 18:57:40
> 15424 at trofast3.marin.ntnu.no completed Sun Dec  6 19:42:55
> xyz 15500 at trofast3.marin.ntnu.no LS-DYNA_971 NCPU=2 started Sun Dec  6
> 20:17:02
> 15500 at trofast3.marin.ntnu.no completed Sun Dec  6 20:26:03
> xyz 18291 at trofast2.marin.ntnu.no LS-DYNA_971 NCPU=1 started Sun Dec  6
> 21:01:17
> 18291 at trofast2.marin.ntnu.no completed Sun Dec  6 21:01:28
> tanhoi 552 at iimt-tanhoi-w.ivt.ntnu.no LS-DYNA_971 NCPU=1 started Mon
> Dec  7 09:31:00
> 552 at iimt-tanhoi-w.ivt.ntnu.no presumed dead Mon Dec  7 10:36:48
> sabril 18863 at trofast2.marin.ntnu.no LS-DYNA_971 NCPU=2 started Mon
> Dec  7 13:14:47
> 18863 at trofast2.marin.ntnu.no completed Mon Dec  7 13:24:07
> sabril 18937 at trofast2.marin.ntnu.no LS-DYNA_971 NCPU=2 started Mon
> Dec  7 14:21:34
> sabril 18969 at trofast2.marin.ntnu.no LS-DYNA_971 NCPU=2 started Mon
> Dec  7 14:28:42
> 18969 at trofast2.marin.ntnu.no killed Mon Dec  7 14:31:48
> 18937 at trofast2.marin.ntnu.no killed Mon Dec  7 14:32:06

Here's how I would probably do it:

Use 2 dicts, one for the start time of each user, and the other for the
total elapsed time of each user.

For each line extract the user name, date/time and whether the user is
starting, finishing, etc.

When a user starts, save the info in the first dict, and when a user
finishes, calculate the elapsed time and add it to the total for that
user.

The date/time can be parsed by the time or datetime module.



More information about the Python-list mailing list