Dynamically generated models using exec. Is it too custly?

24 views
Skip to first unread message

Arruda

unread,
Apr 16, 2012, 12:19:02 PM4/16/12
to django...@googlegroups.com
I'm doing something like this to generate some models dynamically:

SIMILAR_MODELS_TEMPLATE=
"""
@some_decorator
class %(PREFIX)sModel (Some_Abs_model):
    \"""%(DESCRIPTION)s
    \"""
    pass
"""

then I have a factory:
def similar_models_factory(prefix,description):
     format_dict = {
                          'PREFIX' : prefix,
                          'DESCRIPTION' : description,
     }
     cls_name = "%sModel" % prefix
     cls_source = SIMILAR_MODELS_TEMPLATE % format_dict
     
     exec(cls_source)
     new_cls = locals().get(cls_name)
     return new_cls

And finally I have in the models.py:
from my_factories import similar_models_factory 
 
SIMILAR_MODELS_LIST =(
    {'prefix': 'Foo', 'desc' : 'Foo model and etc..',},
    {'prefix': 'Bars', 'desc' : 'Bars model and etc..',},
.....
)
 
for smodel in SIMILAR_MODELS_LIST:
    cls = similar_models_factory(smodels['prefix'], smodels['desc'])
    vars()[cls.__name__] = cls

And this gives my just what I want, but I read somewhere that if you do:
exec ""
 
It would be too costly, and I would like to know if this would have some heavy load in my project(and if please, how can I calculate this, to compare?) 
Thanks!

Philip Mountifield

unread,
Apr 16, 2012, 12:40:00 PM4/16/12
to django...@googlegroups.com, Philip Mountifield
Did you know that you can use the type function to dynamiclly generate a class definition?

See http://docs.python.org/library/functions.html?highlight=type#type

From the example:
>>> class X(object):
...     a = 1

Is exactly the same as doing...

>>> X = type('X', (object,), dict(a=1))
No exec required...

Regards
Phil
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/64JCLnsIFu0J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


-- 

Philip Mountifield
Formac Electronics Ltd
tel  +44 (0) 1225 837333
fax  +44 (0) 1225 430995

pmount...@formac.net
www.formac.net
www.telgas.net

Jeff Heard

unread,
Apr 16, 2012, 12:45:58 PM4/16/12
to django...@googlegroups.com
Check https://github.com/JeffHeard/ga_dynamic_models.  It may be similar to what you're looking for.  Even if you don't want the code, you can see a way of dynamically generating models.  

Arruda

unread,
Apr 16, 2012, 12:55:03 PM4/16/12
to django...@googlegroups.com
Hummm, I see, thanks for that, I didn't know about type.
And is there a way to generate functions too?(I was also doing it using exec).

And do you have any idea of how can I see the difference in performance when using one or the other?
Thanks. 
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
-- 

Philip Mountifield
Formac Electronics Ltd
tel  +44 (0) 1225 837333
fax  +44 (0) 1225 430995

pmount...@formac.net
www.formac.net
www.telgas.net

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

Philip Mountifield

unread,
Apr 17, 2012, 4:39:24 AM4/17/12
to django...@googlegroups.com, Philip Mountifield
Are you changing the definition of the function each time, or just effectively making duplicates of some common function under different names?

You can do things like this:

def make_funct(blah):
    def inner():
        print blah
   
    return inner

Then make a few of them with different names and pre-assigned config via the parameters:

f1 = make_funct('Hello World!')
f2 = make_funct('Foo')

And call 'em (with no parameters):

f1()    =>    prints Hello World!
f2()    =>    prints Foo

Perhaps that's not what you're looking for though?

In terms of performance, I suggest trying out both approaches and measuring the time taken to do it a few hundred or thousand times and compare.

Regards
Phil
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/pxR15NDv5RUJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Arruda

unread,
Apr 17, 2012, 8:48:43 AM4/17/12
to django...@googlegroups.com, Philip Mountifield
yes, I see, but how can I measure this? Do you know the function that is used?
Thanks for the help.

Philip Mountifield

unread,
Apr 17, 2012, 9:04:05 AM4/17/12
to django...@googlegroups.com, Philip Mountifield
You probably want the profile module from the standard library. This will tell you all sorts of useful things about how much time functions are taking and how many times they are called etc...

http://docs.python.org/library/profile.html#module-profile

Regards
Phil

Arruda

unread,
Apr 17, 2012, 9:24:26 AM4/17/12
to django...@googlegroups.com, Philip Mountifield
Nice, I'll test things out.
Reply all
Reply to author
Forward
0 new messages