473,387 Members | 1,582 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,387 software developers and data experts.

enum 0.3: Enumerations in Python

Howdy all,

I've uploaded enum 0.3 to the Cheeseshop.

<URL:http://cheeseshop.python.org/pypi/enum/>

Enumerations are now sequences, iterable (as before) *and* indexable::
from enum import Enum
Weekdays = Enum('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
pizza_night = Weekdays[4]
print pizza_night == Weekdays.thu True

Since the values of an enumeration are directly reflected in the
values and attributes, Enum instances are immutable to preserve this
relationship::
Weekdays.foo = object() [...]
enum.EnumImmutableError: Enumeration does not allow modification Weekdays.mon = Weekdays.fri [...]
enum.EnumImmutableError: Enumeration does not allow modification Weekdays[4] = object() [...]
enum.EnumImmutableError: Enumeration does not allow modification
The package's long description:

"""This package provides a class for robust enumerations in Python.

An enumeration object is created with a sequence of string
arguments to the Enum() function::
from enum import Enum
Colours = Enum('red', 'blue', 'green')
Weekdays = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
The return value is an immutable sequence object with a value for each
of the string arguments. Each value is also available as an attribute
named from the corresponding string argument::
pizza_night = Weekdays[4]
pixel_colour = Colours.blue


The values are constants that can be compared only with other values
from the same enumeration, but can be coerced to simple strings
matching the original arguments to Enum().

The design is based in part on Zoran Isailovski's recipe, "First
Class Enums in Python" in the ASPN Python Cookbook
<URL:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413486>.
"""

--
\ "I bought some batteries, but they weren't included; so I had |
`\ to buy them again." -- Steven Wright |
_o__) |
Ben Finney
Nov 26 '05 #1
0 1279

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

Similar topics

12
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using...
1
by: mikron30 | last post by:
Hi, Is it legal to create two enums with the same value? typedef enum { a, b=a, c } save_value_enum;
21
by: Bilgehan.Balban | last post by:
Hi, I have two different enum definitions with members of same name. The compiler complains about duplicate definitions. Is this expected behaviour? Can't I have same-named fields in...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
0
by: Ben Finney | last post by:
Howdy all, I've uploaded enum 0.2 to the Cheeseshop. The main change is that enumeration values now export their enumtype, index, and key (the things that define each value) as attributes....
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
3
by: MathStuf | last post by:
I am creating a program that uses a lot of enumerations and associated arrays of strings that look like this: enum Enum1 { E1_NONE = -1, E1_A = 0, E1_B = 1, E1_END = 2 }
4
by: Jure Bogataj | last post by:
Hi all! I have two enumerations: public enum MyEnum1 and public enum MyEnum2 I want to create function that accepts parameter as member of either enumeration. Is this at all possible?
0
by: tony_in_da_uk | last post by:
*** I posted this on comp.lang.c++.moderated a couple days ago, and got a near-flippant response from someone who clearly didn't take the time to consider the import. I replied, and it's still not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.