Skip to content

Commit a1b0ac9

Browse files
committed
Define __repr__ for WireVectors.
This helps with debugging. Before: ``` >>> import pyrtl >>> w1 = pyrtl.WireVector(name="w1", bitwidth=1) >>> w2 = pyrtl.WireVector(name="w2", bitwidth=2) >>> l = [w1, w2] >>> l [<pyrtl.wire.WireVector object at 0x7a3307d84ad0>, <pyrtl.wire.WireVector object at 0x7a3307fc9d10>] ``` After: ``` >>> import pyrtl >>> w1 = pyrtl.WireVector(name="w1", bitwidth=1) >>> w2 = pyrtl.WireVector(name="w2", bitwidth=2) >>> l = [w1, w2] >>> l [w1/1W, w2/2W] ```
1 parent 2049f28 commit a1b0ac9

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pyrtl/wire.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ def __hash__(self):
159159

160160
def __str__(self):
161161
"""A string representation of the wire in 'name/bitwidth code' form."""
162-
return ''.join([self.name, '/', str(self.bitwidth), self._code])
162+
return f'{self.name}/{self.bitwidth}{self._code}'
163+
164+
def __repr__(self):
165+
return str(self)
163166

164167
def _validate_bitwidth(self, bitwidth):
165168
if bitwidth is not None:
@@ -934,6 +937,9 @@ def __hash__(self):
934937
def __str__(self):
935938
return str(self.wire)
936939

940+
def __repr__(self):
941+
return repr(self.wire)
942+
937943
def __ilshift__(self, other):
938944
self.wire <<= other
939945
return self

tests/test_wire.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ def test_ops(self):
487487

488488
self.assertEqual(hash(a), hash(wrapped_a))
489489
self.assertEqual(str(a), str(wrapped_a))
490+
self.assertEqual(repr(a), repr(wrapped_a))
490491
self.assertEqual(len(a), len(wrapped_a))
491492

492493
a_and_b = pyrtl.wire.WireVector(name='a_and_b', bitwidth=4)

0 commit comments

Comments
 (0)