Skip to content

Commit

Permalink
tuple -> tuple[type1, ... typen] in tree_summary type row
Browse files Browse the repository at this point in the history
  • Loading branch information
ASEM000 committed Mar 29, 2024
1 parent 810983b commit 07af95f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions sepes/_src/tree_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,21 @@ def _(node: Any) -> str:
return tree_repr(ShapeDTypePP(shape, dtype))


@tree_summary.def_type(tuple)
def _(node: tuple) -> str:
# - output tuple[types,...] instead of just tuple in the type row.
# - usually this encounterd if the tree_summary depth is not inf
# so the tree leaves could contain non-atomic types.
treelib = sepes._src.backend.treelib

one_level_types = treelib.map(
tree_summary.type_dispatcher,
node,
is_leaf=lambda x: False if id(x) == id(node) else True,
)
return f"tuple[{','.join(one_level_types)}]"


if is_package_avaiable("jax"):
# jax pretty printing extra handlers
import jax
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_tree_summary():
assert (
tree_summary(r1, depth=1)
# trunk-ignore(flake8/E501)
== "┌────┬────────────┬─────┬───────┐\n│Name│Type │Count│Size │\n├────┼────────────┼─────┼───────┤\n│.a │int │1 │ │\n├────┼────────────┼─────┼───────┤\n│.b │str │1 │ │\n├────┼────────────┼─────┼───────┤\n│.c │float │1 │ │\n├────┼────────────┼─────┼───────┤\n│.d │str │1 │ │\n├────┼────────────┼─────┼───────┤\n│.e │list │5 │ │\n├────┼────────────┼─────┼───────┤\n│.f │set │1 │ │\n├────┼────────────┼─────┼───────┤\n│.g │dict │27 │100.00B│\n├────┼────────────┼─────┼───────┤\n│.h │f32[5,1] │5 │20.00B │\n├────┼────────────┼─────┼───────┤\n│.i │f32[1,6] │6 │24.00B │\n├────┼────────────┼─────┼───────┤\n│.j │f32[1,1,4,5]│20 │80.00B │\n├────┼────────────┼─────┼───────┤\n│.k │tuple │3 │ │\n├────┼────────────┼─────┼───────┤\n│.l │a │2 │ │\n├────┼────────────┼─────┼───────┤\n│.m │f32[5,5] │25 │100.00B│\n├────┼────────────┼─────┼───────┤\n│.n │bool[] │1 │1.00B │\n├────┼────────────┼─────┼───────┤\n│.o │c64[2] │2 │16.00B │\n├────┼────────────┼─────┼───────┤\n│Σ │Repr1 │101 │341.00B│\n└────┴────────────┴─────┴───────┘"
== '┌────┬──────────────────┬─────┬───────┐\n│Name│Type │Count│Size │\n├────┼──────────────────┼─────┼───────┤\n│.a │int │1 │ │\n├────┼──────────────────┼─────┼───────┤\n│.b │str │1 │ │\n├────┼──────────────────┼─────┼───────┤\n│.c │float │1 │ │\n├────┼──────────────────┼─────┼───────┤\n│.d │str │1 │ │\n├────┼──────────────────┼─────┼───────┤\n│.e │list │5 │ │\n├────┼──────────────────┼─────┼───────┤\n│.f │set │1 │ │\n├────┼──────────────────┼─────┼───────┤\n│.g │dict │27 │100.00B│\n├────┼──────────────────┼─────┼───────┤\n│.h │f32[5,1] │5 │20.00B │\n├────┼──────────────────┼─────┼───────┤\n│.i │f32[1,6] │6 │24.00B │\n├────┼──────────────────┼─────┼───────┤\n│.j │f32[1,1,4,5] │20 │80.00B │\n├────┼──────────────────┼─────┼───────┤\n│.k │tuple[int,int,int]│3 │ │\n├────┼──────────────────┼─────┼───────┤\n│.l │tuple[int,int] │2 │ │\n├────┼──────────────────┼─────┼───────┤\n│.m │f32[5,5] │25 │100.00B│\n├────┼──────────────────┼─────┼───────┤\n│.n │bool[] │1 │1.00B │\n├────┼──────────────────┼─────┼───────┤\n│.o │c64[2] │2 │16.00B │\n├────┼──────────────────┼─────┼───────┤\n│Σ │Repr1 │101 │341.00B│\n└────┴──────────────────┴─────┴───────┘'
)

assert (
Expand Down

0 comments on commit 07af95f

Please sign in to comment.