Hi!
I've asked Google, but have not found any useful information there.
Situation: I have a base class, say class base(object):
ImportantClassA ttribute = 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.
ImportantClassA ttribute = 7 docString = 'The case %s.' % (ImportantClass Attribute,) exec('''class new(base):
"""%s"""
pass ''' % (docString,)) new.ImportantCl assAttribute = ImportantClassA ttribute 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.ImportantCl assAttribute = 7 new.__doc__ = ('The case %(ImportantClas sAttribute)s.'
% new.__dict__)
Traceback (most recent call last):
File "<pyshell#3 5>", line 1, in -toplevel-
new.__doc__ = ('The case %(ImportantClas sAttribute)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 3 2139
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)('der ived', (base,), {'__doc__': 'zipp'})
Alex
"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): ImportantClassA ttribute = 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. >>> ImportantClassA ttribute = 7 >>> docString = 'The case %s.' % (ImportantClass Attribute,) >>> exec('''class new(base): """%s""" pass ''' % (docString,)) >>> new.ImportantCl assAttribute = ImportantClassA ttribute >>> 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.ImportantCl assAttribute = 7 >>> new.__doc__ = ('The case %(ImportantClas sAttribute)s.' % new.__dict__)
Traceback (most recent call last): File "<pyshell#3 5>", line 1, in -toplevel- new.__doc__ = ('The case %(ImportantClas sAttribute)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 makeBaseSubclas s(impClassAttr) :
return type('new_%s' % impClassAttr,
(base,object),
{'ImportantClas sAttribute': impClassAttr,
'__doc__': 'The case %s' % impClassAttr}) new = makeBaseSubclas s(7) new.ImportantCl assAttribute
7 new.__doc__
'The case 7'
HTH,
George
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)('der ived', (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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 partly, from a snipet I found in the user
contributions to the "str_replace" function in the manual on php.net.
However there is a small problem - it only works in uppercase. Please
first see my code below and then a brief description of the problem:
|
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 or C++. At the moment it is very
hit and miss with the C/C++ comment generation, as it actually works
correctly some of the time, but only some of the time, with it often failing
to pick up the comments and in our office, working only on some...
|
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 by the Account). In
the tblAccounts thee is a field Company which occurs many times (there is
more than one Account for each Company:).
Whet I want to do on my Fom is this: I have a combobox with all my company's
in it. When I choce a Company...
|
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 my program to just few lines to know the reason for this error.
Atlast,
i found that if i am using the stored procedures to get the data, then this
|
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 Server. however
while executing following code lines on MS Acess
ds = new DataSet();
oAdapter.SelectCommand.CommandText = this.SelectCommand;
oAdapter.SelectCommand.Connection = oConn;
| |
by: Nospam |
last post by:
Is there a tutorial on dynamic SID generation?
|
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 syntax in visual basic to edit a combobox? I can do all the dynamic month generation, but I do not know how to edit a combobox from VB.
Thank you.
|
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 updating a table bound to a datagridview (using
the OLEDataAdapter) when the table does not have a primary key; my code works
when the table does have primary key(s).
Is there a workaround for working with tables that do not have any primary...
|
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.
Suppose I have a table,items in a database:
itemid description count
So, basically, I want to create an image that has 3 ovals,
representing the top 3 occurring items (with the highest count) in
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |