build.dsl: make Pins and DiffPairs iterable.
authorwhitequark <whitequark@whitequark.org>
Sat, 25 May 2019 22:37:32 +0000 (22:37 +0000)
committerwhitequark <whitequark@whitequark.org>
Sat, 25 May 2019 22:43:48 +0000 (22:43 +0000)
Returns pin names.

nmigen/build/dsl.py
nmigen/test/test_build_dsl.py

index f3e88f01b011a54b6aecf41213cc6c202909d656..4ba05c48203a2a177301ba5e8f25cf0678bc0e22 100644 (file)
@@ -13,6 +13,12 @@ class Pins:
                             .format(dir))
         self.dir = dir
 
+    def __len__(self):
+        return len(self.names)
+
+    def __iter__(self):
+        return iter(self.names)
+
     def __repr__(self):
         return "(pins {} {})".format(self.dir, " ".join(self.names))
 
@@ -29,6 +35,12 @@ class DiffPairs:
 
         self.dir = dir
 
+    def __len__(self):
+        return len(self.p.names)
+
+    def __iter__(self):
+        return zip(self.p.names, self.n.names)
+
     def __repr__(self):
         return "(diffpairs {} (p {}) (n {}))".format(
             self.dir, " ".join(self.p.names), " ".join(self.n.names))
index 9af7726b038728935a6cb09c799f92df1626465e..6a1c1cc7753e1c7532225d26e590b4039c1f8509 100644 (file)
@@ -8,6 +8,7 @@ class PinsTestCase(FHDLTestCase):
         self.assertEqual(repr(p), "(pins io A0 A1 A2)")
         self.assertEqual(len(p.names), 3)
         self.assertEqual(p.dir, "io")
+        self.assertEqual(list(p), ["A0", "A1", "A2"])
 
     def test_wrong_names(self):
         with self.assertRaises(TypeError,
@@ -27,6 +28,7 @@ class DiffPairsTestCase(FHDLTestCase):
         self.assertEqual(dp.p.names, ["A0", "A1"])
         self.assertEqual(dp.n.names, ["B0", "B1"])
         self.assertEqual(dp.dir, "io")
+        self.assertEqual(list(dp), [("A0", "B0"), ("A1", "B1")])
 
     def test_dir(self):
         dp = DiffPairs("A0", "B0", dir="o")