scallop dome pyformex logo

Previous topic

3. arraytools — A collection of numerical array utilities.

Next topic

5. draw — Create 3D graphical representations.

[FSF Associate Member]

Valid XHTML 1.0 Transitional

4. script — Basic pyFormex script functions

The script module provides the basic functions available in all pyFormex scripts. These functions are available in GUI and NONGUI applications, without the need to explicitely importing the script module.

Classes defined in module script

Functions defined in module script

script.Globals()

Return the globals that are passed to the scripts on execution.

When running pyformex with the –nogui option, this contains all the globals defined in the module formex (which include those from coords, arraytools and numpy.

When running with the GUI, this also includes the globals from gui.draw (including those from gui.color).

Furthermore, the global variable __name__ will be set to either ‘draw’ or ‘script’ depending on whether the script was executed with the GUI or not.

script.export(dic)

Export the variables in the given dictionary.

script.export2(names, values)

Export a list of names and values.

script.forget(names)

Remove the global variables specified in list.

script.forgetAll()

Delete all the global variables.

script.rename(oldnames, newnames)

Rename the global variables in oldnames to newnames.

script.listAll(clas=None, like=None, filtr=None, dic=None, sort=False)

Return a list of all objects in dictionay that match criteria.

  • dic: a dictionary object, defaults to pyformex.PF
  • clas: a class name: if specified, only instances of this class will be returned
  • like: a string: if given, only object names starting with this string will be returned
  • filtr: a function taking an object name as parameter and returning True or False. If specified, only objects passing the test will be returned.

The return value is a list of keys from dic.

script.named(name)

Returns the global object named name.

script.getcfg(name)

Return a value from the configuration.

script.ask(question, choices=None, default='')

Ask a question and present possible answers.

If no choices are presented, anything will be accepted. Else, the question is repeated until one of the choices is selected. If a default is given and the value entered is empty, the default is substituted. Case is not significant, but choices are presented unchanged. If no choices are presented, the string typed by the user is returned. Else the return value is the lowest matching index of the users answer in the choices list. Thus, ask(‘Do you agree’,[‘Y’,’n’]) will return 0 on either ‘y’ or ‘Y’ and 1 on either ‘n’ or ‘N’.

script.ack(question)

Show a Yes/No question and return True/False depending on answer.

script.error(message)

Show an error message and wait for user acknowlegement.

script.system(cmdline, result='output')

Run a command and return its output.

If result == ‘status’, the exit status of the command is returned. If result == ‘output’, the output of the command is returned. If result == ‘both’, a tuple of status and output is returned.

script.playScript(scr, name=None, filename=None, argv=[], pye=False)

Play a pyformex script scr. scr should be a valid Python text.

There is a lock to prevent multiple scripts from being executed at the same time. This implies that pyFormex scripts can currently not be recurrent. If a name is specified, set the global variable pyformex.scriptName to it when the script is started. If a filename is specified, set the global variable __file__ to it.

script.breakpt(msg=None)

Set a breakpoint where the script can be halted on a signal.

If an argument is specified, it will be written to the message board.

The exitrequested signal is usually emitted by pressing a button in the GUI.

script.stopatbreakpt()

Set the exitrequested flag.

script.convertPrintSyntax(filename)

Convert a script to using the print function

script.checkPrintSyntax(filename)

Check whether the script in the given files uses print function syntax.

Returns the compiled object if no error was found during compiling. Returns the filename if an error was found and correction has been attempted. Raises an exception if an error is found and no correction attempted.

script.runScript(fn, argv=[])

Play a formex script from file fn.

fn is the name of a file holding a pyFormex script. A list of arguments can be passed. They will be available under the name argv. This variable can be changed by the script and the resulting argv is returned to the caller.

script.runAny(appname=None, argv=[], step=False, refresh=False)

Run the current pyFormex application or script file.

This function does nothing if no appname/filename is passed or no current script/app was set. If arguments are given, they are passed to the script. If step is True, the script is executed in step mode. The ‘refresh’ parameter will reload the app.

script.exit(all=False)

Exit from the current script or from pyformex if no script running.

script.quit()

Quit the pyFormex program

This is a hard exit from pyFormex. It is normally not called directly, but results from an exit(True) call.

script.processArgs(args)

Run the application without gui.

Arguments are interpreted as names of script files, possibly interspersed with arguments for the scripts. Each running script should pop the required arguments from the list.

script.setPrefs(res, save=False)

Update the current settings (store) with the values in res.

res is a dictionary with configuration values. The current settings will be update with the values in res.

If save is True, the changes will be stored to the user’s configuration file.

script.printall()

Print all Formices in globals()

script.isWritable(path)

Check that the specified path is writable.

BEWARE: this only works if the path exists!

script.chdir(path, create=False)

Change the current working directory.

If path exists and it is a directory name, make it the current directory. If path exists and it is a file name, make the containing directory the current directory. If path does not exist and create is True, create the path and make it the current directory. If create is False, raise an Error.

Parameters:

  • path: pathname of the directory or file. If it is a file, the name of the directory holding the file is used. The path can be an absolute or a relative pathname. A ‘~’ character at the start of the pathname will be expanded to the user’s home directory.
  • create: bool. If True and the specified path does not exist, it will be created. The default is to do nothing if the specified path does not exist.

The changed to current directory is stored in the user’s preferences for persistence between pyFormex invocations.

script.pwdir()

Print the current working directory.

script.mkdir(path)

Create a new directory.

Create a new directory, including any needed parent directories.

  • path: pathname of the directory to create, either an absolute or relative path. A ‘~’ character at the start of the pathname will be expanded to the user’s home directory. If the path exists, the function returns True without doing anything.

Returns True if the pathname exists (before or after).

script.mkpdir(path)

Make sure the parent directory of path exists.

script.runtime()

Return the time elapsed since start of execution of the script.

script.startGui(args=[])

Start the gui

script.checkRevision(rev, comp='>=')

Check the pyFormex revision number.

  • rev: a positive integer.
  • comp: a string specifying a comparison operator.

By default, this function returns True if the pyFormex revision number is equal or larger than the specified number.

The comp argument may specify another comparison operator.

If pyFormex is unable to find its revision number (this is the case on very old versions) the test returns False.

script.requireRevision(rev, comp='>=')

Require a specified pyFormex revision number.

The arguments are like checkRevision. Ho9wever, this function will raise an error if the requirement fails.

script.writeGeomFile(filename, objects, sep=' ', mode='w', shortlines=False)

Save geometric objects to a pyFormex Geometry File.

A pyFormex Geometry File can store multiple geometrical objects in a native format that can be efficiently read back into pyformex. The format is portable over different pyFormex versions and even to other software.

  • filename: the name of the file to be written. If it ends with ‘.gz’ the file will be compressed with gzip. If a file with the given name minus the trailing ‘.gz’ exists, it will be destroyed.
  • objects: a list or a dictionary. If it is a dictionary, the objects will be saved with the key values as there names. Objects that can not be exported to a Geometry File will be silently ignored.
  • mode: can be set to ‘a’ to append to an existing file.
  • sep: the string used to separate data. If set to an empty string, the data will be written in binary format and the resulting file will be smaller but less portable.

Returns the number of objects written to the file.

script.readGeomFile(filename)

Read a pyFormex Geometry File.

A pyFormex Geometry File can store multiple geometrical objects in a native format that can be efficiently read back into pyformex. The format is portable over different pyFormex versions and even to other software.

  • filename: the name of an existing pyFormex Geometry File. If the filename ends on ‘.gz’, it is considered to be a gzipped file and will be uncompressed transparently during the reading.

Returns a dictionary with the geometric objects read from the file. If object names were stored in the file, they will be used as the keys. Else, default names will be provided.