Hello Ecto!¶
The beginning. Well, you should have read Using ecto in your own projects first. Your ecto experience will involve a hybrid approach to development, using c++ and Python.
CMake¶
You’ll need a CMake file named CMakeLists.txt
srcs/CMakeListsHello.txt
# This is standard CMake, you need that line
cmake_minimum_required(VERSION 2.8)
# This just defines the name of that project from CMake
project(hello_ecto)
# You need ecto, so find it first: that will give you access to macros
find_package(ecto REQUIRED)
# This is all you need to know: it will create an ecto module named tutorial
# and it will be built in the ${where_your_Python_lib_are_bult}/ecto_tutorial folder.
# If you add the ``INSTALL`` flag, it will also install it when you do ``make install`
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/ecto_tutorial)
ectomodule(tutorial DESTINATION ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} INSTALL
# You need a file defining the module (cf. below)
tutorial.cpp
# You need a file defining some content for module (cf. below)
Hello.cpp
)
c++¶
So, the c++ side of things. Ecto code structurally break down into Cells and Modules
The module code:
Download:
srcs/tutorial.cpp
#include <ecto/ecto.hpp> //Defines a top level ecto python cell with the name 'tutorial' //Note that the shared library must be called tutorial.so //In CMake this is achieved using the ecto specific macro: //ectomodule(tutorial INSTALL // DESTINATION whatever_folder // tutorial.cpp // <source1.cpp> // <source2.cpp> //) ECTO_DEFINE_MODULE(tutorial) { }
The cell code:
Download:
srcs/Hello.cpp
#include <ecto/ecto.hpp> #include <iostream> namespace tutorial { using ecto::tendrils; struct Hello { /* We only a process function for now, and it just prints somethin * Its inputs and outputs are ignored in that case */ int process(const tendrils& /*in*/, const tendrils& /*out*/) { std::cout << "Hello" << std::endl; return ecto::OK; } }; } // This macro is required to register the cell with the module // first argument: the module that was defined in the tutorial.cpp // second argument: the cell we want to expose in the module // third argument: the name of that cell as seen in Python // fourht argument: a description of what that cell does ECTO_CELL(tutorial, tutorial::Hello, "Hello", "Prints 'Hello' to standard output.");Our first example.
As you can see in Hello.cpp this is one of the most basic ecto cells.
python¶
The python counter part to hello might look like:
Download: srcs/hello.py
#!/usr/bin/env python
# we import out ecto module: it was built in the ecto_tutorial folder and it is named tutorial
import ecto_tutorial.tutorial as tutorial
# we get the cell that we named Hello in that module
printer = tutorial.Hello()
# execute the cell itself
printer.process()
The Ecto
module is imported just like any Python module so make sure you have an __init__.py
in the same folder.
Let’s run it¶
Well first, you need to build your module and your cell. If you put the 4 files above in a common folder,
(and make sure you renamed CMakeListsHello.txt
to CMakeLists.txt
), just type:
mkdir build && cd build && cmake ../ && make && cd ..
Now, ecto
created a Python module so it needs to be added to your Python path:
export PYTHONPATH=`pwd`/build:$PYTHONPATH
Also, do not forget this standard Python: if you have a folder containing Python modules, for it to become a proper package,
you need a file named __init__.py
in it so:
touch ./build/ecto_tutorial/__init__.py
Now, make your script executable and run it:
chmod 755 hello.py && ./hello.py
No module named PySide.QtCore
Hello