Run Python script inside C++ with Python.h - c++

I'm currently aware of this turotial, but I don't see anything in the tutorial that just simply runs a script. If I have a file pySolve.py how can I just call it to be executed inside of my code? No input is required as the C++ end generates all files needed before calling the python solve script.

You need to call PyRun_File or one of its variants. And of course first you must call Py_Initialize. You can see example usage of these functions in my open-source project here: https://github.com/jzwinck/pccl/blob/master/run.cpp

Related

How to get C++ function names in hg diffs?

I would like to get the C++ function names in a file. I tried do this with the command diff because I only need get the name of the function which is modified but I could not get it.
I know that with python files it is possible using git with the option 'git diff file.py'.
Is it possible do it with c++ files in Mercurial?
I think you are looking for the config option diff.showfunc. The doc (hg help config) says:
"showfunc"
Show which function each change is in.
It's working fine for Python functions and I think the heuristic should work similarly for C++ files. Maybe it will need functions body to be indented to correctly detect C++ functions.

Testing with .in files without changing code

I'm doing some programming problems from the previous year competition and in the problem text there is only one case, which is simple so I can just rewrite it when testing.
Now, I also have a folder with a bunch of .in and .out files, in format '01.in, 01.out, 02.in, 02.out, etc'.
Is there a way to somehow take one of those .in files and automatically use all the lines of it as input without making changes inside my program but rather doing it directly from the command line?
Thanks
Assuming linux:
cat *.in | yourprogram
On Windows you'd use type instead of cat.
I assume your program takes in and processes the agruments (argv[]) passed to it already. If this is the case, one way could be to write a simple wrapper program (in Python for example) which opens the required .in files, reads the lines in it and invokes your C++ program by passing the required lines as input to them.
Then you can execute this python program or make changes to it as required.

python script to create another python script (or python executable)

As part of my code, I want to write a python method which when called, creates an executable file. (Another script that I can execute with python's interpreter is also fine).
This final script is almost fixed, except for a few input objects that only my method knows about, and which are necessary for the final executable to work (a dictionary for example). How can I link these objects to the final executable?
This sounds like a work for some templating engine. Jinja for example.
You can also use pickle to serialize/deserialize (save/load) the data which the other executables would use.

How to run .sql script file in c++?

I want to execute a script file of mysql in my code, written in c++. How i can do it ?
You can always use the system function to call the mysql program to do it for you.
Well, if you want the optimal solution, simply iterate over each line (your SQL file shall have only ONE command for each line) using the standard C++ file handlings.
You simply pass each line to the MySQL library calls. This is what I do for my SQLite initialization scripts.
You can even add a very minimal comment support (like --) and skip any line starting with those characters.

AngelScript, how to load a script file?

I know how I can bind C++ functions to AngelScript, but in my C++ code, how do I load an .as script file? How can I say in my C++ "Execute myscript.as now!" ?
In the AngelScript API I don't find any function like "LoadScript" or "ExecuteScript".
Or do I have to define a path somewhere from where AngelScript loads all scripts and I don't need to tell it the exact files?
Just found it out (in a small side sentence in the docs):
AngelScript doesn't provide a build in file loading. That's why there is no API function. So the manual loading is indeed the only way.
asIScriptModule::AddScriptSection will load a script string. asIScriptContext::Execute will execute a function from a script. The documentation was pretty clear about all of this; you might want to give it a look.