From 9bf8c6d34df47320f9c0971bf56f3c6ae1ed057e Mon Sep 17 00:00:00 2001 From: whitequark Date: Sat, 25 May 2019 22:37:32 +0000 Subject: [PATCH] build.dsl: make Pins and DiffPairs iterable. Returns pin names. --- nmigen/build/dsl.py | 12 ++++++++++++ nmigen/test/test_build_dsl.py | 2 ++ 2 files changed, 14 insertions(+) diff --git a/nmigen/build/dsl.py b/nmigen/build/dsl.py index f3e88f0..4ba05c4 100644 --- a/nmigen/build/dsl.py +++ b/nmigen/build/dsl.py @@ -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)) diff --git a/nmigen/test/test_build_dsl.py b/nmigen/test/test_build_dsl.py index 9af7726..6a1c1cc 100644 --- a/nmigen/test/test_build_dsl.py +++ b/nmigen/test/test_build_dsl.py @@ -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") -- 2.30.2