[Grok-dev] Creating an object from a path
Santiago Videla
santiago.videla at gmail.com
Fri Feb 6 10:11:28 EST 2009
Hey,
I think that you are looking for a Factory.
an example from `Web Component Development with Zope 3` book:
from zope.component.interfaces import IFactory
from zope.interface import implementedBy
class RecipeFactory:
implements(IFactory)
title = u"Create a new recipe"
description = u"This factory instantiates new recipes."
def __call__(self):
return Recipe()
def getInterfaces(self):
return implementedBy(Recipe)
<utility
factory=".recipe.RecipeFactory"
name="worldcookery.Recipe"
permission="zope.Public"
/>
$ bin/debugzope
>>> from zope.component import getUtility
>>> from zope.component.interfaces import IFactory
>>> recipe_factory = getUtility(IFactory, u"worldcookery.Recipe")
>>> weisswurst = recipe_factory()
>>> weisswurst
<worldcookery.recipe.Recipe object at 0x30469c30>
I try this myself on grok using grok.GlobalUtility to define the Factory
class and avoid the zcml, and works like a charm.
hope it helps
Regards
On Fri, Feb 6, 2009 at 12:38 PM, Sebastian Ware <sebastian at urbantalk.se>wrote:
> If I have a string:
>
> "<class 'trigger.promotion.Promotion'>"
>
> can I create an object of the type [trigger.promotion.Promotion]?
>
> Mvh Sebastian
>
>
>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
>
--
Santiago Videla
www.revolucionesweb.com.ar
http://www.linkedin.com/in/svidela
Sigue la mata dando de que hablar siempre abajo y a la izquierda donde el
pensamiento que se hace corazón resplandece con la palabra sencilla y
humilde que l at s tod at s somos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/grok-dev/attachments/20090206/1aef403e/attachment.html
More information about the Grok-dev
mailing list