From: whitequark Date: Sat, 25 May 2019 22:37:32 +0000 (+0000) Subject: build.dsl: make Pins and DiffPairs iterable. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=9bf8c6d34df47320f9c0971bf56f3c6ae1ed057e;p=nmigen.git build.dsl: make Pins and DiffPairs iterable. Returns pin names. --- 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")