EvalString - Maple Help

Python

 EvalString
 parse and evaluate a Python statement

 Calling Sequence EvalString(cmd) EvalString(cmd,output=mode)

Parameters

 cmd - string mode - identical(none,python,maple)

Description

 • The EvalString command sends the given string to be executed by a Python interpreter.
 • By default the given cmd is prepended with syntax to form a temporary variable assignment so that the result can be fetched and sent back to Maple as a reference or converted data structure.  This is the default output=maple mode.
 • To avoid data-structure conversions use output=python.  With this option, even trivial conversions like integer results will remain as Python object references.  More typically this output mode is used for very large results that need to be passed to another Python function, but will not otherwise be used directly in Maple.
 • The output=none option is required for statements where it is syntactically invalid to be prefixed with a temporary variable assignment.  This includes procedure declarations, import statements, and multi-line scripts among other things.
 • Evaluating Python commands is stateful.  Variables assigned in previous commands remain assigned and can be used in subsequent commands until a Maple restart is executed.
 • This function is part of the Python package, so it can be used in the short form EvalString(..) only after executing the command with(Python). However, it can always be accessed through the long form of the command by using Python[EvalString](..).

Examples

 > $\mathrm{with}\left(\mathrm{Python}\right):$

A simple evaluation:

 > $\mathrm{EvalString}\left("1+1"\right)$
 ${2}$ (1)

Remember ^ means bitwise XOR in Python:

 > $\mathrm{EvalString}\left("2^10"\right)$
 ${8}$ (2)

Access something built in

 > $\mathrm{ImportModule}\left("statistics"\right)$
 > $\mathrm{EvalString}\left("statistics.median_grouped\left(\left[1, 3, 3, 5, 7\right], interval=2\right)"\right)$
 ${3.50000000000000}$ (3)

When defining a procedure make sure you use the output=none option

 > $\mathrm{EvalString}\left("def mysum\left(a,b\right): return a+b",\mathrm{output}=\mathrm{none}\right)$
 > $\mathrm{EvalString}\left("mysum\left(1,2\right)"\right)$
 ${3}$ (4)

Indentation matters; use \n for newlines and spaces or \t for tabs

 > $\mathrm{EvalString}\left("def flatten\left(lst\right):\n\treturn sum\left( \left(\left[x\right] if not isinstance\left(x, list\right) else flatten\left(x\right)\n\tfor x in lst\right), \left[\right] \right)",\mathrm{output}=\mathrm{none}\right):$
 > $\mathrm{EvalString}\left("flatten\left(\left[\left[1\right],2,\left[\left[3\right],4,5\right]\right]\right)"\right)$
 $\left[{1}{,}{2}{,}{3}{,}{4}{,}{5}\right]$ (5)

Multiple statements can be evaluated at the same time, but the result returned to Maple is from the FIRST statement, not the last.

 > $\mathrm{EvalString}\left("a=1\nb=2\na+b"\right)$
 ${1}$ (6)

It is recommended to use output=none when evaluating multiple statements at the same time

 > $\mathrm{EvalString}\left("a=1\nb=2\nr=a+b",\mathrm{output}=\mathrm{none}\right)$
 > $\mathrm{EvalString}\left("r"\right)$
 ${3}$ (7)
 > $\mathrm{GetVariable}\left("r"\right)$
 ${3}$ (8)

To avoid automatic data-structure conversion use output=python

 > $\mathrm{emptystring}≔\mathrm{EvalString}\left("\text{'}\text{'}",\mathrm{output}=\mathrm{python}\right)$
 ${\mathrm{emptystring}}{≔}{""}$ (9)
 > $\mathrm{emptystring}:-\mathrm{join}\left(\left["a","b","c"\right]\right)$
 ${"abc"}$ (10)

Compatibility

 • The Python[EvalString] command was introduced in Maple 2018.