Hello, all. Somebody know how to get a Zope Context within Zope Product or External Method? For example, I have an object (e.g. DTML Method) somewhere in Zope, called as "MyObject". It's might be accessed: http://www.myhost.com/test/MyObject The problem is, that External Method like this works O.K.: [test.py (External Method)] --------8<---------- def foo(self): return self.test.MyObject() --------8<---------- ...but *within a class* -- not: [test.py (External Method)] --------8<---------- class bar: def spam(self): return self.test.MyObject() def foo(self): b = bar() return b.spam() --------8<---------- ...because I need a context, like: return context.test.MyObject() ...but I still can't find how to get it. ;-( Thank you much. -- Sincerely yours, Bogdan M. Maryniuck `When you say "I wrote a program that crashed Windows", people just stare at you blankly and say "Hey, I got those with the system, *for free*".' (By Linus Torvalds)
I got round this one by actually passing the context to the class constructor as an argument, and storing it as self.context with the class's __init__ method. You can then freely call things on it, in my case for r in self.context.someSQLMethod(): or whatever. Since python functions are call-by-reference, you can even do self.context.manage_addFolder() or whatever, and it will affect the context originially passed to it. David On Mon, 5 Aug 2002, Bo M. Maryniuck wrote:
Hello, all.
Somebody know how to get a Zope Context within Zope Product or External Method?
For example, I have an object (e.g. DTML Method) somewhere in Zope, called as "MyObject". It's might be accessed: http://www.myhost.com/test/MyObject
The problem is, that External Method like this works O.K.:
[test.py (External Method)] --------8<---------- def foo(self): return self.test.MyObject() --------8<----------
...but *within a class* -- not: [test.py (External Method)] --------8<---------- class bar: def spam(self): return self.test.MyObject()
def foo(self): b = bar() return b.spam() --------8<----------
...because I need a context, like:
return context.test.MyObject()
...but I still can't find how to get it. ;-( Thank you much. -- Sincerely yours, Bogdan M. Maryniuck
`When you say "I wrote a program that crashed Windows", people just stare at you blankly and say "Hey, I got those with the system, *for free*".' (By Linus Torvalds)
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
DA Loeffler wrote:
I got round this one by actually passing the context to the class constructor as an argument, and storing it as self.context with the class's __init__ method. You can then freely call things on it, in my case
Ouch! Bad! Pain! Don't!
for r in self.context.someSQLMethod():
or whatever. Since python functions are call-by-reference, you can even do self.context.manage_addFolder() or whatever, and it will affect the context originially passed to it.
This is distinctly dodgy and probably work more by luck than design :-S
Somebody know how to get a Zope Context within Zope Product or External Method?
For example, I have an object (e.g. DTML Method) somewhere in Zope, called as "MyObject". It's might be accessed: http://www.myhost.com/test/MyObject
The problem is, that External Method like this works O.K.:
[test.py (External Method)] --------8<---------- def foo(self): return self.test.MyObject() --------8<----------
Indeed...
...but *within a class* -- not: [test.py (External Method)] --------8<---------- class bar: def spam(self): return self.test.MyObject()
def foo(self): b = bar() return b.spam() --------8<----------
Well, creating class instances in external methods like this is distinctly dodgy. Perhaps you could explain what you're trying to do and we can suggest the 'right' way of going about it? cheers, Chris
On Mon, 5 Aug 2002, Chris Withers wrote:
DA Loeffler wrote:
I got round this one by actually passing the context to the class constructor as an argument, and storing it as self.context with the class's __init__ method. You can then freely call things on it, in my case
Ouch! Bad! Pain! Don't! [...] This is distinctly dodgy and probably work more by luck than design :-S
OK, so how else do I do it? The problem is that I am trying to create a class whose instances will be ephemeral, lasting only for one transaction, but which can call objects within the ZODB. Essentially my classes are glorified Pluggable Brains, but they have to combine data from several database queries, which are of course all Z SQL methods. The data cannot be passed as class initialisation parameters as the class needs to decide what calls to make depending on the data from other queries. David
DA Loeffler wrote:
On Mon, 5 Aug 2002, Chris Withers wrote:
DA Loeffler wrote:
I got round this one by actually passing the context to the class constructor as an argument, and storing it as self.context with the class's __init__ method. You can then freely call things on it, in my case
Ouch! Bad! Pain! Don't! [...] This is distinctly dodgy and probably work more by luck than design :-S
OK, so how else do I do it? The problem is that I am trying to create a class whose instances will be ephemeral, lasting only for one transaction, but which can call objects within the ZODB. Essentially my classes are glorified Pluggable Brains, but they have to combine data from several database queries, which are of course all Z SQL methods. The data cannot be passed as class initialisation parameters as the class needs to decide what calls to make depending on the data from other queries.
FWIW i also do this and dont have any problems. i think the important point here is that these classes are not persistent; they (or rather instances of them) are constructed with each request, do their stuff and are thrown away.
David
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Chris Withers wrote:
DA Loeffler wrote:
I got round this one by actually passing the context to the class constructor as an argument, and storing it as self.context with the class's __init__ method. You can then freely call things on it, in my case
Ouch! Bad! Pain! Don't!
To be a little bit more verbose than Chris, imagine copying and pasting this instance. Or if you have this setting: Folders a,b, both subfolders of c, and you do something like c.a.b.manage_addProduct['yourProduct'].addYourProduct( ... ). What exactly is the "context" now?
for r in self.context.someSQLMethod():
or whatever. Since python functions are call-by-reference, you can even do self.context.manage_addFolder() or whatever, and it will affect the context originially passed to it.
See above, where would the folder be added?
[SNIP]
Are you both, Bogdan and David, talking about "context" as in the context occuring with python scripts, perhaps? cheers, oliver
On Monday 05 August 2002 18:19, Oliver Bleutgen wrote: [SKIPPED]
What exactly is the "context" now?
Well, you're right. But how to simple call "manage_addSomething" or "manage_delObject['SomeObject']" from a Z Product *Class*? -- Sincerely yours, Bogdan M. Maryniuck quit When the quit statement is read, the bc processor is terminated, regardless of where the quit state- ment is found. For example, "if (0 == 1) quit" will cause bc to terminate. (Seen in the manpage for "bc". Note the "if" statement's logic)
Bo M. Maryniuck wrote:
On Monday 05 August 2002 18:19, Oliver Bleutgen wrote: [SKIPPED]
What exactly is the "context" now?
Well, you're right. But how to simple call "manage_addSomething" or "manage_delObject['SomeObject']" from a Z Product *Class*?
In practice, while instanciating your class, you want to simultanously add subobjects? Something like this (stolen from zopelabs.com, see http://www.zopelabs.com/cookbook/995468614 def __init__(self, acquiredObjectName, container): """Initialize my instance""" self = self.__of__(container) # Create an acquisition wrapper acquiredObject = getattr(self, acquiredObjectName) # Acquire the object HTH, oliver
On Monday 05 August 2002 17:42, DA Loeffler wrote:
I got round this one by actually passing the context to the class constructor as an argument, and storing it as self.context with the class's __init__ method. You can then freely call things on it, in my case
for r in self.context.someSQLMethod():
or whatever. Since python functions are call-by-reference, you can even do self.context.manage_addFolder() or whatever, and it will affect the context originially passed to it.
It really does not work or probably You just does not understand what I need (or I not understand You as well). O.K. I'll try again to exaplain. In Zope product, I do: class foo: def bar(self): return self.context.MyObject() ...and this does not work. O.K., but in the _same_ Zope Product, I just add simple "def" (not within a class), like: def testHere(self): return self.MyObject() ...and it works. O.K., now how to pass the "context", since all the time Zope returns me "The variavle 'context' is not defined"? Can You show me a code a little? -- Sincerely yours, Bogdan M. Maryniuck Sigh. I like to think it's just the Linux people who want to be on the "leading edge" so bad they walk right off the precipice. (Craig E. Groeschel)
Bo M. Maryniuck writes:
Somebody know how to get a Zope Context within Zope Product or External Method? ... class bar: def spam(self): return self.test.MyObject() Your class must derive from "Acquisition.Implicit" for this to work...
Dieter
On Tuesday 06 August 2002 21:01, Dieter Maurer wrote:
Bo M. Maryniuck writes:
class bar: def spam(self): return self.test.MyObject()
Your class must derive from "Acquisition.Implicit" for this to work...
No only for this: "I" should be ObjectManager too, to get all the kitchen. Anyway, thank You, I found all what I need. -- Sincerely yours, Bogdan M. Maryniuck As usual, this being a 1.3.x release, I haven't even compiled this kernel yet. So if it works, you should be doubly impressed. (Linus Torvalds, announcing kernel 1.3.3 on the linux-kernel mailing list.)
participants (6)
-
Bo M. Maryniuck -
Chris Withers -
DA Loeffler -
Dieter Maurer -
Oliver Bleutgen -
peter sabaini