0

i am wondring if somone could help me please to resolve this error cause I am a beginer in pyparsing and i would like to parse a my log file :

Memory: svmem(total=4014465024, available=3451576320, percent=14.0, used=1240055808, free=2774409216, active=543842304, inactive=525877248, buffers=82866176, cached=594300928)
CPU: 35.70 % 
Elapsed execution time: 0.642941951752 seconds

and this is my parsing.py file :

import sys 
import os
from pyparsing import Word, ZeroOrMore, Group, White, Optional, printables, ParseException, restOfLine, alphas, alphanums, nums, Suppress, Combine, LineStart 

class Parser(object):
    def __init__(self):
        Mem = Word(alphanums) + Suppress(":") + Suppress(Word(alphas)) + Suppress("(")
        val1 = Word(alphanums) + "=" + Word(nums) + Suppress(",") 
        val2 = Word(alphanums) + "=" + Word(nums) + Suppress(",") 
        val3 = Word(alphanums) + "=" + Word(nums + '.') + Suppress(",") 
        val4 = Word(alphanums) + "=" + Word(nums) + Suppress(",") 
        val5 = Word(alphanums) + "=" + Word(nums) + Suppress(",") 
        val6 = Word(alphanums) + "=" + Word(nums) + Suppress(",") 
        val7 = Word(alphanums) + "=" + Word(nums) + Suppress(",") 
        val8 = Word(alphanums) + "=" + Word(nums) + Suppress(",") 
        val9 = Word(alphanums) + "=" + Word(nums) + Suppress(")") 
        cpu = Word(alphanums) + ":" + Word(nums) + Suppress("%") 
        time = Word(alphanums) + ":" + Word(nums + '.') + Suppress(Word(alphas)) 
        #Metrics = Combine(Mem + val1 + val2 + val3 + val4 + val5 + val6 + val7 + val8 + val9)  
        self.__pattern = Mem + val1 + val2 + val3 + val4 + val5 + val6 + val7 + val8 + val9 + cpu + time

    def parse(self, line):
        parsed = self.__pattern.parseString(line)

        payload              = {}
        payload["Mem"]  = parsed[0]
        payload["val1"]  = parsed[1]
        payload["val2"]  = parsed[2]
        payload["val3"]  = parsed[3]
        payload["val4"]  = parsed[4]
        payload["val5"]  = parsed[5]
        payload["val6"]  = parsed[6]
        payload["val7"]  = parsed[7]
        payload["val8"]  = parsed[8]
        payload["val9"]  = parsed[9]
        payload["cpu"]  = parsed[10]
        payload["time"]   = parsed[11]


        return payload

        """ --------------------------------- """

def main():
  parser = Parser()

  with open('./perf.log') as syslogFile:
     for line in syslogFile:
         if line != "\n":
            fields = parser.parse(line)
       print "parsed:", fields

if __name__ == "__main__":
    main()

and this is the error Message :

Traceback (most recent call last):
  File "xlog.py", line 54, in <module>
    main()
  File "xlog.py", line 50, in main
    fields = parser.parse(line)
AttributeError: 'Parser' object has no attribute 'parse'
PaulMcG
  • 62,419
  • 16
  • 94
  • 130
yosra
  • 11
  • 4

1 Answers1

1

Your indentation appears to be incorrect. It looks like your parse method is declared inside your __init__. Remember that whitespace matters in Python, and you can declare a function inside of a function.

Steve H
  • 101
  • 3
  • After I run the code that is indented as shown above, I get a different error now, one related to the parser. That will be the next thing for you to fix. – PaulMcG May 02 '16 at 01:53
  • you mean that i have to look for my expression in my parser class cause i took them like it was in same line but they are not – yosra May 02 '16 at 09:56
  • Then just parse them all as one string, not line by line. Pyparsing will skip over the newlines. But you might be better off writing a separate parser for each line first, until you get each one working. Then you can combine them into one and just parse the whole file at once. – PaulMcG May 02 '16 at 11:33
  • yeap it works , i parsed them line by line then i replace '\n' with '' in the line and then i applied them for all the parsed expression and it worked. Thnx for the response :) – yosra May 04 '16 at 14:05