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'
3
  • Please fix indentation problems in your code. parse looks like a function created inside __init__ method. Commented May 1, 2016 at 20:40
  • Try now, using the improved indentation above. Commented May 1, 2016 at 21:08
  • the parse function it is not inside init it is after it just when i copied the code i made some space in it . i didn't found why this error appeared Commented May 1, 2016 at 21:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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
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.
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.