This example illustrates how Python code can access data types written in the Xc++ language. See also the documentation of the cxPython library.
The Shape, Time and Pizza libraries are packaged into a Python Wheel file named:
pypizza-2.0.0-cp27-cp27m-win_amd64.whl
during the install phase of CMake.
This is achieved with the following statements in MyCompany/CMakeLists.txt :
set(PIZZA_TARGETS Time Shapes Pizza)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pypizza_package/pypizza/lib)
# Copy setup.py.in to pypizza_package/setup.py, allowing cmake variables like ${PROJECT_VERSION} to be used
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/setup.py.in ${CMAKE_CURRENT_BINARY_DIR}/pypizza_package/setup.py)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/pypizza_package DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
foreach(tgt ${PIZZA_TARGETS})
add_custom_command(TARGET ${tgt} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${tgt}> ${CMAKE_CURRENT_BINARY_DIR}/pypizza_package/pypizza/lib)
endforeach()
install(
CODE "execute_process( COMMAND ${PYTHON} setup.py bdist_wheel WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pypizza_package )"
CODE "file( GLOB wheelFiles \"${CMAKE_CURRENT_BINARY_DIR}/pypizza_package/dist/*.whl\" )"
CODE "execute_process( COMMAND ${PYTHON} -m pip install --upgrade --no-deps --force-reinstall \${wheelFiles} )"
)
This makes use of the file MyCompany/cmake/setup.py.in and the following files under the MyCompany/pypizza_package folder:
MyCompany/pypizza_package
For convenience this installs the pypizza wheel file for you, by running the following command:
python -m pip install --upgrade --no-deps --force-reinstall pypizza-2.0.0-cp27-cp27m-win_amd64.whl
As a simple check that the pypizza package has been installed correctly, do the following
Run the Python Interpreter by running the command 'python' in a terminal:
Import pypizza and pyceda and take dir(pyceda.rootnamespace) and check that the namespaces 'dt', 'pizza' and 'shapes' have been added under the root namespace.
>>> import pypizza >>> import pyceda >>> dir(pyceda.rootnamespace) ['FindChildNameSpace', 'FindElement', 'FindElementWithRelativePath', 'GetChildNameSpaceIterator', 'GetElementIterator', 'GetName', 'Int64', 'PyObject', 'UInt64', 'ceda', 'dt', 'pizza', 'shapes'] >>>
Type an end-of-file character (Control-D on Unix, Control-Z on Windows) then enter to exit the Python Interpreter.
Some sample python scripts are available under the folder MyCompany/python:
MyCompany/python ├── pypizza_package │ ├── pypizza │ │ └── __init__.py │ ├── LICENSE │ ├── README.md │ └── setup.py ├── CMakeLists.txt ├── datetime_example.py ├── generate_pizza_jni_wrappers.py ├── open_and_close_database_example.py ├── pizza_example.py └── shapes_example.py
You can run these sample python scripts yourself.
For example, in a terminal run datetime-example.py as follows:
python datetime-example.py
The following outputs are generated by these scripts
Script | Description | Output |
---|---|---|
open_and_close_database_example.py | Illustrates creating an (empty) database | No output is printed, a 512kB file named 'mydatabase' is created |
datetime_example.py | Illustrates use of the date and time data types defined in the Time library | output |
shapes_example.py | Illustrates use of the data types defined in the Shapes library | output |
pizza_example.py | Illustrates use of the data types defined in the Pizza library | Writes 512kB database files named 'mypizzadatabase' and 'myreplicatedpizzadatabase'. The output generated changes each time it is run: first , second , third |