Skip to content

Commit d1a2a04

Browse files
committed
repr: introduce one_liner_max_depth=3 (closes #104)
1 parent 91ab8cf commit d1a2a04

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

streamable/visitors/representation.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232

3333

3434
class ToStringVisitor(Visitor[str], ABC):
35-
def __init__(self) -> None:
35+
def __init__(self, one_liner_max_depth: int = 3) -> None:
3636
self.methods_reprs: List[str] = []
37+
self.one_liner_max_depth = one_liner_max_depth
3738

3839
@staticmethod
3940
@abstractmethod
@@ -182,10 +183,16 @@ def visit_atruncate_stream(self, stream: ATruncateStream) -> str:
182183
return stream.upstream.accept(self)
183184

184185
def visit_stream(self, stream: Stream) -> str:
186+
source_stream = f"Stream({self.to_string(stream.source)})"
187+
depth = len(self.methods_reprs) + 1
188+
if depth == 1:
189+
return source_stream
190+
if depth <= self.one_liner_max_depth:
191+
return f"{source_stream}.{'.'.join(reversed(self.methods_reprs))}"
185192
methods_block = "".join(
186193
map(lambda r: f" .{r}\n", reversed(self.methods_reprs))
187194
)
188-
return f"(\n Stream({self.to_string(stream.source)})\n{methods_block})"
195+
return f"(\n {source_stream}\n{methods_block})"
189196

190197

191198
class ReprVisitor(ToStringVisitor):

tests/test_stream.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,29 @@ class CustomCallable:
193193
complex_stream.display(logging.ERROR)
194194

195195
self.assertEqual(
196-
"""(
197-
Stream(range(0, 256))
198-
)""",
199196
str(Stream(src)),
197+
"Stream(range(0, 256))",
200198
msg="`repr` should work as expected on a stream without operation",
201199
)
202200
self.assertEqual(
201+
str(Stream(src).skip(10)),
202+
"Stream(range(0, 256)).skip(10, until=None)",
203+
msg="`repr` should return a one-liner for a stream with 1 operations",
204+
)
205+
self.assertEqual(
206+
str(Stream(src).skip(10).skip(10)),
207+
"Stream(range(0, 256)).skip(10, until=None).skip(10, until=None)",
208+
msg="`repr` should return a one-liner for a stream with 2 operations",
209+
)
210+
self.assertEqual(
211+
str(Stream(src).skip(10).skip(10).skip(10)),
203212
"""(
204213
Stream(range(0, 256))
205-
.map(<lambda>, concurrency=2, ordered=True, via='process')
214+
.skip(10, until=None)
215+
.skip(10, until=None)
216+
.skip(10, until=None)
206217
)""",
207-
str(Stream(src).map(lambda _: _, concurrency=2, via="process")),
208-
msg="`repr` should work as expected on a stream with 1 operation",
218+
msg="`repr` should go to line for a stream with 3 operations",
209219
)
210220
self.assertEqual(
211221
str(complex_stream),

0 commit comments

Comments
 (0)