473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help With PyParsing of output from win32pdhutil.ShowAllProcesses()

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

Sep 11 '07 #1
1 2617
On 9/11/07, Steve <sr********@gmail.comwrote:
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) :
Any particular reason you need to use pyparsing? Seems like an
overkill for such simple data.

Here's an example:

import pprint

X="""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"""

data = []
for line in X.split('\n')[1:]: # Skip the first row
split = line.split()
row = [split[0]] # Get the process name
row += [int(x) for x in split[1:]] # Convert strings to int, fail
if any aren't.
data.append(row)

pprint.pprint(data)

# Output follows:
#
#[['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]]
#
Sep 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Khoa Nguyen | last post by:
Hi, I am a newbie to Python and pyparsing. I am having difficulty creating a grammar that spans multiple lines, the input data look like this RTSP/1.0 200 OK\r\n Cseq: 1\r\n Session:...
9
by: RiGGa | last post by:
Hi, I want to parse a web page in Python and have it write certain values out to a mysql database. I really dont know where to start with parsing the html code ( I can work out the database...
15
by: kpp9c | last post by:
I am kind of in a bit of a jam (okay a big jam) and i was hoping that someone here could give me a quick hand. I had a few pages of time calculations to do. So, i just started in on them typing...
5
by: Dave | last post by:
OK, I'm stumped. I'm trying to find newline characters (\n, specifically) that are NOT in comments. So, for example (where "<-" = a newline character):...
4
by: MooMaster | last post by:
I'm trying to develop a little script that does some string manipulation. I have some few hundred strings that currently look like this: cond(a,b,c) and I want them to look like this: ...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
1
by: Neal Becker | last post by:
I'm just trying out pyparsing. I get stack overflow on my first try. Any help? #/usr/bin/python from pyparsing import Word, alphas, QuotedString, OneOrMore, delimitedList first_line = ''...
4
by: egonslokar | last post by:
Hello Python Community, It'd be great if someone could provide guidance or sample code for accomplishing the following: I have a single unicode file that has descriptions of hundreds of...
5
by: Paul McGuire | last post by:
I've just uploaded to SourceForge and PyPI the latest update to pyparsing, version 1.5.1. It has been a couple of months since 1.5.0 was released, and a number of bug-fixes and enhancements have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.