Problem with re.match - Newbie needs some advice

rh0dium sklass at pointcircle.com
Tue May 31 18:42:03 EDT 2005


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




More information about the Python-list mailing list