Greetings: As the subject indicates I am working with a product(P1) and I would like to call the methods of a different product(P2) from with in the first. When I try from P@ import method I get an error. How do I get P2 to into the namespace of P1 or at least into the global namespace. thanks very much -matthew
On Tue, 2003-12-02 at 10:32, Matthew Thorley wrote:
Greetings:
As the subject indicates I am working with a product(P1) and I would like to call the methods of a different product(P2) from with in the first. When I try from P@ import method I get an error. How do I get P2 to into the namespace of P1 or at least into the global namespace.
Untested, obviously... but probably close enough to help: ----- from Products.P1.p1module import p1class def my_wrapper(self, request): return p1class.p1method(self, request) ----- HTH, Dylan
On Tuesday 02 December 2003 12:59 pm, Dylan Reinhardt wrote:
On Tue, 2003-12-02 at 10:32, Matthew Thorley wrote:
As the subject indicates I am working with a product(P1) and I would like to call the methods of a different product(P2) from with in the first. When I try from P@ import method I get an error. How do I get P2 to into the namespace of P1 or at least into the global namespace.
Untested, obviously... but probably close enough to help:
----- from Products.P1.p1module import p1class
def my_wrapper(self, request): return p1class.p1method(self, request) -----
I can attest that this works. I use it a *lot*, as I have a product with explicit dependencies on some other products. I also find that I need to import *my* product modules this way when I use nested directory/package structure in my product. I have not found any equivalent to "../" in the way Python imports modules, so I can't just "go up a directory". Is there one, BTW? Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
Terry Hancock wrote:
On Tuesday 02 December 2003 12:59 pm, Dylan Reinhardt wrote:
On Tue, 2003-12-02 at 10:32, Matthew Thorley wrote:
As the subject indicates I am working with a product(P1) and I would like to call the methods of a different product(P2) from with in the first. When I try from P@ import method I get an error. How do I get P2 to into the namespace of P1 or at least into the global namespace.
Untested, obviously... but probably close enough to help:
----- from Products.P1.p1module import p1class
def my_wrapper(self, request): return p1class.p1method(self, request) -----
I can attest that this works. I use it a *lot*, as I have a product with explicit dependencies on some other products.
I also find that I need to import *my* product modules this way when I use nested directory/package structure in my product. I have not found any equivalent to "../" in the way Python imports modules, so I can't just "go up a directory".
Is there one, BTW?
As I recall, this was explicitly rejected from Python for some reason. I suppose you can abstract it a bit by creating, say, a module 'parent' in your current package and importing the names present in the directory above: 'from Products.MyProduct import *'. Then you can 'from parent import SomeBasicClass'. --jcc -- "Code generators follow the 80/20 rule. They solve most of the problems, but not all of the problems. There are always features and edge cases that will need hand-coding. Even if code generation could build 100 percent of the application, there will still be an endless supply of boring meetings about feature design." (http://www.devx.com/java/editorial/15511)
On Tuesday 02 December 2003 02:37 pm, J Cameron Cooper wrote:
Terry Hancock wrote:
On Tuesday 02 December 2003 12:59 pm, Dylan Reinhardt wrote:
from Products.P1.p1module import p1class
I can attest that this works. I use it a *lot*, [...] I have not found any equivalent to "../" in the way Python imports modules, so I can't just "go up a directory". Is there one, BTW?
As I recall, this was explicitly rejected from Python for some reason.
Probably because it's "implicit". And potentially would interfere with moving the module/package.
I suppose you can abstract it a bit by creating, say, a module 'parent' in your current package and importing the names present in the directory above: 'from Products.MyProduct import *'. Then you can 'from parent import SomeBasicClass'.
Truthfully, it's not so objectionable to do it the way I'm doing it. I just wonder sometimes whether I'm going the long-way-round. In college, I was asked how to compute the distance between two stars in my astronomy lab class. I replied with an explanation about measuring the RA and Dec separations off the reticle and then using the sqrt(ra^2 + dec^2) to get the result. To which my instructor replied "Or, you could just turn the reticle." I learned a lesson that night. Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
participants (4)
-
Dylan Reinhardt -
J Cameron Cooper -
Matthew Thorley -
Terry Hancock