Checking the existence of parsed variables

2008-01-23 Thread Gavin Lusby

Hello all,
I am regularly writing programs that parses ever changing lists of
variables.
The method I do it is by loading it in to a dictionary of the form:
d['variable_name']=variable_value

So when it comes to a method that uses a required set of variables I have to
make tens of the following statements
if d.has_key('swr'): 
swr = float(d['swr'])
else: 
print 'ERROR: COREYTAB variable swr not specified'; iErr += 1

When I thought a pythonic way of doing it would be to use a variable
variable.(sorry that sounds ridiculous) but it would work like this:

_start_code_
___
# make all input parameters equal the string of themselves
type = 'type'; tabnum = 'tabnum';  coreyw = 'coreyw'; coreyow =
'coreyow'
swc = 'swc'; swr = 'swr'; kro_swc = 'kro_swc'; krw_swr = 'krw_swr'
coreyg = 'coreyg'; coreygo = 'coreygo'; sgi = 'sgi'; sgc = 'sgc'
sgr = 'sgr'; kro_sgc = 'kro_sgc'; krg_swc = 'krg_swc'
# error check parameters existence and if there, over-write their info
# if there are integer values, then must remember to re-typecast them
later
for var in (type, tabnum, coreyw, coreyow, swc, swr, kro_swc, krw_swr,
coreyg, coreygo, sgi, sgc, sgr, kro_sgc, krg_swc):
if d.has_key(var):
if isnumeric(d[var]):
var = float(d[var])
else:
var = d[var]
else:
print 'ERROR: getcorey() parameter '+str(var)+' not specified'
iErr += 1
_end_code___
___

Since this doesn't work, any other ideas out there?

Many thanks in advance, Gavin

-- 
http://mail.python.org/mailman/listinfo/python-list

File.write() not working in a sleepy loop

2008-02-19 Thread Gavin Lusby
Hello,

I am getting odd behaviour when I am trying to write a file in a while loop
with a sleep function call.

I am trying to periodically change the number of jobs on a cluster depending
on the current usage and a dummy version that does not require access to the
cluster usage is attached at the end.

The crux of the matter is in the main loop. When I run it, it writes a blank
file on the first iteration, then sleeps and on the 2nd iteration, it
finally puts in the correct value. I simply can't understand why it
postpones the open, write, close section of the 1st loop until the sleep is
finished. The sleep() is clearly after the write().

Thanks in advance, Gavin

 
___sample_code___sample_code___sample_code___sample_code___sample_code___sample_code___sample_code

while i == 1:
if pid <> -1:
i = pid_exists(pid)
if g.verbose: os.system('clear')
## get data ###
freeload, halfload, fullload =
20,20,40#getnodedata()<--- dummy data
weekend, phase = gettimedata()
userload =
2#getuserdata()<-- dummy
data
if g.verbose: print '...total nodes =',2 * (freeload + halfload +
fullload)
if g.verbose: print '...used nodes =',halfload + 2 * fullload
if g.verbose: print '...free nodes =',2 * freeload + halfload
if g.verbose: print '...user nodes =',userload
# usage logic #
if weekend or phase == 3:
answer = maxnode
if g.verbose:
print '...maxing out the machine with '+str(answer)
else:
answer = max(minnode,2*freeload+halfload-slack)
if g.verbose:
print '...limiting runs to '+str(answer)+' nodes'
 write outfile 
o=open(os.getcwd()+g.dsep+file, 'w')
if g.verbose:
print '...writing to '+os.getcwd()+g.dsep+file
o.write('%over = (njobs => '+str(answer)+');\n')
o.close
### sleep now #
sleep(refresh)
#! /data/gen27/utg/lusbg1/bin/Python-2.5.1/python
import sys, os, getopt, datetime
from time import sleep
from subprocess import Popen, PIPE
from getpass import getuser

class g:
'''
global variables class

'''
if os.name in ('nt','dos','ce'):
dsep = '\\'
else:
dsep = '/'
verbose = False

def main():
'''
main iterative subroutine

'''
if g.verbose: os.system('clear'); print '\n...loadmax.py'
### get opts ##
try:
opts, args = getopt.getopt(sys.argv[1:], "o:p:r:l:u:s:thv", 
["help","verbose"])
except getopt.GetoptError:
usage()
sys.exit()
file = 'mybad'
slack = 20
minnode = 40
maxnode = 100
refresh = 10*60
tracker = 'tracker.dat'
answer = 0
pid = -1
for opt, arg in opts:
if opt == "-v": # verbosity switch
g.verbose = True
if opt == "-o": # output filename switch
file = arg
if opt == "-t": # tracker filename switch
tracker = True
if opt == "-r": # refresh rate converted min->sec
refresh = float(arg)*60.0
if opt == "-u": # maximum permissable nodes
maxnode = int(arg)
if opt == "-l": # minimum permissable nodes
minnode = int(arg)
if opt == "-p": # follow specified pid
pid = int(arg)
if opt == "-s": # no of slack nodes
slack = int(arg)
if opt in ("-h", "--help"): # help!
usage()
sys.exit()
if file == 'mybad':
print '...error: output file not specified'
usage()
sys.exit()
i = 1
while i == 1:
if pid <> -1:
i = pid_exists(pid)
if g.verbose: os.system('clear')
## get data ###
freeload, halfload, fullload = 20,20,40#getnodedata()
weekend, phase = gettimedata()
userload = 2#getuserdata()
if g.verbose: print '...total nodes =',2 * (freeload + halfload + 
fullload)
if g.verbose: print '...used nodes =',halfload + 2 * fullload
if g.verbose: print '...free nodes =',2 * freeload + halfload
if g.verbose: print '...user nodes =',userload
# usage logic #
if weekend or phase == 3:
answer = maxnode
if g.verbose:
print '...maxing out the machine with '+str(answer)
else:
answer = max(minnode,2*freeload+halfload-slack)
if g.verbose: