-
Notifications
You must be signed in to change notification settings - Fork 0
pythonizing dont know python
Don't know Python?
No problem!
We're really only using a little Python, but there are a few things you need to know.
That being said, everything inside the mpi-binding
block must be in valid Python syntax. If the syntax isn't correct, building the LaTeX document will fail.
Here's a few "gotchas" that you need to watch out for, particularly if you're not familiar with the Python language:
Python requires a constant level of indenting. Whatever level you indent to, make sure that all of your initial lines are indented to the same level.
# Good indenting
foo()
bar()
# Bad indenting (will cause an error)
baz()
yow()
You probably won't use any conditionals or have any reason for sub-blocks of code in the MPI standard Pythonizing, but if you do, you use indenting as the delimiter for that block of code. For example:
foo()
if value == 3:
print("Hey, the value was 3!")
value = 0
print("I reset the value to 0 for you")
# Outdented, so now we're back outside the conditional code block
print("The value is: " + value)
Strings can be delimited with single or double quote marks: '
or "
. For example:
# Like several scripting languages, you do not need
# to declare variables before using them:
foo = 'Hello, world'
bar = "Hello, world"
Multi-line strings can be delimited with three single or double quote marks:
foo == """Hello,
world"""
bar = '''Hello,
world.
I am awesome.
This is a very long,
multi
line
string.'''
Note that inside the string, indenting is irrelevant. But the string expression -- the assignments to foo
and bar
in this case -- must follow proper Python indenting.
Line breaks are allowed inside of ()
. For the purposes of the MPI standard, this will likely occur within the argument list to one of the MPI Pythonizing functions. Additionally, inside ()
, the indenting rules are relaxed: you can indent inconsistently. For example:
# Here's a non-line broken line
foo(a, b, c)
# Here's a line with a line break
foo(a,
b,
c)
# Note that the indenting does not need to be consistent within the ()
some_reall_long_function_name(a,
b, c, d, e, f,
g, h, i, j, k)
Comments are generally of two flavors: dedicated comments or useless strings.
Dedicated comments start with the #
symbol, like many other scripting languages. For example:
# This is a comment line
foo() # This is a comment at the end of a line
For lengthy, multi-line comments, some Python programmers like to have a useless multi-line string. I.e., it's a valid string, but it evaluates to no purpose when executing the Python. For example:
"""This is a multi-line string
You can do whatever you want in this string
Including indenting inconsistently.
Hello, world!"""
foo()
But note that the string is a valid Python expression, so it must be indentend consistently with other Python expressions. In the example above, note how the opening """
and foo()
are indented to the same level.
Here's a slightly-unrealistic example that shows everything from above:
\begin{mpi-binding}
"""This is the binding for MPI_ALLOC_MEM.
It is special in many ways.
But really, I just wanted to show a multi-line string that acts like a
comment"""
functionname('MPI_Alloc_mem')
# Use the special ALLOC_MEM_NUM_BYTES type
# See comment in binding_tool.py for an explanation
parameter('size', 'ALLOC_MEM_NUM_BYTES',
desc='size of memory segment in bytes')
parameter('info', 'INFO')
parameter('baseptr', 'C_BUFFER', direction='out',
desc='''pointer to beginning of memory
segment allocated''')
\end{mpi-binding}