Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Commit 3ca4111

Browse files
authored
Str repr indentation (#86)
* indent dataset repr * move repr tests to test_formatting.py * whatsnew
1 parent 1fef083 commit 3ca4111

File tree

4 files changed

+60
-57
lines changed

4 files changed

+60
-57
lines changed

datatree/formatting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def tree_repr(dt):
6969
if len(node.children) > 0:
7070
lines.append(f"{fill}{renderer.style.vertical}{line}")
7171
else:
72-
lines.append(f"{fill}{line}")
72+
lines.append(f"{fill}{' ' * len(renderer.style.vertical)}{line}")
7373

7474
# Tack on info about whether or not root node has a parent at the start
7575
first_line = lines[0]

datatree/tests/test_datatree.py

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import textwrap
2-
31
import pytest
42
import xarray as xr
53
import xarray.testing as xrt
@@ -337,57 +335,3 @@ class TestBrowsing:
337335

338336
class TestRestructuring:
339337
...
340-
341-
342-
class TestRepr:
343-
def test_print_empty_node(self):
344-
dt = DataTree(name="root")
345-
printout = dt.__str__()
346-
assert printout == "DataTree('root', parent=None)"
347-
348-
def test_print_empty_node_with_attrs(self):
349-
dat = xr.Dataset(attrs={"note": "has attrs"})
350-
dt = DataTree(name="root", data=dat)
351-
printout = dt.__str__()
352-
assert printout == textwrap.dedent(
353-
"""\
354-
DataTree('root', parent=None)
355-
Dimensions: ()
356-
Data variables:
357-
*empty*
358-
Attributes:
359-
note: has attrs"""
360-
)
361-
362-
def test_print_node_with_data(self):
363-
dat = xr.Dataset({"a": [0, 2]})
364-
dt = DataTree(name="root", data=dat)
365-
printout = dt.__str__()
366-
expected = [
367-
"DataTree('root', parent=None)",
368-
"Dimensions",
369-
"Coordinates",
370-
"a",
371-
"Data variables",
372-
"*empty*",
373-
]
374-
for expected_line, printed_line in zip(expected, printout.splitlines()):
375-
assert expected_line in printed_line
376-
377-
def test_nested_node(self):
378-
dat = xr.Dataset({"a": [0, 2]})
379-
root = DataTree(name="root")
380-
DataTree(name="results", data=dat, parent=root)
381-
printout = root.__str__()
382-
assert printout.splitlines()[2].startswith(" ")
383-
384-
def test_print_datatree(self):
385-
dt = create_test_datatree()
386-
print(dt)
387-
388-
# TODO work out how to test something complex like this
389-
390-
def test_repr_of_node_with_data(self):
391-
dat = xr.Dataset({"a": [0, 2]})
392-
dt = DataTree(name="root", data=dat)
393-
assert "Coordinates" in repr(dt)

datatree/tests/test_formatting.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,62 @@
55
from datatree import DataTree
66
from datatree.formatting import diff_tree_repr
77

8+
from .test_datatree import create_test_datatree
9+
10+
11+
class TestRepr:
12+
def test_print_empty_node(self):
13+
dt = DataTree(name="root")
14+
printout = dt.__str__()
15+
assert printout == "DataTree('root', parent=None)"
16+
17+
def test_print_empty_node_with_attrs(self):
18+
dat = Dataset(attrs={"note": "has attrs"})
19+
dt = DataTree(name="root", data=dat)
20+
printout = dt.__str__()
21+
assert printout == dedent(
22+
"""\
23+
DataTree('root', parent=None)
24+
Dimensions: ()
25+
Data variables:
26+
*empty*
27+
Attributes:
28+
note: has attrs"""
29+
)
30+
31+
def test_print_node_with_data(self):
32+
dat = Dataset({"a": [0, 2]})
33+
dt = DataTree(name="root", data=dat)
34+
printout = dt.__str__()
35+
expected = [
36+
"DataTree('root', parent=None)",
37+
"Dimensions",
38+
"Coordinates",
39+
"a",
40+
"Data variables",
41+
"*empty*",
42+
]
43+
for expected_line, printed_line in zip(expected, printout.splitlines()):
44+
assert expected_line in printed_line
45+
46+
def test_nested_node(self):
47+
dat = Dataset({"a": [0, 2]})
48+
root = DataTree(name="root")
49+
DataTree(name="results", data=dat, parent=root)
50+
printout = root.__str__()
51+
assert printout.splitlines()[2].startswith(" ")
52+
53+
def test_print_datatree(self):
54+
dt = create_test_datatree()
55+
print(dt)
56+
57+
# TODO work out how to test something complex like this
58+
59+
def test_repr_of_node_with_data(self):
60+
dat = Dataset({"a": [0, 2]})
61+
dt = DataTree(name="root", data=dat)
62+
assert "Coordinates" in repr(dt)
63+
864

965
class TestDiffFormatting:
1066
def test_diff_structure(self):

docs/source/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ Deprecations
6060
Bug fixes
6161
~~~~~~~~~
6262

63+
- Fixed indentation issue with the string repr (:pull:`86`)
64+
By `Tom Nicholas <https://github.com/TomNicholas>`_.
65+
6366
Documentation
6467
~~~~~~~~~~~~~
6568

0 commit comments

Comments
 (0)