Adding python script to c++ project - c++

How would I go about adding a script written in python to a c++ project? Thanks
Edit: Basically all the script does is email some data. I would like to pass the data and maybe the email address to a function written in python. Hope that clears things up..

You could look at Boost.Python which is a "a C++ library which enables seamless interoperability between C++ and the Python programming language."
You have to be more specific, though.

You may be interested in Boost.Python: Embedding the Interpreter, or Python/C API: Embedding the Python Interpreter. You can either use the Python C APIs directly or use the Boost.Python library as you so choose. You might also be interested in reading Embedding Python in Your C Programs which walks you through it.

The most primitve solution would be to use the system command to call you script, but that does limit your control over it to setting environment variables and passing parameters.
system("myscript.py param1 param2")

Related

Sublime for python's Ctrl+Space function?

I have been using sublime-text to write my python codes, but have just realized there is a very useful Ctrl+Space command we can use in the default python IDLE, which makes me re-consider where to write the code again.
Is there any way sublime can do this? Or is there any other text editor that does it?
Check out the Anaconda plugin for ST3 (not related to the Anaconda Python distribution at all). It does a whole bunch of stuff, including intelligent autocomplete based on an object's type, as well as autocompleting methods of imported modules, along with other functions like linting, showing documentation, finding usage of an object, etc. It only works with Python, but really, what other languages are worth programming in? :)
Setup is pretty straightforward, and allows for you to have different configurations (and different Python interpreters, even virtualenvs) on a per-project basis. I've been using this plugin for quite a while, and I absolutely love it. It's much faster and more accurate than SublimeCodeIntel, and combines functions of several other plugins all in one place.

OpenFrameworks + Python

Is there any bindings to execute functions in OpenFrameworks (C++ toolkit) using Python 2.7? Or any alternative for this available?
Experiments on python 2.6 has been made, see #Babu answer. But not updates..
I don't know a lot about it but you can try to have a look at SIP (even i think it is an hard task to let it work completely):
SIP is a tool for quickly writing Python modules that interface with
C++ and C libraries. Its home page is at
http://riverbankcomputing.co.uk/software/sip/intro. It was written by
Phil Thompson who is still actively maintaining it.
I don't think there is a binding available for Python 2.7. For 2.6, it's available here,
For Windows: http://forum.openframeworks.cc/index.php/topic,3031.0.html
For OSX: http://forum.openframeworks.cc/index.php/topic,2763.0.html
I am also very interested by Python bindings, but it seems quite hard to implement...
That's why things like cppyy could be really handy! I didn't have a close look yet, but it's definitely on my todo list.
Interesting thing to checkout when working with OpenFrameWorks + Python is using BeautifulSoup to parse HTML and use that in OPenFrameworks to visualize the informations.

Python3 or C/C++: TWAIN interface

I have seen twain module for Python2, is there anything similar for Python3?
I need to control scanning process and, specifically, histogram settings (shadow, highlight, gamma), resolution and scanning window size. What would be the best way to automate it in Python3?
EDIT: If not in Python, are there any C/C++ libraries which could be integrated into Python and control scanning process? I know only one library, EZTwain, it supports multiple programming languages (excluding Python) but it didn't work for some histogram settings and there was no support even for commercial version, so I would avoid using it.
We use a third-party toolkit named leadtools that might help you. This toolkit supports the most common programming languages including C/C++, C#, VB.NET and so more. For more information, you can check these 2 help topics:
For .NET:
Acquiring an Image using .NET
For C++:
Acquiring an Image using C++

Using C compiled code from python GUI

I am writing an application where I am coding all of my front-end and GUI with python library (wxpython specifically). For this application, I would like to write the model class with C and use python to use the compiled C code? How can this be implemented in python?
I know this is little vague question but I am struggling with the starting point.
If you're using CPython (the most popular version of Python), you'll need to learn the CPython C API. Other python implementations may or may not support C calls. You can also use the ctypes library which is easier to learn, but also more rigid and may not support everything you need.
You can try Cython, which is basically Python with embedded support for C types and calls (it goes through the CPython API).
You could strictly seperate design(python part) and code(c++ part) like this:
Write a complete c++ programm that works in the terminal/console and then make the python-application call these c++-terminal programm via os.Popen.
So if your programm is a calculator it does this:
(python gui) 5 + 5 -> my_c_programm.exe "5 + 5" -> (returns) 10 -> (python gui) display
that way you can use your programm with and without gui.
Its easier and faster than embedding Python in your c++ programm or extending Python with C++.
I basically do the same thing on my current project, but like this:
php: my webinterface
python: for structure and logic and easy operation
c++: for heavy calculations and where I need speed
so php -> python -> c++
and it works very well for me :)

C/C++ Python interpreter

What I'm trying to do is write an app in C/C++ which will allow users to enter a Python script, which the app will then interpret & run. The aforementioned script will have a separate API that I'll implement in C, then expose to the user.
Is there a way to do this? I've searched on Google, but I've only found ways to create 'extensions' and not actual interpreters (like Lua allows).
Documentation about Embedding Python in Another Application says:
The previous chapters discussed how to extend Python, that is, how to extend the functionality of Python by attaching a library of C functions to it. It is also possible to do it the other way around: enrich your C/C++ application by embedding Python in it. Embedding provides your application with the ability to implement some of the functionality of your application in Python rather than C or C++. This can be used for many purposes; one example would be to allow users to tailor the application to their needs by writing some scripts in Python. You can also use it yourself if some of the functionality can be written in Python more easily.
Look especially into Extending Embedded Python:
Until now, the embedded Python interpreter had no access to functionality from the application itself. The Python API allows this by extending the embedded interpreter. That is, the embedded interpreter gets extended with routines provided by the application.
Just read the docs I referenced and you should be able to implement Python interpreter within your app written in C.
You can do a call from C++ to Python and vice versa using Boost Python
If you're interested I recently implemented embedded Python scripting for C++ from first principles. I created a C++ Python wrapper library called ECS:Python (Embedded C++ Scripting with Python) that allows you to expose object from a C++ application to an embedded Python interpreter for interactive scripting. It's light-weight and very easy to use (and free!).
http://sourceforge.net/projects/ecspython