473,385 Members | 1,958 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,385 software developers and data experts.

PYTHON API : add new classes in a module from an other module

Hi every body

I am tring to create new Python modules.

In a first time, I have created a module named FOO in which I have
inserted new classes. No problem :

PyMODINIT_FUNC initFOO( void )
{
PyObject* module = Py_InitModule3( "FOO", 0, "foo module" ) ;
if ( ! module ) return ;

if ( PyType_Ready( &my_type) < 0 ) return ;
Py_INCREF( &my_type );
PyModule_AddObject( module, "my_pytype", (PyObject*) &my_type ) ;
}

In a second time, I would like create a new module named BAR. In the
'init' function of this module, Ii would like add new classes in FOO.
So, I would like white something like the following :

PyMODINIT_FUNC initBAR( void )
{
foo_module = Py_GetModule( "FOO" ) ;
if( ! foo_module ) return ;

if ( PyType_Ready( &my_addtype) < 0 ) return ;
Py_INCREF( &my_addtype );
PyModule_AddObject( foo_module, "my_pyaddtype", (PyObject*)
&my_addtype ) ;
}

So, my question is : does it exist in the Python API, a equivalent
function of "Py_GetModule( <module name> )" (that a have invented for
the example !) ;
or does an other solution exist in order to do the same think ?

Thanks,
Mathieu

Jul 18 '05 #1
1 1752
mathieu gontier <mg*************@laposte.net> wrote:
...
foo_module = Py_GetModule( "FOO" ) ;
if( ! foo_module ) return ; ... So, my question is : does it exist in the Python API, a equivalent
function of "Py_GetModule( <module name> )" (that a have invented for
the example !) ;
or does an other solution exist in order to do the same think ?


There is no direct way to "get a module named this way if it is already
imported otherwise do nothing". PyImport_AddModule comes close, but it
will return an empty module (and insert it into sys.modules too...) in
the 'otherwise' case.

PyImport_GetModuleDict returns sys.module to you as a PyObject*
(borrowed reference, so no worry about needing to decref it later), and
then you can call PyDict_GetItemString on it -- the latter DOES return 0
without setting and exception if the string isn't a key in the
dictionary, so overall it may come closest to your desires even though
you do need a two-calls sequence.

Summarizing, then:

{
PyObject* sys_modules = PyImport_GetModuleDict();
PyObject* foo_module = PyDict_GetItemString(sys_modules, "FOO");
if(!foo_module) return;

/* use foo_module freely here, do not decref it when you're done */

}
Alex
Jul 18 '05 #2

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

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 349 open ( +7) / 3737 closed (+25) / 4086 total (+32) Bugs : 939 open (-12) / 6648 closed (+60) / 7587 total (+48) RFE : 249 open...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.