I am in the process of developing a Python product for Zope. I have created several container (folderish) objects, and have come across a problem when two objects can recursively contain each other. For example, I will explain 3 of my modules: 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 In module CITEINFO.py import LWORKCIT class CITEINFO(Folder) <all the class stuff here> def add_citeinfo(self, ..., ...): <function statements here> In module LWORKCIT.py class LWORKCIT(Folder) def add_lworkcit(self, ..., ...): <function statements here> 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. 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. If further information, explanation, or code is needed for someone to provide me with a little help, please ask and I will elaborate. Any insight into a solution would be greatly appreciated. Thank you in advance. __________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com
At 07:38 AM 1/22/2003, Stacy Roberts wrote:
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.
I'm having difficulty parsing your exact requirements, but here's three suggestions that might help. 1. You show your class inheriting from Folder. Do you also inherit from OFS.ObjectManager.ObjectManager? Odds are, you'll want to. 2. If these three items can all contain each other, it may make things a lot easier to define their constructor methods in a base class. 3. To create a 'local' reference to an imported function, do something like: ----------------- import foo my_foo = foo.method_name ----------------- From then on, any time you call my_foo() in that scope, you're actually making a call to foo.method_name() Hope one (or all) of those helps get you where you're going. Dylan
Dylan Reinhardt wrote at 2003-1-22 08:55 -0800:
1. You show your class inheriting from Folder. Do you also inherit from OFS.ObjectManager.ObjectManager? Odds are, you'll want to. This is not necessary: "Folder" already derives from "ObjectManager".
Dieter
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
participants (3)
-
Dieter Maurer -
Dylan Reinhardt -
Stacy Roberts