Python algorithm - parsing log files