Problem with re.match - Newbie needs some advice

R

rh0dium

Hi all,

I am having a problem with the method cpuNum. Basically I look to see
if the "flag" for "ht" is enabled. If it is the total processors
should be cpucount/2. It's not working. I can't seem to get into the
second re.match. Can someone point out my apparent error?

class Sysinfo:
def __init__(self, cpuinfo="/proc/cpuinfo",
meminfo="/proc/meminfo"):
cpustat = open(cpuinfo)
self.cpudata = cpustat.readlines()
cpustat.close()

def cpuNum(self):
cpucount=0
ht=0
for line in self.cpudata:
# Remove the newlines..
line=line.strip()
if re.match(r"^processor\s+:", line):
cpucount += 1
if re.match(r"^flags\s+:", line):
flags=re.split(r":\s",line)
#print "Found flags", flags[1]
if re.match(r"\bht\b",flags[1]):
print "HT on"
ht=1
if ht:
return cpucount/2
else:
return cpucount

s = Sysinfo( cpuinfo="cpuinfo.1", meminfo="meminfo.1" )
print s.cpuNum()


---- cpuinfo.1 (abbreviated) ----
processor : 0
flags : cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss
ht tm pbe cid

processor : 1
flags : cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss
ht tm pbe cid
 
J

John Machin

rh0dium said:
Hi all,

I can't seem to get into the
second re.match. Can someone point out my apparent error?

errorS:
1. Imprecision -- the problem is in the *third* re.match ...
2. ... which should be re.search
3. Using re at all when simple string methods would do -- see below

[snip]
for line in self.cpudata:
# Remove the newlines..
### line=line.strip()
tokens = line.split()
### if re.match(r"^processor\s+:", line):
if tokens[0] == "processor":
cpucount += 1
### if re.match(r"^flags\s+:", line):
elif tokens[0] == "flags" and "ht" in tokens:
### flags=re.split(r":\s",line)
#### print "Found flags", flags[1]
### if re.match(r"\bht\b",flags[1]):
print "HT on"
ht=1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top