From: Cefn Hoile Date: Fri, 16 Feb 2018 21:25:58 +0000 (+0000) Subject: Module for util objects supporting Micro/CircuitPython translation X-Git-Tag: 0.1.0~4^2~159 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/dd4f50c67a9add29c85a981101e3df07e90dfee3 Module for util objects supporting Micro/CircuitPython translation --- diff --git a/python/mcp/__init__.py b/python/mcp/__init__.py new file mode 100644 index 0000000..d52c0b9 --- /dev/null +++ b/python/mcp/__init__.py @@ -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 index 0000000..01545f6 --- /dev/null +++ b/python/testing/mcp.py @@ -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