[tor-commits] [stem/master] Enum constructor function for capitalized values

atagar at torproject.org atagar at torproject.org
Sun Jan 29 08:54:42 UTC 2012


commit db9d695f16dc2f2ec409be40aa69d7d13cf24826
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Jan 28 11:22:38 2012 -0800

    Enum constructor function for capitalized values
    
    On occasion we want enumerations where the values are capitalized (for instance
    logging runlevels and integ targets). This could be done in a succinct fasion
    via a bit of python hackery but it wasn't pretty. Providing a function to do
    this instead.
---
 run_tests.py           |    4 ++--
 stem/util/enum.py      |   23 ++++++++++++++++++++++-
 stem/util/log.py       |    5 +----
 test/unit/util/enum.py |    9 +++++++++
 4 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index d4d2fd6..8467e2f 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -51,7 +51,7 @@ CONFIG = stem.util.conf.config_dict("test", {
   "target.torrc": {},
 })
 
-Target = stem.util.enum.Enum(*[(v, v) for v in (
+Target = stem.util.enum.UppercaseEnum(
   "ONLINE",
   "RELATIVE",
   "RUN_NONE",
@@ -63,7 +63,7 @@ Target = stem.util.enum.Enum(*[(v, v) for v in (
   "RUN_SCOOKIE",
   "RUN_PTRACE",
   "RUN_ALL",
-)])
+)
 
 DEFAULT_RUN_TARGET = Target.RUN_OPEN
 
diff --git a/stem/util/enum.py b/stem/util/enum.py
index c750352..ccc9590 100644
--- a/stem/util/enum.py
+++ b/stem/util/enum.py
@@ -15,11 +15,13 @@ with overwritten string counterparts:
 'Cat'
 
 to_camel_case - converts a string to camel case
+UppercaseEnum - Provides an enum instance with capitalized values.
 Enum - Provides a basic, ordered  enumeration.
-  |- values - string representation of our enums
+  |- keys - string representation of our enum keys
   |- index_of - indice of an enum value
   |- next - provides the enum after a given enum value
   |- previous - provides the enum before a given value
+  |- __getitem__ - provides the value for an enum key
   +- __iter__ - iterator over our enum keys
 """
 
@@ -42,6 +44,25 @@ def to_camel_case(label, word_divider = " "):
   
   return word_divider.join(words)
 
+def UppercaseEnum(*args):
+  """
+  Provides an Enum instance where the values are identical to the keys. Since
+  the keys are uppercase by convention this means the values are too. For
+  instance...
+  
+  >>> runlevels = UppercaseEnum("DEBUG", "INFO", "NOTICE", "WARN", "ERROR")
+  >>> runlevels.DEBUG
+  'DEBUG'
+  
+  Arguments:
+    args (str) - list of enum keys to initialize with
+  
+  Returns:
+    stem.util.Enum instance with the given keys
+  """
+  
+  return Enum(*[(v, v) for v in args])
+
 class Enum:
   """
   Basic enumeration.
diff --git a/stem/util/log.py b/stem/util/log.py
index 65d5659..a25e3fd 100644
--- a/stem/util/log.py
+++ b/stem/util/log.py
@@ -36,10 +36,7 @@ import stem.util.enum
 # Logging runlevels. These are *very* commonly used so including shorter
 # aliases (so they can be referenced as log.DEBUG, log.WARN, etc).
 
-Runlevel = stem.util.enum.Enum(
-  ("TRACE", "TRACE"),   ("DEBUG", "DEBUG"), ("INFO", "INFO"),
-  ("NOTICE", "NOTICE"), ("WARN", "WARN"),   ("ERROR", "ERROR"))
-
+Runlevel = stem.util.enum.UppercaseEnum("TRACE", "DEBUG", "INFO", "NOTICE", "WARN", "ERROR")
 TRACE, DEBUG, INFO, NOTICE, WARN, ERR = list(Runlevel)
 
 # mapping of runlevels to the logger module's values, TRACE and DEBUG aren't
diff --git a/test/unit/util/enum.py b/test/unit/util/enum.py
index b688a27..5a28f62 100644
--- a/test/unit/util/enum.py
+++ b/test/unit/util/enum.py
@@ -35,6 +35,15 @@ class TestEnum(unittest.TestCase):
     self.assertEquals("Skippy", pets.DOG)
     self.assertEquals("Cat", pets.CAT)
   
+  def test_uppercase_enum_example(self):
+    """
+    Checks that the pydoc example for the UppercaseEnum constructor function is
+    accurate.
+    """
+    
+    runlevels = stem.util.enum.UppercaseEnum("DEBUG", "INFO", "NOTICE", "WARN", "ERROR")
+    self.assertEquals("DEBUG", runlevels.DEBUG)
+  
   def test_enum_methods(self):
     """
     Exercises enumeration methods.





More information about the tor-commits mailing list