creating a python function callable within zope
I have created a python based zope product for the cmf which is working ok. I would like to create a function that belongs in the same .py file as the product class but the function will not be a method of a class instance. I want to be able to call this function from anywher within zope to do something related to the product class but not an instance of that class. I assume the function "def function( self ): etc..." is placed outside of the class statement in the .py file but I cannot work out what I need to do to be able to call it in zope, or if it's possible or wise to do. At the moment I have this function written in a zope "python script". Or am I supposed to make it an external method? Hope that makes sense as I'm still trying to get my head around this class,instance,method,function thing! Any ideas or help on where I am going wrong would be much appreciated. (Running zope 2.5.1 / python 2.1.3 / win2000) Thanks Steve.
a better/easier approach would be to separate the function into a utility module in your product and make it accessible from python scripts and zpt using the RestricedImport functionality, as an example i have this in a utility product's init method from AccessControl import ModuleSecurityInfo from MyUtilModule import func1, func2 security = ModuleSecurityInfo() security.declarePublic('func1', 'func2') security.apply(globals()) another example is the cmf core's utils module this will make it accessible to python scripts and zpt. -kapil On Friday 02 August 2002 01:41 pm, Steve Nicholson wrote:
I have created a python based zope product for the cmf which is working ok. I would like to create a function that belongs in the same .py file as the product class but the function will not be a method of a class instance. I want to be able to call this function from anywher within zope to do something related to the product class but not an instance of that class. I assume the function "def function( self ): etc..." is placed outside of the class statement in the .py file but I cannot work out what I need to do to be able to call it in zope, or if it's possible or wise to do. At the moment I have this function written in a zope "python script". Or am I supposed to make it an external method?
Hope that makes sense as I'm still trying to get my head around this class,instance,method,function thing! Any ideas or help on where I am going wrong would be much appreciated.
(Running zope 2.5.1 / python 2.1.3 / win2000) Thanks Steve.
_______________________________________________ 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 )
i dont know how useful this would be, but theoretically you should be able to put a function into the misc_ dictionary of your product usually you use the misc_ dictionary for images, eg. misc_ = { 'logo' : Globals.ImageFile( ... ) } (this goes into your products __init__.py) which then can be called with an url /misc_/YourProductName/logo so you could do something like def func(): # ... misc_ = { 'func' : func } but i dont know if it would get passed any url parameters etc., and i think you wont have access to acquisition is there some specific reason not to make your function a class method? - peter. Steve Nicholson wrote:
I have created a python based zope product for the cmf which is working ok. I would like to create a function that belongs in the same .py file as the product class but the function will not be a method of a class instance. I want to be able to call this function from anywher within zope to do something related to the product class but not an instance of that class. I assume the function "def function( self ): etc..." is placed outside of the class statement in the .py file but I cannot work out what I need to do to be able to call it in zope, or if it's possible or wise to do. At the moment I have this function written in a zope "python script". Or am I supposed to make it an external method?
Hope that makes sense as I'm still trying to get my head around this class,instance,method,function thing! Any ideas or help on where I am going wrong would be much appreciated.
(Running zope 2.5.1 / python 2.1.3 / win2000) Thanks Steve.
_______________________________________________ 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 )
participants (3)
-
kapil thangavelu -
peter sabaini -
Steve Nicholson