Skip to content

Converting Open GENIE to genie_python

Liam edited this page Nov 7, 2018 · 4 revisions

Scripting > Converting Open GENIE to genie_python

Converting Open GENIE to genie_python

If you know a little bit of Python already, converting Open GENIE scripts to genie_python is very often straightforward. Even if you're new to Python, once you know a few simple pieces of syntax, you'll be able to convert many scripts without issue.

Note: If you are new to Python, consult the Introduction to Python on the Mantid web-site.

List of functions

We've included some common genie_python commands on this page, but for a full list refer to the `genie\_python reference manual`_.

Indentation

One thing where there is no direct comparison between Python and Open GENIE is with indentation. In Python, different code blocks are identified by their indentation level. In many programming languages, code blocks are encased in curly braces ({...}), but Python uses indentation. For example:

print "No indent"
def my_func():
    print "1 indent. Inside the function"
    if True:
        print "2 indents. Inside the if clause"
        print "2 indents. Still inside the if clause"
    print "1 indent. In the function but not the if clause"
print "No indent. Outside the function"

Typically an indent is 3 or 4 spaces. Tabs can be used too, but Python won't recognise that a tab is the same as 4 spaces so can lead to problems. It's often best to avoid them. This might be a new concept if you've only ever written Open GENIE, but trust us in that it opens up a lot of ways to make scripts more powerful, easier to read and more reliable.

Side-by-side comparison

Open GENIE syntax genie_python syntax Comments
PROCEDURE my\_func def my\_func(): This is the standard way to define a function in Python
begin g.begin() Many functions in genie_python have the same name as in Open GENIE. In these cases, prepend g. to indicate the method belongs to genie_python and append () to tell Python to execute the method with no arguments
my\_func\_2 100 my\_func\_2(100) To execute a function, give its name and then the argument in brackets. This is a user-defined function, not a genie_python function, so it has no g. in front.
my\_func\_3 100 200 my\_func\_2(100,200) As above, except that multiple arguments are separated by a comma
waitfor seconds=20 g.waitfor(seconds=20) Some methods can take named arguments. In these cases you simply give named arguments in the form [name]=[value]. Note that you can mix named and unnamed arguments, but unnamed arguments always appear at the start of the argument list. This section gives a more detailed description
cset/nocontrol MY_BLOCK=value g.cset('MY_BLOCK',value,runcontrol=False) Some Open GENIE commands take optional arguments. The same principle applies as in the above example.
# This is a comment # This is a comment Comments are the same in both languages
Clone this wiki locally