# -*- coding: iso-8859-7 -*-
from gen import ing,prg,tog
from jorpath import path
def inpDouble(mes, douDef=None):
"Inputs a double with default value."
while True:
dline = raw_input(tog(mes)).strip()
if dline == '' and douDef != None: return douDef # Default value
try: return float(dline)
except: pass
prg("\n \n .\n")
def inpLong(mes, douDef=None):
"Inputs an integer with default value."
while True:
dline = raw_input(tog(mes)).strip()
if dline == '' and douDef != None: return douDef # Default value
try: return int(dline)
except: pass
prg("\n \n .\n")
def inpNo(mes, douDef=None):
"Inputs yes or no."
while True:
dline = raw_input(tog(mes)).strip()
if dline == '' and douDef != None: return douDef # Default value
dl = ing(dline[:2])
#---------Check what We read
if dl in ('', '', 'na', 'NA', 'ye', 'YE', '1'): return True
if dl in ('', '', 'ox', 'OX', 'no', 'NO', '0'): return False
prg('\n (, , na, ye, YE, 1) (X, , x, no, NO, 0)\n .\n')
def inpStr(mes, douDef=None):
"Inputs a string with default value."
while True:
dline = ing(raw_input(tog(mes)).strip())
if dline == '' and douDef != None: return douDef # Default value
return dline
def inpFiles(mes, suf, nest=False):
"""Gets data files with suffix suf.
Examples:
1. fils = inpFiles(" .asc . * (enter=*) : ", ".asc")
The above gets all the files in current directory (and recursively in the
subdirectories if nest==True)
which have .asc as a suffix: a.asc, thanasis.asc, 1.asc, ...
2. The filenames are transformed to lower, to facilitate windows..
"""
while True:
suf = suf.lower()
fentries = inpStr(mes, "*")
fildats = []; cdir = path(".")
for fentry in fentries.split():
if "*" in fentry or "?" in fentry:
if nest: f = list(cdir.walkfiles(fentry+suf)) # nested subdirectories
else: f = list(cdir.files(fentry+suf)) # only current directory
if len(f) == 0: prg("Warning: no %s files matches '%s'" % (suf, fentry))
fildats.extend(f)
else:
fildats.append(path(fentry+suf))
if len(fildats) > 0: return fildats
prg("Error: No %s files defined or found." % suf)
prg("Try again.")
def inpStrB(mes, douDef=None):
"Inputs a non-blank string with default value."
while True:
dline = ing(raw_input(tog(mes)).strip())
if dline == '' and douDef != None: dline = douDef # Default value
if dline.strip() != '': return dline
prg('\n ( )\n .\n')
def inpDoubleR (mes, douMin, douMax, douDef):
"Inputs a double with default value and range check."
while True:
dou = inpDouble(mes, douDef)
if douMin <= dou <= douMax: return dou
prg('\n :')
prg('%.3f %.3f' % (douMin, douMax))
prg('\n .\n')
def inpLongR (mes, douMin, douMax, douDef):
"Inputs an integer with default value and range check."
while True:
dou = inpLong(mes, douDef)
if douMin <= dou <= douMax: return dou
prg('\n :')
prg('%d %d' % (douMin, douMax))
prg('\n .\n')
#===========================================================================
def medDoubleR (un, mes, douMin, douMax, douDef):
"""
c-----This routine tries to read a double number dou from unit iun.
c If it does and dou is between douMin and douMax -> OK.
c If dou can not be read or is outside douMin-douMax,
c dou takes the default value, and warning message mes is printed.
c This routine is used to read values from file "mediate.tmp". See
c library fildat
"""
dline = un.readline()
if dline != "": # not end of file
try:
dou = float(dline.strip())
if douMin <= dou <= douMax: return dou
except:
pass
prg("%s %.3f" % (mes, douDef)) # Use the default value and print message
return douDef
def medLongR (un, mes, douMin, douMax, douDef):
"""
c-----This routine tries to read a long number dou from unit iun.
c If it does and dou is between douMin and douMax -> OK.
c If dou can not be read or is outside douMin-douMax,
c dou takes the default value, and warning message mes is printed.
c This routine is used to read values from file "mediate.tmp". See
c library fildat
"""
dline = un.readline()
if dline != "": # not end of file
try:
dou = int(dline.strip())
if douMin <= dou <= douMax: return dou
except:
pass
prg("%s %d" % (mes, douDef)) # Use the default value and print message
return douDef
def medStr(un, mes, douDef):
"""
c-----This routine tries to read a string dou from unit iun.
c If it does and dou is not blank -> OK.
c If dou can not be read or is blank,
c dou takes the default value, and warning message mes is printed.
c This routine is used to read values from file "mediate.tmp". See
c library fildat
"""
dline = un.readline()
if dline != "": # not end of file
dline = dline.strip()
if dline != "": return dline
prg("%s %s" % (mes, douDef)) # Use the default value and print message
return douDef
def test():
"Tests the functions."
akl = inpDoubleR(' (return=500) : ', 1.0e-10, 1.0e10, 500.0)
print "akl=", akl
if __name__ == "__main__": test()
|