473,597 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help With PyParsing of output from win32pdhutil.Sh owAllProcesses( )

Hi All (especially Paul McGuire!)

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

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.Sh owAllProcesses( )

print process_info
print

## Output from ShowAllProcesse s :

##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_inf o)
print('type = ', type(sProcess_I nfo))

## 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.Liter al('Process Name ID Process,% Processor
Time,% User Time,% Privileged Time,Virtual Bytes Peak,Virtual
Bytes').suppres s()
integer = pyparsing.Word( pyparsing.nums)
process_name = pyparsing.Word( pyparsing.alpha s)

#ProcessList = heading + process_name + pyparsing.OneOr More(integer)
ProcessList = process_name + pyparsing.OneOr More(integer)

# Now parse data and print results

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

try:
data = ProcessList.par seString(curren t_line)
print "data:", data
except:
pass
print('\n\nPars e Actual data : \n\n')
## Parse the actual data from ShowAllProcesse s :

ProcessList = heading + process_name + pyparsing.OneOr More(integer)
data = ProcessList.par seString(sProce ss_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.par seString(sProce ss_Info)
File "C:\Python25\li b\site-packages\pypars ing.py", line 821, in
parseString
loc, tokens = self._parse( instring.expand tabs(), 0 )
File "C:\Python25\li b\site-packages\pypars ing.py", line 712, in
_parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "C:\Python25\li b\site-packages\pypars ing.py", line 1864, in
parseImpl
loc, resultlist = self.exprs[0]._parse( instring, loc, doActions,
callPreParse=Fa lse )
File "C:\Python25\li b\site-packages\pypars ing.py", line 716, in
_parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "C:\Python25\li b\site-packages\pypars ing.py", line 2106, in
parseImpl
return self.expr._pars e( instring, loc, doActions,
callPreParse=Fa lse )
File "C:\Python25\li b\site-packages\pypars ing.py", line 716, in
_parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "C:\Python25\li b\site-packages\pypars ing.py", line 1118, in
parseImpl
raise exc
pyparsing.Parse Exception: 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 2638
On 9/11/07, Steve <sr********@gma il.comwrote:
Hi All (especially Paul McGuire!)

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

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(d ata)

# 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
1456
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: 12345-1\r\n \r\n
9
2909
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 part ). I have had a look at htmllib but I need more info. Can anyone point me in the right direction , a tutorial or something would be great. Many thanks
15
2009
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 them in my time calculator and writing them in by hand. Now i realize, that i really need a script to do this because: 1. It turns out there are hundreds of pages of this stuff. 2. I have to do something similar in again soon. 3. By doing it by...
5
1317
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): ========================================== 1: <- 2: /*<- 3: ----------------------<-
4
1912
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: cond(c,a,b)
0
2040
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 word and number arguments like this: GRILLE EURAT5 Coin Nord-Ouest : 46.50/ 0.50 Coin Sud-E Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file.
1
353
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 = '' def main():
4
1535
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 objects. The file fairly resembles HTML-EXAMPLE pasted below. I need to parse the file in such a way to extract data out of the html and to come up with a tab separated file that would look like OUTPUT-
5
1483
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 accumulated in SVN, so time for a release! Here's what's new in Pyparsing 1.5.1: - Added __dir__() methods to ParseBaseException and ParseResults, to support new dir() behavior in Py2.6 and Py3.0. If dir() is called on a ParseResults object,...
0
7967
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7885
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8031
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6687
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5428
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.