learning python, using string help

Tom Anderson twic at urchin.earth.li
Thu Feb 2 18:55:43 EST 2006


On Thu, 2 Feb 2006, mikeisgreat at gmail.com wrote:

> Well, I did want to add some formatting for example

I getcha. This is really an HTML problem rather than a python problem, 
isn't it? What you need to do is output a table.

FWIW, here's how i'd do it (assuming you've got HP-UX ruptime, since 
that's the only one i can find example output for [1]):

http://urchin.earth.li/~twic/ruptime.py

You can use this as a library, a command-line tool, or a CGI script; it'll 
automagically detect which context it's in and do the right thing. It's 
built around the output from the HP-UX version of ruptime; let me know 
what the output from yours looks like (a few lines would do) and i'll show 
you how to change it.

The first key bit is a pair of regular expressions:

lineRe = re.compile(r"(\S+)\s+(\S+)\s+([\d+:]+)(?:,\s+(\d+) users,\s+load ([\d., ]+))?")
uptimeRe = re.compile(r"(?:(\d+)\+)?(\d*):(\d*)")

These rip the output from ruptime apart to produce a set of fields, which 
can then be massaged into useful data. Once that's done, there's a big 
splodge of code which prints out an HTML document containing a table 
displaying the information. It's probably neither the shortest nor the 
cleanest bit of code in the universe, but it does the job and should, i 
hope, be reasonably clear.

tom

[1] http://docs.hp.com/en/B2355-90743/ch06s02.html

-- 
Science Never Sleeps



More information about the Python-list mailing list