| ePolyglot: Examination and development of multilanguage programming using Eiffel, Python, and Haskell | ||
|---|---|---|
| Prev | Chapter 8. Simple use of the embedded Python interpreter | Next |
The following sample program, located in production/examples/simple, shows the sort of scripting possible with these two methods. It uses py_run_string to display a message and create and display a dictionary, then uses evaluated_expression to create the same dictionary as an Eiffel-based PYTHON_DICTIONARY:
Example 8-6. The simple_test.e program
class SIMPLE_TEST
inherit
PYTHON_CLIENT
creation
make
feature
make is
local
a : PYTHON_DICTIONARY
r : INTEGER
do
py_initialize
std_output.put_string( "The Python interpreter is available %
%from within Eiffel for simple tasks,%N%
%including evaluation of code into Eiffel-%
%compatible PYTHON_OBJECTs!%N" )
std_output.put_string( "Printing a message from within Python%N" )
r := py_run_simple_string( "print 'Hello, simple interpreter world!'" )
r := py_run_simple_string( "a = { 'a' : 'Hello', 'b' : 'World!' }" )
r := py_run_simple_string( "print 'Creating and showing a dictionary from %
%within Python: '" )
r := py_run_simple_string( "print a" )
create a.make_from_python_object( evaluated_expression( "{ 'a' : 'Hello', 'b' : 'World!' }" ) )
std_output.put_string( "Creating and showing the same dictionary from %
%within Eiffel:%N" + a.to_string + "%N" )
rescue
if not py_err_occurred.is_null then
py_err_print
end
end
end
|
It provides the output shown below:
Example 8-7. Output from the simple_test.e program
The Python interpreter is available from within Eiffel for simple tasks,
including evaluation of code into Eiffel-compatible PYTHON_OBJECTs!
Printing a message from within Python
Hello, simple interpreter world!
Creating and showing a dictionary from within Python:
{'b': 'World!', 'a': 'Hello'}
Creating and showing the same dictionary from within Eiffel:
{'b': 'World!', 'a': 'Hello'} |