[Tutor] My First Python Program (Vey Large)!

alan.gauld@bt.com alan.gauld@bt.com
Tue, 8 Oct 2002 11:37:10 +0100


> Comments are often an issue with interpreted languages 

Not significant with Python which compiles the code before running it.
The compile stage may take marginally longer but its no big deal.


However I'll strip out many of them below for brevity!

> # Program Variables ('s' denotes 'string', 'i' denotes 
> 'integer' and 'l' denotes 'logical')

Be aware that python variables are all object references so
the naming convention could be misleading since there is 
nothing to stop you reassigning (even accidentally) an 
integer to a string etc...

> sSrvName = ""       # Temporary string to store server names
> sSrvStatus = {}     # Array to contain current server event status
> sSrvDownDate = {}   # Array to contain current server down date
> sSrvDownTime = {}   # Array to contain current server down time

These are actually dictionaries not arrays, that could be 
significant later...

> lStart = _TRUE_     # Report start flag
> iSrvNumber = 0      # Number of servers so far found (integer)
> sReptStart = ""     # Initialise Report Start Date & Time (string)
> sReptEnd = ""       # Initialise Report End Date & Time (string)

This is crying out for a server class but we can ignore that for now!

> fCTXLog = open ('c:\python22\ctxstat.log', 'r')

Probably not a good idea to store the log file in the python directory 
tree, better to set up a dedicated folder for it somewhere...

> while _TRUE_:
>     sLine = fCTXLog.readline() + fCTXLog.readline()
>     if string.strip (sLine) == "":
>         break

You don't need the string module for strip, its now a method of 
string objects... since you use strip later assign to a variable:

sLine = sLine.strip()

>     sEvents = ["20" + sLine [7:9] + ":" + sLine [0:2] + ":" + 
> sLine [3:5],
> sLine [10:15], string.strip(sLine [17:27])]

string addition is inefficient coz it creates multiple intermediate strings,

you might get a speed up using string formatting instead:

sEvents = "20%s:%s:%s%s%s" %
(sLine[7:9],sLine[0:2],sLine[3:5],sline10:15],sLine17:25])

>     if ctxfunc.readctxini(sEvents[2], "Found", "No") == "No":
>         ctxfunc.writectxini(sEvents[2], "Found", "Yes")
>         ctxfunc.writectxini(sEvents[2], "Restarts", 0)
>         iSrvNumber = iSrvNumber + 1
>         sSrvNumber = "Server " +
> string.zfill(string.strip(str(iSrvNumber)), 3)

Try string formatting again:

          sSrvNumber = "Server%d" % iSrvNumber

>     sPDate = sEvents [0] + ":" + sEvents [1]+ ":00"

Hmm, always 2:0:00... I assume this will get changed at some point?


>     if lStart == _TRUE_:
>         sReptStart = sPDate
>         lStart = _FALSE_

>     if sLine [49:50] == "U":

One char so just use sLine[49] - indexing is cheaper than slicing....

>         sEvents.append ("UP")
>     else:
>         sEvents.append ("DOWN")

>     if string.find (sSrvName, "[" + sEvents[2] + "]") < 0:
>         sSrvName = sSrvName + "[" + sEvents [2] + "]"

You do the string creation twice so assign to a temp var:

     str = "[" + sEvents [2] + "]"
     if string.find (sSrvName, str) < 0:
         sSrvName = sSrvName + str

saves all that multiple string creation stuff being done more than once

>         sSrvStatus [sEvents[2]] = "UP"

>     if sSrvStatus[sEvents[2]] != sEvents [3]:
>         sSrvStatus [sEvents[2]] = sEvents [3]
>         if sEvents[3] == "DOWN":

This isn't quite so bad but you still do the indexing multiple 
times, temp variables are better.

index = sEvents[2]
condition = sEvents[3]

if sSrvStatus[index] != condition
     sSrvStatus[index] = condition
     if condition == "DOWN":
   
>                 iRestarts = iRestarts + 1
>                 sEventNo = 'Event ' + string.zfill(str(iRestarts), 3)

Use string formatting again.

> iReptTime = jcrfunc.time2sec(sReptEnd) - jcrfunc.time2sec(sReptStart)

Not sure what the jcrfunc stuff does but you might be better using 
the standard python time module?

> fCTXRept.write ("Citrix Server Report\n====================")
> fCTXRept.write ("\nReport Start:\t" + sJReptStart)
> fCTXRept.write ("\nReport End:\t" + sJReptEnd)
> fCTXRept.write ("\nReport Time:\t" + sJReptTime + " (" + 

Probably better to use triple quoted strings and formatting here then 
write the string in one go...

str = """    # The newline will be included but makes for neater code
layout...
Citrix Server Report
====================
Report Start:\t%s
Report End:\t%s
Report Time:\t%s (%d sec)

""" % (sJReptStart, sJReptEnd, sJReptTime)

fCTXRept.write(str)

Oops, I've run out of time(a teleconference calls)... 
but hopefully some general principles there.

Someone else will add more I guess.

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld