473,395 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I use python object in C++

for example I have the following code:

#include <Python.h>

void exec_pythoncode( int arg, char**argv )
{
Py_Initialize();
Py_Main(argc,argv);
Py_Finalize();
}

What I would like to know is how can I get the variables I want
after Py_Main(argc,argv)

say I have a=[1,2,'Hello World',0.1234] in the python space,
I would like to have it as a struct in C++.

Any sample code?
Thanks in advance!
Sep 24 '08 #1
5 4695
On Sep 23, 7:50*pm, lixinyi...@gmail.com wrote:
for example I have the following code:

#include <Python.h>

void exec_pythoncode( int arg, char**argv )
{
* * Py_Initialize();
* * Py_Main(argc,argv);
* * Py_Finalize();

}

What I would like to know is how can I get the variables I want
after Py_Main(argc,argv)

say I have a=[1,2,'Hello World',0.1234] in the python space,
I would like to have it as a struct in C++.

Any sample code?
Thanks in advance!
What do you know about the contents of 'argc' and 'argv'? If it's
impossible with Py_Main, can you use one of the other entry points?

If you've never manipulated PyObject* objects in C, they can be
hairy. Even if you know 'a' is a sequence, its contents are still all
PyObject*s, which you can access via PyList_... and PySequence_
functions.
Sep 24 '08 #2
If the PyObject is a PyList, and all list items are strings,
say a=['aaa','bbb','ccc']

How can I have a
myArray[0] = "aaa"
myArray[1] = "bbb"
myArray[2] = "ccc"
in C++?

Do I have to
use PyModule_GetDict() to get the dict first?
what about the next?

>
What do you know about the contents of 'argc' and 'argv'? *If it's
impossible with Py_Main, can you use one of the other entry points?

If you've never manipulated PyObject* objects in C, they can be
hairy. *Even if you know 'a' is a sequence, its contents are still all
PyObject*s, which you can access via PyList_... and PySequence_
functions.
Sep 24 '08 #3
On Sep 23, 9:30*pm, lixinyi...@gmail.com wrote:
If the PyObject is a PyList, and all list items are strings,
say a=['aaa','bbb','ccc']

How can I have a
myArray[0] = "aaa"
myArray[1] = "bbb"
myArray[2] = "ccc"
in C++?

Do I have to
use PyModule_GetDict() to get the dict first?
what about the next?
What do you know about the contents of 'argc' and 'argv'? *If it's
impossible with Py_Main, can you use one of the other entry points?
If you've never manipulated PyObject* objects in C, they can be
hairy. *Even if you know 'a' is a sequence, its contents are still all
PyObject*s, which you can access via PyList_... and PySequence_
functions.

This one writes '[aaa, bbb, ccc]' to the console in two different
ways.

#include <Python.h>

int main() {
PyObject *list, *listrepr;
Py_Initialize();

/* first */
list= PyList_New( 3 );
PyList_SetItem( list, 0, PyString_FromString( "aaa" ) );
PyList_SetItem( list, 1, PyString_FromString( "bbb" ) );
PyList_SetItem( list, 2, PyString_FromString( "ccc" ) );

listrepr= PyObject_Repr( list );
printf( "%s\n", PyString_AsString( listrepr ) );

Py_DECREF( listrepr );
Py_DECREF( list );

/* second */
list= Py_BuildValue( "[sss]", "aaa", "bbb", "ccc" );
listrepr= PyObject_Repr( list );
printf( "%s\n", PyString_AsString( listrepr ) );

Py_DECREF( listrepr );
Py_DECREF( list );

Py_Finalize();
return 0;
}

Did you want to execute some Python code, and examine variables it
creates?
Sep 24 '08 #4
On Sep 23, 11:06*pm, "Aaron \"Castironpi\" Brady"
<castiro...@gmail.comwrote:
On Sep 23, 9:30*pm, lixinyi...@gmail.com wrote:
If the PyObject is a PyList, and all list items are strings,
say a=['aaa','bbb','ccc']
How can I have a
myArray[0] = "aaa"
myArray[1] = "bbb"
myArray[2] = "ccc"
in C++?
Do I have to
use PyModule_GetDict() to get the dict first?
what about the next?
What do you know about the contents of 'argc' and 'argv'? *If it's
impossible with Py_Main, can you use one of the other entry points?
If you've never manipulated PyObject* objects in C, they can be
hairy. *Even if you know 'a' is a sequence, its contents are still all
PyObject*s, which you can access via PyList_... and PySequence_
functions.

This one writes '[aaa, bbb, ccc]' to the console in two different
ways.

#include <Python.h>

int main() {
* * PyObject *list, *listrepr;
* * Py_Initialize();

* * /* first */
* * list= PyList_New( 3 );
* * PyList_SetItem( list, 0, PyString_FromString( "aaa" ) );
* * PyList_SetItem( list, 1, PyString_FromString( "bbb" ) );
* * PyList_SetItem( list, 2, PyString_FromString( "ccc" ) );

* * listrepr= PyObject_Repr( list );
* * printf( "%s\n", PyString_AsString( listrepr ) );

* * Py_DECREF( listrepr );
* * Py_DECREF( list );

* * /* second */
* * list= Py_BuildValue( "[sss]", "aaa", "bbb", "ccc" );
* * listrepr= PyObject_Repr( list );
* * printf( "%s\n", PyString_AsString( listrepr ) );

* * Py_DECREF( listrepr );
* * Py_DECREF( list );

* * Py_Finalize();
* * return 0;

}

Did you want to execute some Python code, and examine variables it
creates?
Here's a third way:

PyObject *list, *listrepr, *dict, *result, *name;
/* third */
dict= PyDict_New( );
result= PyRun_String( "myarray= [ 'ggg', 'hhh', 'iii' ]",
Py_file_input, dict, NULL );
name= PyString_FromString( "myarray" );
list= PyDict_GetItem( dict, name );
listrepr= PyObject_Repr( list );
printf( "%s\n", PyString_AsString( listrepr ) );

Py_DECREF( dict );
Py_DECREF( result );
Py_DECREF( name );
Py_DECREF( listrepr );
Py_DECREF( list );

PyRun_String runs a namespace, 'dict', which holds the variables.
Sep 24 '08 #5
On 9$B7n(B24$BF|(B, $B8a8e(B1:15, "Aaron \"Castironpi\" Brady" <castiro...@gmail.com>
wrote:
On Sep 23, 11:06 pm, "Aaron \"Castironpi\" Brady"

<castiro...@gmail.comwrote:
On Sep 23, 9:30 pm, lixinyi...@gmail.com wrote:
If the PyObject is a PyList, and all list items are strings,
say a=['aaa','bbb','ccc']
How can I have a
myArray[0] = "aaa"
myArray[1] = "bbb"
myArray[2] = "ccc"
in C++?
Do I have to
use PyModule_GetDict() to get the dict first?
what about the next?
What do you know about the contents of 'argc' and 'argv'? If it's
impossible with Py_Main, can you use one of the other entry points?
If you've never manipulated PyObject* objects in C, they can be
hairy. Even if you know 'a' is a sequence, its contents are still all
PyObject*s, which you can access via PyList_... and PySequence_
functions.
This one writes '[aaa, bbb, ccc]' to the console in two different
ways.
#include <Python.h>
int main() {
PyObject *list, *listrepr;
Py_Initialize();
/* first */
list= PyList_New( 3 );
PyList_SetItem( list, 0, PyString_FromString( "aaa" ) );
PyList_SetItem( list, 1, PyString_FromString( "bbb" ) );
PyList_SetItem( list, 2, PyString_FromString( "ccc" ) );
listrepr= PyObject_Repr( list );
printf( "%s\n", PyString_AsString( listrepr ) );
Py_DECREF( listrepr );
Py_DECREF( list );
/* second */
list= Py_BuildValue( "[sss]", "aaa", "bbb", "ccc" );
listrepr= PyObject_Repr( list );
printf( "%s\n", PyString_AsString( listrepr ) );
Py_DECREF( listrepr );
Py_DECREF( list );
Py_Finalize();
return 0;
}
Did you want to execute some Python code, and examine variables it
creates?

Here's a third way:

PyObject *list, *listrepr, *dict, *result, *name;
/* third */
dict= PyDict_New( );
result= PyRun_String( "myarray= [ 'ggg', 'hhh', 'iii' ]",
Py_file_input, dict, NULL );
name= PyString_FromString( "myarray" );
list= PyDict_GetItem( dict, name );
listrepr= PyObject_Repr( list );
printf( "%s\n", PyString_AsString( listrepr ) );

Py_DECREF( dict );
Py_DECREF( result );
Py_DECREF( name );
Py_DECREF( listrepr );
Py_DECREF( list );

PyRun_String runs a namespace, 'dict', which holds the variables.

Thanks! it helps.

I know how to read from python now.

main_module = PyImport_AddModule("__main__");
global_dict = PyModule_GetDict(main_module);
pyObject = PyDict_GetItem(global_dict,key);
it will require lots of type checking, but it's fine to me.

Sep 24 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jerald | last post by:
Running python 2.3.4 on valgrind (a tool like purify which checks the use of uninitialized memory, etc), gives a lot of errors. See below. jfj@cluster:~/> python -V Python 2.3.4...
1
by: Justin Johnson | last post by:
Hello, I'm trying to build Python 2.5.0 on AIX 5.3 using IBM's compiler (VisualAge C++ Professional / C for AIX Compiler, Version 6). I run configure and make, but makes fails with undefined...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.