Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Dynamic generation of doc-strings of dynamically generated classes

Reply
Thread Tools

Dynamic generation of doc-strings of dynamically generated classes

 
 
Mikael Olofsson
Guest
Posts: n/a
 
      10-17-2005
Hi!

I've asked Google, but have not found any useful information there.

Situation: I have a base class, say

>>> class base(object):

ImportantClassAttribute = None

Now, I want to dynamically generate subclasses of base. That's not a
problem. However, I very much want those subclasses to have individual
doc-strings. More precicely, I want that important class attribute to be
reflected in the doc-string. That's the problem. The only way I've
managed to accomplish that is something like the following.

>>> ImportantClassAttribute = 7
>>> docString = 'The case %s.' % (ImportantClassAttribute,)
>>> exec('''class new(base):

"""%s"""
pass ''' % (docString,))
>>> new.ImportantClassAttribute = ImportantClassAttribute
>>> new.__doc__

'The case 7.'

This works as intended. The subclasses do get the doc-strings I want
them to have, and I can live with this solution. But: This solution does
not strike me as especially beautiful or readable. My first naļve
attempt was instead the following.

>>> class new(base):

pass

>>> new.ImportantClassAttribute = 7
>>> new.__doc__ = ('The case %(ImportantClassAttribute)s.'

% new.__dict__)

Traceback (most recent call last):
File "<pyshell#35>", line 1, in -toplevel-
new.__doc__ = ('The case %(ImportantClassAttribute)s.'
TypeError: attribute '__doc__' of 'type' objects is not writable

This is readable to me, but apparently not the way to go, since I'm not
allowed to replace the doc-string like this. I've also tried a number of
other ways, but they all stumble on similar reasons.

Any ideas? Am I stuck with the clumsy exec-solution, or are there other
ways to dynamically generate doc-strings of classes?

/MiO
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      10-17-2005
Mikael Olofsson <(E-Mail Removed)> wrote:
...
> Any ideas? Am I stuck with the clumsy exec-solution, or are there other
> ways to dynamically generate doc-strings of classes?


The best way to make classes on the fly is generally to call the
metaclass with suitable parameters (just like, the best way to make
instances of any type is generally to call that type):

derived = type(base)('derived', (base,), {'__doc__': 'zipp'})


Alex
 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      10-17-2005
"Mikael Olofsson" <(E-Mail Removed)> wrote:

> Hi!
>
> I've asked Google, but have not found any useful information there.
>
> Situation: I have a base class, say
>
> >>> class base(object):

> ImportantClassAttribute = None
>
> Now, I want to dynamically generate subclasses of base. That's not a
> problem. However, I very much want those subclasses to have individual
> doc-strings. More precicely, I want that important class attribute to be
> reflected in the doc-string. That's the problem. The only way I've
> managed to accomplish that is something like the following.
>
> >>> ImportantClassAttribute = 7
> >>> docString = 'The case %s.' % (ImportantClassAttribute,)
> >>> exec('''class new(base):

> """%s"""
> pass ''' % (docString,))
> >>> new.ImportantClassAttribute = ImportantClassAttribute
> >>> new.__doc__

> 'The case 7.'
>
> This works as intended. The subclasses do get the doc-strings I want
> them to have, and I can live with this solution. But: This solution does
> not strike me as especially beautiful or readable. My first naļve
> attempt was instead the following.
>
> >>> class new(base):

> pass
>
> >>> new.ImportantClassAttribute = 7
> >>> new.__doc__ = ('The case %(ImportantClassAttribute)s.'

> % new.__dict__)
>
> Traceback (most recent call last):
> File "<pyshell#35>", line 1, in -toplevel-
> new.__doc__ = ('The case %(ImportantClassAttribute)s.'
> TypeError: attribute '__doc__' of 'type' objects is not writable
>
> This is readable to me, but apparently not the way to go, since I'm not
> allowed to replace the doc-string like this. I've also tried a number of
> other ways, but they all stumble on similar reasons.
>
> Any ideas? Am I stuck with the clumsy exec-solution, or are there other
> ways to dynamically generate doc-strings of classes?


There's nothing specifically about doc-strings, but you can create and customise a whole class
dynamically:

def makeBaseSubclass(impClassAttr):
return type('new_%s' % impClassAttr,
(base,object),
{'ImportantClassAttribute': impClassAttr,
'__doc__': 'The case %s' % impClassAttr})

>>> new = makeBaseSubclass(7)
>>> new.ImportantClassAttribute

7
>>> new.__doc__

'The case 7'


HTH,
George


 
Reply With Quote
 
Mikael Olofsson
Guest
Posts: n/a
 
      10-18-2005
Alex Martelli wrote:
> The best way to make classes on the fly is generally to call the
> metaclass with suitable parameters (just like, the best way to make
> instances of any type is generally to call that type):
>
> derived = type(base)('derived', (base,), {'__doc__': 'zipp'})


and George Sakkis said something similar.

Thanks, both of you. As I expected, there was a much better way than my
clumsy way. Anyway, this took me to section 3.3.3 in the reference
manual, and that will help me further.

Thanks again. Back to the keyboard!

/MiO
 
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
Pickling dynamically generated classes George Sakkis Python 0 01-26-2008 07:48 AM
Test::Unit::TestCase derived classes don't work when testmethods are generated dynamically Derek Wong Ruby 1 03-20-2007 05:22 AM
Use dynamic-generated JAXB compiled-classes kevincw01 Java 1 03-30-2006 03:14 AM
using rdoc to document dynamically-generated classes Paul Brannan Ruby 4 07-19-2004 07:51 PM
HTML Generation (Next Generation CGI) John W. Long Ruby 4 11-24-2003 04:24 AM



Advertisments