Skip to content

Commit

Permalink
Types now have a 'show' command (that indents).
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski committed Apr 19, 2022
1 parent 56802fe commit 7f733d5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 35 deletions.
5 changes: 5 additions & 0 deletions src/awkward/_v2/types/arraytype.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE

import sys

import awkward as ak
import awkward._v2.types.type
from awkward._v2.forms.form import _parameters_equal
Expand Down Expand Up @@ -37,6 +39,9 @@ def length(self):
def __str__(self):
return "".join(self._str("", True))

def show(self, stream=sys.stdout):
stream.write("".join(self._str("", False) + ["\n"]))

def _str(self, indent, compact):
return [str(self._length) + " * "] + self._content._str(indent, compact)

Expand Down
76 changes: 47 additions & 29 deletions src/awkward/_v2/types/recordtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from collections.abc import Iterable

import json

import awkward as ak
import awkward._v2._prettyprint
from awkward._v2.types.type import Type
Expand Down Expand Up @@ -79,53 +77,73 @@ def _str(self, indent, compact):
out = [self._typestr]

else:
children = [x._str(indent, compact) for x in self._contents]
if compact:
pre, post = "", ""
else:
pre, post = "\n" + indent + " ", "\n" + indent

children = []
for i, x in enumerate(self._contents):
if i + 1 < len(self._contents):
if compact:
y = x._str(indent, compact) + [", "]
else:
y = x._str(indent + " ", compact) + [",\n", indent, " "]
else:
if compact:
y = x._str(indent, compact)
else:
y = x._str(indent + " ", compact)
children.append(y)

params = self._str_parameters()
name = self.parameter("__record__")

if not self.is_tuple:
pairs = []
for k, v in zip(self._fields, children):
if ak._v2._prettyprint.is_identifier.match(k) is None:
key_str = repr(k)
if key_str.startswith("u"):
key_str = key_str[1:]
else:
key_str = k
pairs.append([key_str, ": "] + v)
flat_pairs = [y for x in pairs for y in x]

if params is None:
if self.is_tuple:
with_commas = [y for x in children for y in x + [", "]][:-1]
flat_children = [y for x in children for y in x]
if name is None:
out = ["("] + with_commas + [")"]
out = ["(", pre] + flat_children + [post, ")"]
else:
out = [name, "["] + with_commas + ["]"]
out = [name, "[", pre] + flat_children + [post, "]"]
else:
pairs = []
for k, v in zip(self._fields, children):
if ak._v2._prettyprint.is_identifier.match(k) is None:
key_str = repr(k)
if key_str.startswith("u"):
key_str = key_str[1:]
else:
key_str = k
pairs.append([key_str, ": "] + v)
with_commas = [y for x in pairs for y in x + [", "]][:-1]
if name is None:
out = ["{"] + with_commas + ["}"]
out = ["{", pre] + flat_pairs + [post, "}"]
else:
out = [name, "["] + with_commas + ["]"]
out = [name, "[", pre] + flat_pairs + [post, "]"]

else:
if self.is_tuple:
with_commas = [y for x in children for y in x + [", "]][:-1]
flat_children = [y for x in children for y in x]
if name is None:
out = ["tuple[["] + with_commas + ["], ", params, "]"]
out = (
["tuple[[", pre]
+ flat_children
+ [post, "], ", params, "]"]
)
else:
out = [name, "["] + with_commas + [", ", params, "]"]
out = (
[name, "[", pre] + flat_children + [", ", post, params, "]"]
)
else:
if name is None:
with_commas = [y for x in children for y in x + [", "]][:-1]
fields = [json.dumps(x) for x in self._fields]
out = (
["struct[[", ", ".join(fields), "], ["]
+ with_commas
+ ["], ", params, "]"]
["struct[{", pre] + flat_pairs + [post, "}, ", params, "]"]
)
else:
pairs = [[k, ": "] + v for k, v in zip(self._fields, children)]
with_commas = [y for x in pairs for y in x + [", "]][:-1]
out = [name, "["] + with_commas + [", ", params, "]"]
out = [name, "[", pre] + flat_pairs + [", ", post, params, "]"]

return [self._str_categorical_begin()] + out + [self._str_categorical_end()]

Expand Down
4 changes: 4 additions & 0 deletions src/awkward/_v2/types/type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE

import json
import sys

import awkward as ak

Expand All @@ -25,6 +26,9 @@ def typestr(self):
def __str__(self):
return "".join(self._str("", True))

def show(self, stream=sys.stdout):
stream.write("".join(self._str("", False) + ["\n"]))

_str_parameters_exclude = ("__categorical__",)

def _str_categorical_begin(self):
Expand Down
26 changes: 22 additions & 4 deletions src/awkward/_v2/types/uniontype.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,32 @@ def _str(self, indent, compact):
out = [self._typestr]

else:
children = [x._str(indent, compact) for x in self._contents]
with_commas = [y for x in children for y in x + [", "]][:-1]
if compact:
pre, post = "", ""
else:
pre, post = "\n" + indent + " ", "\n" + indent

children = []
for i, x in enumerate(self._contents):
if i + 1 < len(self._contents):
if compact:
y = x._str(indent, compact) + [", "]
else:
y = x._str(indent + " ", compact) + [",\n", indent, " "]
else:
if compact:
y = x._str(indent, compact)
else:
y = x._str(indent + " ", compact)
children.append(y)

flat_children = [y for x in children for y in x]
params = self._str_parameters()

if params is None:
out = ["union["] + with_commas + ["]"]
out = ["union[", pre] + flat_children + [post, "]"]
else:
out = ["union["] + with_commas + [", ", params, "]"]
out = ["union[", pre] + flat_children + [", ", post, params, "]"]

return [self._str_categorical_begin()] + out + [self._str_categorical_end()]

Expand Down
4 changes: 2 additions & 2 deletions tests/v2/test_0914-types-and-forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def test_RecordType():
{"x": 123},
)
)
== 'struct[["x", "y"], [unknown, bool], parameters={"x": 123}]'
== 'struct[{x: unknown, y: bool}, parameters={"x": 123}]'
)
assert (
str(
Expand Down Expand Up @@ -876,7 +876,7 @@ def test_RecordType():
{"x": 123, "__categorical__": True},
)
)
== 'categorical[type=struct[["x", "y"], [unknown, bool], parameters={"x": 123}]]'
== 'categorical[type=struct[{x: unknown, y: bool}, parameters={"x": 123}]]'
)
assert (
str(
Expand Down

0 comments on commit 7f733d5

Please sign in to comment.