Most viewed recipes tagged "enum"http://code.activestate.com/recipes/tags/enum/views/2013-03-07T13:10:29-08:00ActiveState Code RecipesYet another 'enum' for Python (Python)
2010-01-28T18:23:11-08:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/577024-yet-another-enum-for-python/
<p style="color: grey">
Python
recipe 577024
by <a href="/recipes/users/924636/">Gabriel Genellina</a>
(<a href="/recipes/tags/enum/">enum</a>).
Revision 5.
</p>
<p>There are quite a few 'enum' recipes already here. This one is short, cheap, and has neither bells nor whistles :)</p>
Yet another enum for Python (Python)
2009-04-03T12:02:33-07:00Antonio Cunihttp://code.activestate.com/recipes/users/4169762/http://code.activestate.com/recipes/576711-yet-another-enum-for-python/
<p style="color: grey">
Python
recipe 576711
by <a href="/recipes/users/4169762/">Antonio Cuni</a>
(<a href="/recipes/tags/enum/">enum</a>).
</p>
<p>There are plenty of ways to declare C-like enums in Python. This one tries to be minimalistic and with a nice syntax, (ab)using decorators.</p>
<p>The code is also available here:
<a href="http://codespeak.net/svn/user/antocuni/hack/enum.py" rel="nofollow">http://codespeak.net/svn/user/antocuni/hack/enum.py</a></p>
Short Enum Recipe (Python)
2013-01-31T14:16:06-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577021-short-enum-recipe/
<p style="color: grey">
Python
recipe 577021
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/enum/">enum</a>, <a href="/recipes/tags/quick/">quick</a>, <a href="/recipes/tags/short/">short</a>).
Revision 9.
</p>
<p>If you want a small code snippet for enumerations in Python and the following code is sufficient for your needs, please go ahead and use it! Running the code is as simple as this: <code>STATE = enum('GET_QUIZ, GET_VERSE, TEACH')</code></p>
<p>This recipe was edited and adapted after the author was inspired by seeing other, better enum recipes lying around.</p>
A simple enum type (Python)
2013-02-13T06:53:38-08:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578455-a-simple-enum-type/
<p style="color: grey">
Python
recipe 578455
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/enum/">enum</a>, <a href="/recipes/tags/metaclass/">metaclass</a>).
Revision 2.
</p>
<p>Inspired by various threads[1] on the python-ideas list, here's a simple enum type. Enums are generated either via the class syntax or via Enum.make().</p>
<pre class="prettyprint"><code>class Breakfast(Enum):
SPAM, HAM, EGGS
BACON, SAUSAGE
Breakfast = Enum.make("SPAM HAM EGGS BACON SAUSAGE")
</code></pre>
<p>Here are some of the features:</p>
<p>Enum:</p>
<ul>
<li>inheriting from an enum inherits copies of its values.</li>
<li>the export() method allows for exposing an enum's values in another namespace.</li>
</ul>
<p>Enum Values:</p>
<ul>
<li>the underlying values within an enum are essentially useless, diminishing the temptation to rely on them.</li>
<li>identity is equality, like with None, True, and False.</li>
<li>enum values support bitwise operations within the same enum.</li>
<li>the result of a bitwise operation is always the same object given the same inputs.</li>
</ul>
<p>[1] see <a href="http://mail.python.org/pipermail/python-ideas/2013-January/019003.html" rel="nofollow">http://mail.python.org/pipermail/python-ideas/2013-January/019003.html</a></p>
Simple enum mechanism (Python)
2012-01-15T12:30:31-08:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578015-simple-enum-mechanism/
<p style="color: grey">
Python
recipe 578015
by <a href="/recipes/users/4174477/">Thomas Lehmann</a>
(<a href="/recipes/tags/enum/">enum</a>).
</p>
<p><strong>Here are the basic ideas (orientation: C++)</strong>:</p>
<ul>
<li>You are responsible to find names for constants and this code provides a way to give values which differ from each other</li>
<li>The context is to allow different enums with values starting by 0</li>
<li>If you like to use constants like here: "Orientation.TOP" then place those constants in the relating class</li>
<li>You still can assign your own values</li>
</ul>
<p><strong>About the code</strong>:</p>
<ul>
<li>It's not much code also it might look like (implementation + documentation + unittests)</li>
<li>The __docformat__ is for epydoc. Temporarily remove the "@singleton" when trying to generate the HTML documentation (can not yet be handled by epydoc).</li>
</ul>
<p><strong>Example(s)</strong>:
Have a look at the unittests please.</p>
Python Immutable Enumerations Using Duck Punching (Python)
2012-10-18T19:41:22-07:00Josh Friendhttp://code.activestate.com/recipes/users/4183977/http://code.activestate.com/recipes/578294-python-immutable-enumerations-using-duck-punching/
<p style="color: grey">
Python
recipe 578294
by <a href="/recipes/users/4183977/">Josh Friend</a>
(<a href="/recipes/tags/duckpunching/">duckpunching</a>, <a href="/recipes/tags/enum/">enum</a>, <a href="/recipes/tags/enumeration/">enumeration</a>, <a href="/recipes/tags/monkeypatching/">monkeypatching</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Here's a fun method of creating enumerations in Python using dynamic class creation and duck-punching (monkey-patching). It works by creating a class called <code>enum</code> using the <code>type</code> metaclass. Duck-punching is then used to add properties to the class. the <code>fget</code> method of the property returns the enum value, but the <code>fset</code> and <code>fdel</code> methods throw exceptions, keeping the enumeration immutable. You can have enum values assigned automatically in sequence, assign them yourself, or have a mix of both.</p>
Yet again, one more enum for Python (Python)
2010-02-22T22:53:42-08:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/577063-yet-again-one-more-enum-for-python/
<p style="color: grey">
Python
recipe 577063
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/enum/">enum</a>).
</p>
<p>This is a fork of recipe #577024 which describes an enum type for Python using __slots__ and read-only variables. Apart from the enum with default positional argument values, this one adds another enum type using keyword arguments.</p>
Python Enum and/or named-integer class (Python)
2011-11-29T10:04:41-08:00Graham Poulterhttp://code.activestate.com/recipes/users/4172291/http://code.activestate.com/recipes/577921-python-enum-andor-named-integer-class/
<p style="color: grey">
Python
recipe 577921
by <a href="/recipes/users/4172291/">Graham Poulter</a>
(<a href="/recipes/tags/enum/">enum</a>).
Revision 6.
</p>
<p>Recipe for Python Enum class that builds on the "FOO = 1" pattern by adding a printable name. </p>
<p>In other words, an Enum instance is a named integer.</p>
First Class Enums in Python (Python)
2013-03-07T13:10:29-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578485-first-class-enums-in-python/
<p style="color: grey">
Python
recipe 578485
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/enum/">enum</a>, <a href="/recipes/tags/immutable/">immutable</a>).
</p>
<p>True immutable symbolic enumeration with qualified value access.</p>
C-style enumerations (Python)
2013-01-29T16:13:39-08:00Arthur Gardinerhttp://code.activestate.com/recipes/users/4185064/http://code.activestate.com/recipes/578438-c-style-enumerations/
<p style="color: grey">
Python
recipe 578438
by <a href="/recipes/users/4185064/">Arthur Gardiner</a>
(<a href="/recipes/tags/enum/">enum</a>, <a href="/recipes/tags/enumeration/">enumeration</a>).
</p>
<p>Fast and dirty C style enumerations specifically made for machine states but easily adapted. Not the most optimized implementation but a hardy read-only structure that I got tired of emailing around.</p>