

Python Enums for Humans - wuster
https://gist.github.com/1920153
I've been doing Python web apps for a few years. All of them have involved some pattern of declaring runtime vars to represent DB values, and also mapping those DB values to user-facing display values in your view templates. The practice of keeping the { var --&#62; value } and { value --&#62; display string } maps separately led to annoying dev-time bugs. Here's my short and easy solution.<p>MIT License. Enjoy.
======
stevek
Does nobody else find the repetition objectionable? e.g. NEW = ('N', 'New'),
If your values have good names, you can easily deduce "Some Constant" from
SOME_CONSTANT or vice-versa. While I'm at it, having abbreviations built-in
seems iffy too.

~~~
wuster
The apparent repetition is due to the triviality of the example. Imagine if
you had to translate the display string or change the display name without
renaming all of your code references to the constant.

------
jaspersfather
What makes that better than just using strings?

~~~
wuster
[ Edit: add more context ]

My use case for this is to map between the database and application logic.

In the example, the short string 'N' is for storage in a db column, *.NEW is
for use in code, and "New" is for use to display in an UI. You don't
necessarily want to persist the display or var name because you may want to
rename them in the app later. IMO it's a bad idea to store display
representations as data.

