Discussion:
[Modeling-users] dynamic metaclass overwrites custom methods
n***@tin.it
2004-08-31 22:13:08 UTC
Permalink
Hello,

if I define:

class A:
__metaclass__ = dynamic.CustomObjectMeta
entityName = 'A'
mdl_define_properties = 1

def setName( self, value ):
pass

name being a class property, when the metaclass __new__ method populates
the instance with code it overrides setName with the stock one.
My fix here is to check for the existence of the method before overriding
and rename it to 'mdl_'+method_name (which might not be inline
with your naming conventions) in case it's already there.

patch follows

comments anyone?

cheers
--
Delio

diff -aur /home/delio/tmp/ModelingCore-0.9-pre-17.1/Modeling/dynamic.py ./dynamic.py
--- /home/delio/tmp/ModelingCore-0.9-pre-17.1/Modeling/dynamic.py 2004-02-17 09:01:06.000000000 +1300
+++ ./dynamic.py 2004-08-31 19:51:40.000000000 +1200
@@ -265,6 +265,9 @@
func_name, c=getter_code(prop)
info(classdict, "adding getter: %s"%func_name)
getter=function_for_code(c, func_name)
+ if classdict.has_key( func_name ):
+ func_name = 'mdl_' + func_name
+ info(classdict, "getter already defined renaming to: %s"%func_name)
classdict[func_name]=getter

def define_getters(entity, classname, classdict):
@@ -277,6 +280,9 @@
for func_name, c in names_and_funcs:
info(classdict, "adding setter: %s"%func_name)
setter=function_for_code(c, func_name)
+ if classdict.has_key( func_name ):
+ func_name = 'mdl_' + func_name
+ info(classdict, "setter already defined renaming to: %s"%func_name)
classdict[func_name]=setter

def define_setters(entity, classname, classdict):
Sebastien Bigaret
2004-09-02 16:43:04 UTC
Permalink
Hi,
Post by n***@tin.it
Hello,
__metaclass__ = dynamic.CustomObjectMeta
entityName = 'A'
mdl_define_properties = 1
pass
name being a class property, when the metaclass __new__ method populates
the instance with code it overrides setName with the stock one.
My fix here is to check for the existence of the method before overriding
and rename it to 'mdl_'+method_name (which might not be inline
with your naming conventions) in case it's already there.
patch follows
comments anyone?
That should be handled, you're absolutely right. However, I would
suggest to make a 'warning' instead, and to overwrite the already
defined method m() with a new one calling simply self.willChange()
before calling m() --and the same for getAttribute()() but w/
self.willRead() instead.

What do you think?

-- Sébastien.


PS: for Ernesto: I did not forget about your post on properties, I just
did not take the time to get back on it yet, sorry.
Post by n***@tin.it
cheers
--
Delio
diff -aur /home/delio/tmp/ModelingCore-0.9-pre-17.1/Modeling/dynamic.py ./dynamic.py
--- /home/delio/tmp/ModelingCore-0.9-pre-17.1/Modeling/dynamic.py 2004-02-17 09:01:06.000000000 +1300
+++ ./dynamic.py 2004-08-31 19:51:40.000000000 +1200
@@ -265,6 +265,9 @@
func_name, c=getter_code(prop)
info(classdict, "adding getter: %s"%func_name)
getter=function_for_code(c, func_name)
+ func_name = 'mdl_' + func_name
+ info(classdict, "getter already defined renaming to: %s"%func_name)
classdict[func_name]=getter
@@ -277,6 +280,9 @@
info(classdict, "adding setter: %s"%func_name)
setter=function_for_code(c, func_name)
+ func_name = 'mdl_' + func_name
+ info(classdict, "setter already defined renaming to: %s"%func_name)
classdict[func_name]=setter
n***@tin.it
2004-09-06 23:07:00 UTC
Permalink
On 02 Sep 2004 20:42:44 +0200
Post by Sebastien Bigaret
That should be handled, you're absolutely right. However, I would
suggest to make a 'warning' instead, and to overwrite the already
defined method m() with a new one calling simply self.willChange()
before calling m() --and the same for getAttribute()() but w/
self.willRead() instead.
Letting the framework call willChange/willRead *before* calling my
biz logic sounds elegant but takes the freedom to define if/when to call
them away, plus I like not to write code that addresses "_AttrName" internal
attributes directly and prefer to let the framework generated methods do it.
On top of that it makes code generation a little bit more complex for no
clear (? to me ?) advantage, I'd stick to the simple solution :)
what do you think?

--
Delio

Loading...