<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
Hello Bob,<br>
<br>
Thanks for this better and simple approach!<br>
To be perfect I would like to be able to do.<br>
>>> type = CategoryType()<br>
>>> type.TRANSISTOR<br>
0<br>
I will use your enums declaration and the range part to use as keys
for enums.<br>
And I will raise a KeyError in toString() to handle invalid
litteral.<br>
<br>
Thanks!<br>
<br>
Regards<br>
Karim<br>
<br>
<br>
On 01/18/2011 02:24 PM, bob gailer wrote:
<blockquote cite="mid:4D359477.8070309@gmail.com" type="cite">
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
On 1/18/2011 8:08 AM, Karim wrote:
<blockquote cite="mid:4D3590CD.5070402@free.fr" type="cite">
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<br>
I know this is ugly but until now it is the only way (with this
side effect) I found to declare Enums class that I <u>understand</u>:<br>
<br>
<small><b>class CategoryType(object):<br>
"""Implements the enumeration and prevent associated
newly created<br>
instances to redefine enums value via special class
variable '__slots__'<br>
definition. It makes also, instances have no attributes
at all.<br>
"""<br>
__slots__ = ()<br>
<br>
TRANSISTOR, MOS, BIPOLAR, CAPACITOR, RESISTOR, DIODE,
ESD, PAD, \<br>
COMPARATOR, OPAMP, ADC, DAC, PLL, OTHER = range(14)<br>
<br>
def toString ( self, litteral ):<br>
"""Convert the litteral integer number to its
associated string representation."""<br>
if litteral == self.TRANSISTOR:<br>
return 'transistor'<br>
elif litteral == self.MOS:<br>
return 'mos'<br>
elif litteral == self.BIPOLAR:</b></small><br>
[...]<br>
</blockquote>
IMHO this just cries out for a dictionary:<br>
<br>
<tt>class CategoryType(object):<br>
__slots__ = ()<br>
enums = {<br>
0: 'transistor',<br>
1: 'mos', <br>
...<br>
}<br>
def toString(self, literal):<br>
"""Convert the literal integer number to its associated
string representation."""<br>
return self.enums[literal]<br>
<br>
</tt>Note this does not handle invalid literal.<br>
<br>
If you are happy with range 14) you could alternatively use a
list:<br>
<tt> enums = ['transistor, 'mos', ...]<br>
</tt><br>
<pre class="moz-signature" cols="72">--
Bob Gailer
919-636-4239
Chapel Hill NC</pre>
</blockquote>
<br>
</body>
</html>