[Zope] Accessing imported module function

Dieter Maurer dieter@handshake.de
Fri, 24 Jan 2003 00:49:11 +0100


Stacy Roberts wrote at 2003-1-22 07:38 -0800:
 > ... object created at wrong place ...
 > ...
 >     In module CITATION.py,  I have the following
 > import statement. This should place this module into
 > my sys.modules dictionary. 
 > 
 >             import CITEINFO
 > 
 >             class CITATION(Folder):
 >                  <all the class stuff here>
 >                  add_citeinfo=CITEINFO.add_citeinfo
This makes "add_citeinfo" an attribute of "CITATION" instances.

 >             This is where my problem lies. In the
 > class statements, I need to gain access to the
 > CITEINFO.py modules add_citeinfo function. 
 >             The CITEINFO.py module exists in the
 > sys.modules and I can retireve it with the following
 > statement:
 >     module = sys.modules 'Products.METADATA.CITEINFO']
 > 
 >              However, I do not know how to gain a
 > reference to the add_citeinfo function. 
Do not do it like this!

   Use "from Products.METADATA.CITEINFO import add_citeinfo"
   or (when you are in the "METADATA" packages)
   "from CITEINFO import add_citeinfo".

 > Inside my 'def add_lworkcit()' function, I make the
 > follwoing call:
 >  
 > withIt.manage_addProduct['METADATA'].add_citeinfo('citeinfo')
 >                 It appears the add_citeinfo defined in
 > the CITATION.py module is called (since I am unable to
 > define a variable add_citeinfo in my LWORKCIT class. 
 >                 My 'citeinfo' object is added to Zope,
 > but in the wrong namespace. 
 >                 'withIt' is the LWORKCIT namespace I
 > want the 'citeinfo' object created in. It seems to
 > ignore this and create a citeinfo object in the
 > CITATION object. 
I expect, you did not register "add_citeinfo" as
a contructor in "registerClass" (during your product initialization).

In this case, the product factory dispatcher
"manage_addProduct['METADATA']" will not have an attribute
"add_citeinfo". "withIt" does neither. Therefore,
acquisition gives you the one of "CITATION".
And this method create in the "CITATION" instance.

The best (easiest) way is to register "add_citeinfo"
or to call the function (imported as shown above) directy.


Dieter