From: TJ Date: Sat, 16 Nov 2013 13:49:46 +0000 (+0000) Subject: Add a utility library for useful functions: add C-style pseudo-enum type X-Git-Url: https://iam.tj/gitweb/gitweb.cgi?p=base2-runner.git;a=commitdiff_plain;h=5dbdfbd6b82fff50940d8b761c9e56fcaf50633c Add a utility library for useful functions: add C-style pseudo-enum type --- diff --git a/library.py b/library.py new file mode 100644 index 0000000..86a2deb --- /dev/null +++ b/library.py @@ -0,0 +1,8 @@ +def enum(*sequential, **named): + """ simulation of C=style enum + credit: Alec Thomas, http://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python + """ + enums = dict(zip(sequential, range(len(sequential))), **named) + reverse = dict((value, key) for key, value in enums.iteritems()) + enums['reverse_mapping'] = reverse + return type('Enum', (), enums)