Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Create classes at runtnime

Reply
Thread Tools

Create classes at runtnime

 
 
Peter Otten
Guest
Posts: n/a
 
      02-05-2011
Carl Banks wrote:

> On Feb 4, 1:32 pm, Peter Otten <(E-Mail Removed)> wrote:
>> Marc Aymerich wrote:
>> > I need to create a pretty complex class at runtime. something like
>> > this one:

>>
>> I have a hunch that you've never heard the famous Kernighan quote:
>>
>> "Everyone knows that debugging is twice as hard as writing a program in
>> the first place. So if you're as clever as you can be when you write it,
>> how will you ever debug it?"
>>
>> Or that if you've heard it you don't heed it.

>
> [snip]
>
>> > What is your recommendation?

>>
>> Find something that is simple and robust. Something else.

>
>
> I've done things like this before, and I would say the Kernigan quote
> is not appropriate here, and the implication that isn't not simple and
> robust is incorrect. It's repetitive code like the OP posted that's
> complex and flimsy. Repetitive code is the exact opposite of the DRY
> principle, and there's a reason DRY is one of the most often cited
> principles here.
>
> Using advanced techniques like metaclasses to exploit the similarity
> to reduce or eliminate the repetitiveness can, if done well, simplify
> code. By a lot.
>
> Advanced is not the same thing as complex.
>
>
> In this particular case I'd say the recommendation to do something
> else is a good one, but it's not true in general.


The trouble with metaclasses is that they are not completely transparent.
Instead of two levels (class and instance) you have to consider three.
Steven D'Aprano has involuntarily given an example of what may go wrong.

Marc has asked another question with a problem that could be attacked with a
metaclass before. Had he been comfortable with metaclasses by now we
wouldn't have seen the above post. So he is operating at the limits of his
current understanding of Python in an area where it is not unlikely that
he'll run into unexpected side effects of the django database wrapper.

I think a reminder that he has to debug the whole thing is in order

Peter
 
Reply With Quote
 
 
 
 
Marc Aymerich
Guest
Posts: n/a
 
      02-05-2011
On Feb 5, 1:06*pm, Marc Aymerich <(E-Mail Removed)> wrote:
> Thank you all for the answers!
>
> I'll try to give you the context in which I need to generate classes
> like this.
>
> I'm developing a reusable control panel for an ISP. I have several
> modules (VirtualUser, SystemUser, VPS, VirtualHost, ...) and they all
> share the fact that you can have limits on resource consumption. for
> example.
>
> VPS: has limits on swap, memory, cpu and disk.
> SystemUser: has limits on disk and traffic.
>
> As all modules share this property of being limited I have decided to
> split the limit functionality in a separate module (called resources).
> Resources are associated dinamically with (vps, systemuser ...)
> through a Monitor class. So it's up to you decide what limits you are
> interested to put in the VPS or in the SystemUser ...
>
> So when you attach the "disk limit" to SystemUser model is necessary
> to create a web form for SystemUser a bit different than if you decide
> to have disk and traffic limits. That is what my posted code is
> supposed to do.
>
> Do you think that I am on the wrong way designing the control panel
> like this?
>
> Btw, I've seen several interesting ideas to create the class
> dinamically (with class factorys, MetaClass, dictionaries ..), but I
> have not yet clear whether this will be usefull for create the
> __init__ function at runtime too. Any clue on this?
>


this seems to work

def makeLimitForm(name, monitors, _model):

class Meta:
model = _model
dct = { 'Meta': Meta }

for monitor in monitors:
field_name = monitor.resource + "_limit"
dct[field_name] = forms.CharField(max_length=100,
initial=monitor.default_limit)

def __init__(self, *args, **kwargs):
ModelForm.__init__(self, *args, **kwargs)
if 'instance' in kwargs:
for monitor in monitors:
field_name = monitor.resource + "_limit"
print monitor
self.fields[field_name].initial =
kwargs['instance'].monitoring.filter(monitor=monitor)[0].current

dct['__init__'] = __init__

return type(name,(ModelForm,),dct)
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-06-2011
Marc Aymerich wrote:

> On Feb 5, 1:06 pm, Marc Aymerich <(E-Mail Removed)> wrote:
>> Thank you all for the answers!
>>
>> I'll try to give you the context in which I need to generate classes
>> like this.
>>
>> I'm developing a reusable control panel for an ISP. I have several
>> modules (VirtualUser, SystemUser, VPS, VirtualHost, ...) and they all
>> share the fact that you can have limits on resource consumption. for
>> example.
>>
>> VPS: has limits on swap, memory, cpu and disk.
>> SystemUser: has limits on disk and traffic.
>>
>> As all modules share this property of being limited I have decided to
>> split the limit functionality in a separate module (called resources).
>> Resources are associated dinamically with (vps, systemuser ...)
>> through a Monitor class. So it's up to you decide what limits you are
>> interested to put in the VPS or in the SystemUser ...
>>
>> So when you attach the "disk limit" to SystemUser model is necessary
>> to create a web form for SystemUser a bit different than if you decide
>> to have disk and traffic limits. That is what my posted code is
>> supposed to do.
>>
>> Do you think that I am on the wrong way designing the control panel
>> like this?
>>
>> Btw, I've seen several interesting ideas to create the class
>> dinamically (with class factorys, MetaClass, dictionaries ..), but I
>> have not yet clear whether this will be usefull for create the
>> __init__ function at runtime too. Any clue on this?
>>

>
> this seems to work
>
> def makeLimitForm(name, monitors, _model):
>
> class Meta:
> model = _model
> dct = { 'Meta': Meta }
>
> for monitor in monitors:
> field_name = monitor.resource + "_limit"
> dct[field_name] = forms.CharField(max_length=100,
> initial=monitor.default_limit)
>
> def __init__(self, *args, **kwargs):
> ModelForm.__init__(self, *args, **kwargs)
> if 'instance' in kwargs:
> for monitor in monitors:
> field_name = monitor.resource + "_limit"
> print monitor
> self.fields[field_name].initial =
> kwargs['instance'].monitoring.filter(monitor=monitor)[0].current
>
> dct['__init__'] = __init__
>
> return type(name,(ModelForm,),dct)


I've looked around to see whether Django offers an API for your usecase, but
only found

http://code.djangoproject.com/wiki/DynamicModels

which seems to describe what you are already doing.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes within classes David ASP .Net 2 07-22-2005 07:13 PM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM
Modifiers applied to attributes, local variables, member functions, classes and inncer classes ! Razvan Java 11 07-17-2004 08:57 PM
Can I using reflection to get all child classes or classes undera package dynamically? Carfield Yim Java 1 05-31-2004 05:33 PM
How to access inner classes variables & methods from outer classes lonelyplanet999 Java 1 11-13-2003 01:54 PM



Advertisments