Python - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Connectivity : Python : Python/EvalString

Python

  

EvalString

  

parse and evaluate a Python statement

 

Calling Sequence

Parameters

Description

Examples

Compatibility

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

withPython:

A simple evaluation:

EvalString1+1

2

(1)

Remember ^ means bitwise XOR in Python:

EvalString2^10

8

(2)

Access something built in

ImportModulestatistics

EvalStringstatistics.median_grouped([1, 3, 3, 5, 7], interval=2)

3.50000000000000

(3)

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

EvalStringdef mysum(a,b): return a+b,output=none

EvalStringmysum(1,2)

3

(4)

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

EvalStringdef flatten(lst):\n\treturn sum( ([x] if not isinstance(x, list) else flatten(x)\n\tfor x in lst), [] ),output=none:

EvalStringflatten([[1],2,[[3],4,5]])

1,2,3,4,5

(5)

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

EvalStringa=1\nb=2\na+b

1

(6)

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

EvalStringa=1\nb=2\nr=a+b,output=none

EvalStringr

3

(7)

GetVariabler

3

(8)

To avoid automatic data-structure conversion use output=python

emptystringEvalString'',output=python

emptystring<Python object: >

(9)

emptystring:-joina&comma;b&comma;c

abc

(10)

Compatibility

• 

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

• 

For more information on Maple 2018 changes, see Updates in Maple 2018.

See Also

Python

Python:-EvalFunction

type/python