tio
index
/Users/wjh/GitHub/Python/modules/tio.py

The tio package is a set of terminal input methods to read the  built in Python types plus Vector2d and Vector3d classes.The packages formats prompts, give defaults, does range checking and general sanity checking. All will re-prompt on errors.
 
There is also a simple internal command handler and journal facility which is being developed.
 
Author:   Will Hossack, The University of Edinburgh

 
Modules
       
cmath
math
sys

 
Functions
       
getBool(prompt, default=None)
Read a logical boolen from the terminal with checking. 
It will accept: yes / no , true / false in lower and upper case,  1 / 0 or any logical expression
prompt string the prompt to be displayed
default boolean logical (may be None)
returns boolean True/False
 
Note: if response is a string it will be evaluated.
getComplex(prompt, default=None, maxabs=inf)
Read a complex from the terminal with optional defaul and range checking.
prompt string the prompt to be displayed.
param default complex the default (may be None)
param maxabs float maximum abs (defaults to float("Inf"))
returns complex in specified range (if imaginary part is zero, it will still retun a complex)
getExpandedFilename(name)
Method to expand a filename and process environmental variable in $env or ~username prefix to a filename. 
param name string with original name, assumed to contains NO leading white spaces
returns string the expanded filename.
 
Typical input is $HOME/data.data or ~fred/data.dat, where $HOME is env name and fred is username.
getFloat(prompt, default=None, min=-inf, max=inf)
Read a float from the terminal with optional default and range checking.
param prompt the prompt string to be displayed
param default float (defaults to None)
param min float min value accepted (defaults to -Inf)
param max float max value accepted (defaults to Inf)
returns float in specified range.
 
Note: if response is a string it will be evaluated.
getInt(prompt, default=None, min=-9223372036854775808, max=9223372036854775807)
Read an int from the terminal with optional default and range checking. This will accept decimal,
but also also binary (prefix 0b) , oct (prefix 0o) , hex prefix (0x)
param prompt string the prompt string to be displayed.
param default int (defaults no None)
param min int min value accepted (defaults to -sys.maxint - 1) 
paramfloat max value, (defaults to -sys.maxin
return int in specified range.
 
Note: if response is a string it will be evaluated.
getOption(prompt, options, default=None)
Method to get a choice of options from a supplied list with error checking.
para prompt string the prompt to be displayed.
param options [] the options list assumed to be list of strings.
default int the default option (int in range 0 -> < len(options)) (default to None)
returns list [] of (opt,option[opt])
 
Each option is tested for existance and uniquness. It will fail an re-prompt as required. There is also\
a simple internal 'help' option that pints the list of options.
getString(prompt, default=None)
Read a string from the terminal with no processing or evaluation. 
param prompt string the prompt to be displayed.
param default the default string (defaults to None)
returns string with no processing, but with leading and trailing white space removed.
 
Note: will allow a zero length string to be returned.
getVector2d(prompt, default=None, maxabs=inf)
Read a Vector2d form the module vector from the terminal with checking.
Format from terminal may be 'x,y'   OR   '[x,y]',  also each componet will be evaluated.
prompt string the prompt to be displayed
default Vector2d the default value (may be None)
maxabs float maximum absolutle value of the Vector2d, defaults to float("Inf")
returns a Vector2d
getVector3d(prompt, default=None, maxabs=inf)
Read a Vector3d form the module vector from the terminal with checking.
Format from terminal may be 'x,y,z'   OR   '[x,y,z]',  also each componet will be evaluated.
prompt string the prompt to be displayed
default Vector3d the default value (may be None)
maxabs float maximum absolutle value of the Vector3d, defaults to float("Inf")
returns a Vector3d
openFile(prompt, key='r', defaulttype=None, defaultname=None)
Method to open a text file with sanity checking with re-prompt on failure.
param prompt string the prompt to be displayed.
param key string the mode as used by open, default is "r" (read)
param defaultype deafult extension which will be added if not supplied.
param  defaultname string the default filename (default to None)
returns the opened filestream
 
The file names is processded to expand environmental variable and user names    so for example $ENV/dir/file.data or ~user/dir/file.data are expanded
setJournal(filename=None)
Method of open / close a journal file that records prompts and commands typed to a text file.
param filename string the name of the journal file, if None will close any current open journal file.
tprint(*args)
    Simply alternative to print that will print to the sysout and also journal if there is a journal file open.
    Output to the journal file be prefixed with a comment character
    param, argument list, each will be conveterd to a str() and concatinated to a single string. 
 
    Also  "
" will be appended if not present and the print buffer will be flushed.