HotFixing methods of modules (not classes)
I read and used this http://www.zope.org/Members/Caseman/Dynamic_Hotfix This works great for changing methods and stuff inside classes, but what about stuff that isn't inside the class? (Maybe more of a Python question rather than Zope) --- module.py ---------- def manage_addSomething(self,id): self._setObject(id,Something(id)) class Something(SimpleItem): def __init__(self,id): self.id = id del somemethod(self): return "hello" ---------------------------- This I can HotFix like this: ---- hotfix: __init__.py -------------- # import class from module import Something def myown(self):return "Hello!" Something.somemethod = myown -------------------------------------- But it is this that I can't get to work. Here I need help: ---- buggyhotfix: __init__.py -------------- # import function from module import manage_addSomething def myown(self,id): id = string.upper(id) self._setObject(id,Something(id)) module.manage_addSomething = myown -------------------------------------- it's the "module.manage_addSomething = myown" part that looks like the evil thing. Any hints? Cheers, Peter
On Monday 16 July 2001 02:58 am, Peter Bengtsson wrote:
I read and used this http://www.zope.org/Members/Caseman/Dynamic_Hotfix
<snip>
But it is this that I can't get to work. Here I need help:
---- buggyhotfix: __init__.py -------------- # import function from module import manage_addSomething
def myown(self,id): id = string.upper(id) self._setObject(id,Something(id))
module.manage_addSomething = myown --------------------------------------
it's the "module.manage_addSomething = myown" part that looks like the evil thing.
Any hints?
you don't have a reference to the module since you're grabbing an explicit reference to the class or function in your cases with the from module import stuff syntax. instead you could use import module module.manage_addSomething = myown module.ThatClass.ThatFunction = MyNewClassFunction hth kapil
Peter Bengtsson wrote:
I read and used this http://www.zope.org/Members/Caseman/Dynamic_Hotfix
[snip]
But it is this that I can't get to work. Here I need help:
---- buggyhotfix: __init__.py -------------- # import function from module import manage_addSomething
def myown(self,id): id = string.upper(id) self._setObject(id,Something(id))
module.manage_addSomething = myown --------------------------------------
it's the "module.manage_addSomething = myown" part that looks like the evil thing.
Any hints?
Cheers, Peter
What if you import the whole module instead of just the one function? as in: ------------------------- import module def myfunc(): ... module.theirfunc = myfunc ------------------------- hth, -- | Casey Duncan | Kaivo, Inc. | cduncan@kaivo.com `------------------>
We tried that, but it doesn't work either. I see no pythonic reason why it should be any different for functions. Perhaps it's got something to do with the way Zope imports its modules. Still troubled. Peter
But it is this that I can't get to work. Here I need help:
---- buggyhotfix: __init__.py -------------- # import function from module import manage_addSomething
def myown(self,id): id = string.upper(id) self._setObject(id,Something(id))
module.manage_addSomething = myown --------------------------------------
it's the "module.manage_addSomething = myown" part that looks like the evil thing.
Any hints?
Cheers, Peter
What if you import the whole module instead of just the one function? as in:
------------------------- import module
def myfunc(): ...
module.theirfunc = myfunc -------------------------
hth, -- | Casey Duncan | Kaivo, Inc. | cduncan@kaivo.com `------------------>
Peter Bengtsson wrote:
But it is this that I can't get to work. Here I need help:
---- buggyhotfix: __init__.py -------------- # import function from module import manage_addSomething
def myown(self,id): id = string.upper(id) self._setObject(id,Something(id))
module.manage_addSomething = myown --------------------------------------
As Casey mentioned, this will fail as written because 'module' isn't defined. You need to 'import module'. There's a problem implicit in the name you're overriding, though. If 'manage_addSomething' is a constructor, as its name suggests, it has probably been registered with the Zope machinery by the time you get around to replacing it in the module namespace. Check to see whether 'manage_addSomething' is passed to a registration function in 'module'. Cheers, Evan @ digicool & 4-am
From: Evan Simpson <evan@4-am.com> If 'manage_addSomething' is a constructor, as its name suggests, it has probably been registered with the Zope machinery by the time you get around to replacing it in the module namespace. Check to see whether 'manage_addSomething' is passed to a registration function in 'module'.
If that's the case, would you re-register in your hotfix?
On Mon, 16 Jul 2001, Evan Simpson wrote:
Peter Bengtsson wrote:
But it is this that I can't get to work. Here I need help:
---- buggyhotfix: __init__.py -------------- # import function from module import manage_addSomething
def myown(self,id): id = string.upper(id) self._setObject(id,Something(id))
module.manage_addSomething = myown --------------------------------------
As Casey mentioned, this will fail as written because 'module' isn't defined. You need to 'import module'. There's a problem implicit in the name you're overriding, though. If 'manage_addSomething' is a constructor, as its name suggests, it has probably been registered with the Zope machinery by the time you get around to replacing it in the module namespace. Check to see whether 'manage_addSomething' is passed to a registration function in 'module'.
You could also go the way of Transparent Folders and apply the patch in OFS/__init__.py. That way it will always be applied before any product initialization... Stefan
participants (6)
-
Casey Duncan -
ender -
Evan Simpson -
marc lindahl -
Peter Bengtsson -
Stefan H. Holek