Extending a C++ application with python - python

I have a legacy (but still internally maintained) application written in C++ which handles some hardware, interacts with databases, receives commands via serial line or socket... in short it does a non-trivial amount of work.
This application runs under Linux (ARM/Buildroot).
Now the need is to revamp the control interface adding a RESTful API.
I am exploring the possibility to do so via a Python extension.
Note I am a C++/java programmer and I'm not really proficient in Python, but I know the basics.
General idea would be:
Start a Python interpreter as a thread in the C++ application.
Use Flask/jinja2 (or just plain Bottle) to handle incoming RESTful requests.
Expose a few (perhaps just one) C++ class to Python.
Call from Python the appropriate C++ methods to perform the required actions.
I studied the official documentation (mainly pertaining plain C) and several alternatives, including:
Boost.Python (possibly too heavy for our limited hardware)
pybind11 (seems concerned only in extending Python, not embedding it).
http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I (deals only with embedding, without giving Python access to C++ classes).
Question are:
does this make any sense?
what is the least intrusive way (if any) to achieve this?
which lib/framework is advised to use?
is there some tutorial project along these lines?
I know the question is quite broad, but I hope to narrow it as soon as the first comments will point me in the right direction.

Can you do it the other way around - embed your C++ code in Python program? This way it would be more prepared for moving existing functionality Python like you said.
Python makes a lot of things easier (faster to develop, easier to maintain) - communication with databases, libraries (if hey have Python wrappers), process/thread management... Keep in C++ just the stuff that needs to be in C++, like dealing with hardware, C/C++-only libraries, CPU-heavy code.
Have a look at Cython for embedding C++ in Python. Cython is basically a compiler from Python-like language to a .c/.cpp file that "just" calls the Python C API, so it can be used from "normal" Python code, but can also call other C/C++ code.
Alternative: Implement an API in the C++ application and create a separate Python application that uses this API. By API I don't mean REST API, but something more low-level - RPC, ZeroMQ, plain sockets...

Related

Is it possible to have an application written on Python, but with embedded C++ into it?

I would like to know if i can have a mobile application written on Python(for example, for interface), but with embedded C++ for some tasks or functions of the app? For example, an application written with Kivy Framework for its interface, and with C++ for some AI features. Thanks.
It is possible to write the necessary library in C++ and then use it in Python. Though this is going to require some tricks that you can find online as you make your library. One example of this (since you want to use AI) is TensorFlow; it is used in Python while its core is written in C++

cPython for .NET API

I have build a bunch of python modules for controlling miscellaneous hardware over TCP. All is written for cPython (no .NET capabilities, as far as I understand).
I want to add a new instrument to the pool that comes with a .NET API. See here.
From the distributer of the API, only ironPython is mentioned as option for interfacing with the instrument.
I have no experience with .NET framework or ironPython, so I'm not sure what other options I might have (if any) and which suits my needs best.
My needs are:
I want to use all my old cPython based modules along side with the new instrument and its .NET based API.
Staying in "normal" python would be the most elegant solution to me - but I'm really open for suggestions.
I've ended up porting my modules from cPython to ironPython.
This seemed to be a fairly straightforward job, and I'm pretty happy with the comparability.

Bridge between Delphi or C code and Python script

I have a Delphi Win32 program. I want to "expose" somehow app structures and procedures via Python module. E.g. module my_api must expose public items for my app structures/methods. This module must "sit" in memory only.
Then I want, in the same app, to call Python scripts (using Python dll) which can import my_api and call my app methods.
How to do it.
You're asking for two things here, which often go together.
First, you want to extend the Python interpreter, adding types and functions and so forth that Python code can use.
Second, you want to embed the Python interpreter in your app, so it can run Python scripts (which can use your extension modules).
Assuming you want to use CPython (the usual Python interpreter), the tutorial Extending and Embedding the Python Interpreter is part of the docs.
You may want to look at other options that help make the extending side easier—for example, you can use Cython to write the bridge code in a near-Python language instead of C, or Boost.Python to write it in nice C++ that takes care of most of the boilerplate for you, or SWIG to try to generate it automatically, or ctypes to avoid writing a bridge in the first place. But it's worth learning the underlying mechanism first.
You may have heard of Python 4 Delphi by now, and if you haven't you can look it up here. https://code.google.com/p/python4delphi/. There are quite a few tutorials on the internet e,g http://www.atug.com/andypatterns/pythonDelphiTalk.htm

How to export python functions and reuse them in another app

I was wondering if there is any way to export python functions to dll. There is py2exe and I can successfully create exe file. My program should be used by another program written in delphi (there is possibility of importing dll's in delphi).
So I was wondering what would be the best way to connect those 2 applications.
Now I can only create exe, execute process in delphi and communicate in some way. But I don't think that's nice way. Maybe somebody have any experience in this subject?
There are some pretty big challenges to making languages work well together. As a simple alternative to trying to hook python code directly into delphi, you could consider using something like an xmlrpc server to provide python functionality remotely.
http://docs.python.org/library/xmlrpclib.html
Of course, any protocol could be used; xmlrpc just has some useful server utilities in python and presumably has a client library in delphi.
You can re-use python functions via modules. Integrating with other language is a different task all together.
py2exe packs all dependent modules and additional dlls required by an application so that it can be easily distributed without creating any installation dependencies for the user.
Cross - language integration requires some work. To integrate with "C", there are various ways like cython etc. If there is similar facility available with delphi, you might be able to use it.
Check out some of these references, it will make it more clear to you on what direction to take.
http://wiki.python.org/moin/IntegratingPythonWithOtherLanguages
http://www.atug.com/andypatterns/pythonDelphiTalk.htm
http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=python+delphi (Google search)

Call from Objective-C into Python

bbum posted an outline of how to do this, but I'm unable to complete the details. Where does the Python code go, and how will my Objective-C code know about it? How would I do it compiling on the command line?
Source here:
Calling Python From Objective-C
I have posted a full explanation of how to do this to my weblog as it is quite a bit longer than something I would post here.
The abstract summary remains the same: use an abstract class to provide the type information necessary to make the C compiler happy and the metadata necessary to make the bridge happy.
Unfortunately the story for using Python via PyObjC from within an Objective-C app is not very good at the moment. py2app which ships with PyObjC can compile loadable bundles (i.e. can be loaded via NSBundle), which seems like the best approach: define an NSObject subclass in python that implements a protocol (obtained via objc.protocolNamed) that you define in Objective-C, then compile this python file into a loadable bundle via py2app (which uses a standard setup.py). Unfortunately, py2app hasn't had much love, especially the plugin (loadable bundle) target, and a serious memory leak was introduced sometime around 10.5 such that any data passed from python to Objective-C from a py2app-compiled bundle leaks. Yuck.
PyObjC manipulates the Objective-C runtime in accordance with the ObjC-related code executed in Python, thus to be able to call python code from Objective-C, the general outline goes like
Write PyObjC wrapper around python code
Execute code declaring PyObjC wrapper to add these definitions to the ObjC runtime
Call PyObjC wrapper from Objective-C. Because it's declared at runtime, the symbols aren't available at compile time, so you'll have to use NSClassFromString et al. to instantiate the class. It's helpful to declare a #protocol with the appropriate methods so that the Objective-C compiler doesn't complain about missing methods.
If you have flexibility, the best option is to use the Cocoa-Python app templates (i.e. create a Python app), and then load your Objective-C code as a loadable bundle from within Python. This takes care of managing the Python interpreter for you.
Otherwise, with the code in main.m of the Cocoa-Python app template, you should be able to create a Python interpreter, execute your PyObjC code and then continue on. Obviously, the interpreter needs to be kept running so that your python code can execute, so you'll likely have to do this from a separate thread. As you can see this can get a little hairy. Better to go with the Python app, as described above.
Keep in mind that PyObjC is not guaranteed to play well with the Objective-C garbage collector, so all of these options require that your Objective-C code not use GC.
Google is your friend. Performing a search on the string "Cocoa Python" quickly turned up PyObjc.

Categories

Resources