Help With PyParsing of output from win32pdhutil.ShowAllProcesses()

Steve sreisscruz at gmail.com
Tue Sep 11 14:12:01 EDT 2007


Hi All (especially Paul McGuire!)

Could you lend a hand in the grammar and paring of the output from the
function win32pdhutil.ShowAllProcesses()?

This is the code that I have so far (it is very clumsy at the
moment) :


import string
import win32api
import win32pdhutil
import re
import pyparsing


process_info = win32pdhutil.ShowAllProcesses()

print process_info
print

## Output from ShowAllProcesses :

##Process Name   ID Process,% Processor Time,% User Time,% Privileged
Time,Virtual Bytes Peak,Virtual Bytes
##PyScripter     	 2572     0     0     0 96370688 96370688
##vmnetdhcp      	 1184     0     0     0 13942784 13942784
##vmount2        	  780     0     0     0 40497152 38400000
##ipoint         	  260     0     0     0 63074304 58531840


sProcess_Info = str(process_info)
print('type = ', type(sProcess_Info))

## Try some test data :
test = ('Process Name   ID Process,% Processor Time,% User Time,%
Privileged Time,Virtual Bytes Peak,Virtual Bytes',
        'PyScripter     	 2572     0     0     0 96370688 96370688',
        'vmnetdhcp      	 1184     0     0     0 13942784 13942784',
        'vmount2        	  780     0     0     0 40497152 38400000',
        'ipoint         	  260     0     0     0 63074304 58531840')

heading = pyparsing.Literal('Process Name   ID Process,% Processor
Time,% User Time,% Privileged Time,Virtual Bytes Peak,Virtual
Bytes').suppress()
integer = pyparsing.Word(pyparsing.nums)
process_name = pyparsing.Word(pyparsing.alphas)

#ProcessList = heading + process_name + pyparsing.OneOrMore(integer)
ProcessList = process_name + pyparsing.OneOrMore(integer)

# Now parse data and print results

for current_line in test :
    print('Current line = %s') % (current_line)

    try:
       data = ProcessList.parseString(current_line)
       print "data:", data
    except:
       pass


print('\n\nParse Actual data : \n\n')
## Parse the actual data from ShowAllProcesses :

ProcessList = heading + process_name + pyparsing.OneOrMore(integer)
data = ProcessList.parseString(sProcess_Info)
print "data:", data
print "data.asList():",
print "data keys:", data.keys()



=====

Output from run :


Process Name ID Process,% Processor Time,% User Time,% Privileged
Time,Virtual Bytes Peak,Virtual Bytes
PyScripter       2572     0     0     0 101416960 97730560
vmnetdhcp        1184     0     0     0 13942784 13942784
vmount2           780     0     0     0 40497152 38400000
ipoint            260     0     0     0 65175552 58535936
DockingDirector   916     0     0     0 102903808 101695488
vmnat             832     0     0     0 15757312 15757312
svchost          1060     0     0     0 74764288 72294400
svchost          1120     0     0     0 46632960 45846528
svchost          1768     0     0     0 131002368 113393664
svchost          1988     0     0     0 33619968 31047680
svchost           236     0     0     0 39841792 39055360
System              4     0     0     0 3624960 1921024
....

None

('type = ', <type 'str'>)
Current line = Process Name   ID Process,% Processor Time,% User Time,
% Privileged Time,Virtual Bytes Peak,Virtual Bytes
Current line = PyScripter        2572     0     0     0 96370688
96370688
data: ['PyScripter', '2572', '0', '0', '0', '96370688', '96370688']
Current line = vmnetdhcp         1184     0     0     0 13942784
13942784
data: ['vmnetdhcp', '1184', '0', '0', '0', '13942784', '13942784']
Current line = vmount2            780     0     0     0 40497152
38400000
data: ['vmount', '2', '780', '0', '0', '0', '40497152', '38400000']
Current line = ipoint             260     0     0     0 63074304
58531840
data: ['ipoint', '260', '0', '0', '0', '63074304', '58531840']


Parse Actual data :


Traceback (most recent call last):
  File "ProcessInfo.py", line 55, in <module>
    data = ProcessList.parseString(sProcess_Info)
  File "C:\Python25\lib\site-packages\pyparsing.py", line 821, in
parseString
    loc, tokens = self._parse( instring.expandtabs(), 0 )
  File "C:\Python25\lib\site-packages\pyparsing.py", line 712, in
_parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "C:\Python25\lib\site-packages\pyparsing.py", line 1864, in
parseImpl
    loc, resultlist = self.exprs[0]._parse( instring, loc, doActions,
callPreParse=False )
  File "C:\Python25\lib\site-packages\pyparsing.py", line 716, in
_parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "C:\Python25\lib\site-packages\pyparsing.py", line 2106, in
parseImpl
    return self.expr._parse( instring, loc, doActions,
callPreParse=False )
  File "C:\Python25\lib\site-packages\pyparsing.py", line 716, in
_parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "C:\Python25\lib\site-packages\pyparsing.py", line 1118, in
parseImpl
    raise exc
pyparsing.ParseException: Expected "Process Name   ID Process,%
Processor Time,% User Time,% Privileged Time,Virtual Bytes
Peak,Virtual Bytes" (at char 0), (line:1, col:1)



Many thanks!

Steve




More information about the Python-list mailing list