]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
Module for util objects supporting Micro/CircuitPython translation
authorCefn Hoile <github.com@cefn.com>
Fri, 16 Feb 2018 21:25:58 +0000 (21:25 +0000)
committerCefn Hoile <github.com@cefn.com>
Fri, 16 Feb 2018 21:25:58 +0000 (21:25 +0000)
python/mcp/__init__.py [new file with mode: 0644]
python/testing/mcp.py [new file with mode: 0644]

diff --git a/python/mcp/__init__.py b/python/mcp/__init__.py
new file mode 100644 (file)
index 0000000..d52c0b9
--- /dev/null
@@ -0,0 +1,21 @@
+class Enum(object):
+
+
+    @classmethod
+    def iteritems(cls):
+        for key in dir(cls):
+            val = getattr(cls, key)
+            if type(val) is cls:
+                yield (key, val)
+
+
+    def __repr__(self):
+        """
+        Assumes instance will be found as attribute of own
+        class. Returns dot-subscripted path to instance
+        """
+        cls = type(self)
+        for key in dir(cls):
+            if getattr(cls, key) is self:
+                return "{}.{}.{}".format(cls.__module__, cls.__qualname__, key)
+        return repr(self)
\ No newline at end of file
diff --git a/python/testing/mcp.py b/python/testing/mcp.py
new file mode 100644 (file)
index 0000000..01545f6
--- /dev/null
@@ -0,0 +1,31 @@
+import unittest
+
+
+class TestEnum(unittest.TestCase):
+
+    def setUp(self):
+        import mcp
+        class Cls(mcp.Enum):
+            pass
+        Cls.one = Cls()
+        Cls.two = Cls()
+        # class refs would be implicitly populated correctly in a real module
+        Cls.__module__ = "ho.hum"
+        Cls.__qualname__ = "Example"
+        self.Cls = Cls
+
+
+    def test_iteritems(self):
+        items = list(self.Cls.iteritems())
+        self.assertEqual( items, [("one",self.Cls.one),("two",self.Cls.two),])
+
+
+    def test_repr(self):
+        name = "one"
+        actual = repr(getattr(self.Cls, name))
+        expected = "{}.{}.{}".format(self.Cls.__module__, self.Cls.__qualname__, name)
+        self.assertEqual( actual, expected)
+
+
+    def test_str(self):
+        self.assertEqual(str(self.Cls.one), repr(self.Cls.one))
\ No newline at end of file