473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Dynamic generation of doc-strings of dynamically generated classes

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
Oct 17 '05 #1
3 2122
Mikael Olofsson <mi****@isy.liu.se> 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
Oct 17 '05 #2
"Mikael Olofsson" <mi****@isy.liu.se> 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
Oct 17 '05 #3
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
Oct 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: duncan.lovett | last post by:
I am working on a graphical heading generator for a clients website as their server does not have the GD library or similar plugins for dynamic image generation. I have achieved the result...
1
by: David Allmond | last post by:
I have been looking at various info about VS 2005 and I keep seeing that the XML code comment generation is being extended to VB.NET, but there seems to be no mention of it correctly supporting C...
4
by: DraguVaso | last post by:
Hi, For my VB.NET application I have the following situation: 2 tables on my SQL Server: tblAccounts and tblRules. For each Account there are many Rules (so tblRules is linked to my tblAccounts...
2
by: Irfan | last post by:
hi, I am getting the following error. Dynamic SQL generation is not supported against a SelectCommand that does not return any base table information when i try to use da.update I squeezed...
0
by: Gancy | last post by:
Hi, I have data access tier designed in such a way, just by changnging application settings, same lines of code is made to work with both MS SQL Server or MS Access. Code works fine with MS SQL...
15
by: Nospam | last post by:
Is there a tutorial on dynamic SID generation?
3
by: fedya | last post by:
I am trying to have the last 12 months to always be the option in the dropdown for a combo box. (Basically a combobox, with dynamic options) I am using Access 2000. What is the function and...
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
The full error message is "Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information." I am getting this error when...
7
by: dino d. | last post by:
Hi- I want to create a dynamic image with areas so that when the user clicks different areas, the user jumps to those pages. The problem is, I can't seem to figure out how to do this efficiently....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.