scope / namespace question
Hi, I'm exposing a lot of classes called DrawableXXX and PathXXX. I want to put them in two sub-modules Drawable and Path, so that the python classes would be called Drawable.XXX and Path.XXX. As far as I understand the documentation of scope, I have to do something like this: BOOST_PYTHON_MODULE(myModule) { // register classes which are not part // of Drawable and Path // ... registerDrawableClasses(); registerPathClasses(); } void registerDrawableClasses() { scope drawable = ???; class_<DrawableXXX>("XXX") .def(... } void registerPathClasses() { scope path = ???; class_<PathXXX>("XXX") .def(... } In the documentation nested classes are used, so should I use an empty class like this? struct drawable_place_holder {}; scope drawable = class_<drawable_place_holder>("Drawable",no_init); Is that the way to go? greetings Achim
--- Achim Domma <achim.domma@syynx.de> wrote:
struct drawable_place_holder {}; scope drawable = class_<drawable_place_holder>("Drawable",no_init);
Is that the way to go?
I don't know the answer to this question so hopefully someone else will help, but I see two alternatives that you might want to consider: - Simply split your module into two and put them in a package. - Manufacture the desired Python interface at the Python level. For example: Files: your_module_ext.so your_module.py File you_module.py: from your_package import your_module_ext as ext class empty: pass Drawable = empty() Drawable.XXX = ext.DrawableXXX Ralf P.S.: The second approach allows you to do other interesting things, for example I use this to define constructors with keywords in a very light-weight fashion, or you could add Python-specific member functions by way of class foo(ext.foo): ..." __________________________________________________ Do you Yahoo!? Y! Web Hosting - Let the expert host your web site http://webhosting.yahoo.com/
"Achim Domma" <achim.domma@syynx.de> writes:
Hi,
I'm exposing a lot of classes called DrawableXXX and PathXXX. I want to put them in two sub-modules Drawable and Path, so that the python classes would be called Drawable.XXX and Path.XXX. As far as I understand the documentation of scope, I have to do something like this:
BOOST_PYTHON_MODULE(myModule) { // register classes which are not part // of Drawable and Path // ...
registerDrawableClasses(); registerPathClasses(); }
void registerDrawableClasses() { scope drawable = ???; class_<DrawableXXX>("XXX") .def(... }
void registerPathClasses() { scope path = ???; class_<PathXXX>("XXX") .def(... }
In the documentation nested classes are used, so should I use an empty class like this?
struct drawable_place_holder {}; scope drawable = class_<drawable_place_holder>("Drawable",no_init);
Is that the way to go?
No, just use the Python/'C' API to create a new module object, grab it with a handle<>. Something like: handle<> inner_module(PyModule_New("myModule.Drawable")); scope drawable = object(inner_module); ...but look in the Python docs for the right way to use the module creation functions. Don't expect the above to work verbatim. -- David Abrahams dave@boost-consulting.com * http://www.boost-consulting.com
participants (3)
-
Achim Domma -
David Abrahams -
Ralf W. Grosse-Kunstleve