title: Python Enum class (with tostring fromstring)
body: <p>I've found a simply way to implement(hack) an enum into Python:</p>
  
  <pre><code>class MyEnum:
    VAL1, VAL2, VAL3 = range(3)
  </code></pre>
  
  <p>I can then call this as such:</p>
  
  <pre><code>bob = MyEnum.VAL1
  </code></pre>
  
  <p>Sexy!</p>
  
  <p>Alright, now I want to be able to get both the numerical value if given a string, or a string if given a numerical value.  Let's say I want the strings to exactly match up to the Enum key's</p>
  
  <p>The best I could think of is something like this:</p>
  
  <pre><code>class MyEnum:
    VAL1, VAL2, VAL3 = range(3)
    @classmethod
    def tostring(cls, val):
      if (val == cls.VAL1):
        return "VAL1"
      elif (val == cls.VAL2):
        return "VAL2"
      elif (val == cls.VAL3):
        return "VAL3"
      else:
        return None
    @classmethod
    def fromstring(cls, str):
      if (str.upper() == "VAL1"):
        return cls.VAL1
      elif (str.upper() == "VAL2"):
        return cls.VAL2
      elif (str.upper() == "VAL2"):
        return cls.VAL2
      else:
        return None
  </code></pre>
  
  <p>or something like that (ignore how i'm catching invalid cases)</p>
  
  <p>Is there a better, more python centric way to do what I'm doing above? Or is the above already as concise as it gets.</p>
  
  <p>It seems like there's got to be a better way to do it.</p>