Skip to content

Commit 9d23167

Browse files
authored
Add sort_table option (#261)
1 parent c115253 commit 9d23167

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

docs/source/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
77

88
## 0.2.1 - 2022-xx-xx
99

10+
- {pull}`261` adds a config file option to sort entries in live table
1011
- {pull}`259` adds an `.svg` for profiling tasks.
1112

1213
## 0.2.0 - 2022-04-14

docs/source/reference_guides/configuration.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ n_entries_in_table = "all" # default 15
144144
145145
````
146146

147+
````{confval} sort_table
148+
149+
You can decide whether the entries displayed in the live table are sorted alphabetically
150+
once all tasks have been executed, which can be helpful when working with many tasks.
151+
Use either `true` or `false`. This option is only supported in the configuration file.
152+
153+
```toml
154+
sort_table = false # default: true
155+
```
156+
157+
````
158+
147159
````{confval} paths
148160
149161
If you want to collect tasks from specific paths without passing the names via the

src/_pytask/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ def pytask_parse_config(
246246
callback=lambda x: x if x is None else int(x),
247247
)
248248

249+
config["sort_table"] = convert_truthy_or_falsy_to_bool(
250+
config_from_file.get("sort_table", True)
251+
)
252+
249253

250254
@hookimpl
251255
def pytask_post_parse(config: dict[str, Any]) -> None:

src/_pytask/live.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def pytask_post_parse(config: dict[str, Any]) -> None:
8585
n_entries_in_table=config["n_entries_in_table"],
8686
verbose=config["verbose"],
8787
editor_url_scheme=config["editor_url_scheme"],
88+
sort_final_table=config["sort_table"],
8889
)
8990
config["pm"].register(live_execution, "live_execution")
9091

@@ -156,6 +157,7 @@ class LiveExecution:
156157
n_entries_in_table = attr.ib(type=int)
157158
verbose = attr.ib(type=int)
158159
editor_url_scheme = attr.ib(type=str)
160+
sort_final_table = attr.ib(default=False, type=bool)
159161
n_tasks = attr.ib(default="x", type=Union[int, str])
160162
_reports = attr.ib(factory=list, type=List[Dict[str, Any]])
161163
_running_tasks = attr.ib(factory=dict, type=Dict[str, Task])
@@ -168,7 +170,7 @@ def pytask_execute_build(self) -> Generator[None, None, None]:
168170
yield
169171
self.live_manager.stop(transient=True)
170172
table = self._generate_table(
171-
reduce_table=False, sort_table=True, add_caption=False
173+
reduce_table=False, sort_table=self.sort_final_table, add_caption=False
172174
)
173175
if table is not None:
174176
console.print(table)

tests/test_live.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import re
34
import textwrap
45
from contextlib import ExitStack as does_not_raise # noqa: N813
56

@@ -306,6 +307,35 @@ def task_create_file(produces):
306307
assert f"{i}.txt" in result.output
307308

308309

310+
@pytest.mark.end_to_end
311+
@pytest.mark.parametrize("sort_table", [True, False])
312+
def test_sort_table_option(tmp_path, runner, sort_table):
313+
source = """
314+
import pytask
315+
316+
def task_a(produces):
317+
pass
318+
319+
@pytask.mark.try_first
320+
def task_b():
321+
pass
322+
323+
"""
324+
tmp_path.joinpath("task_order.py").write_text(textwrap.dedent(source))
325+
326+
config = f"[tool.pytask.ini_options]\nsort_table = '{sort_table}'"
327+
tmp_path.joinpath("pyproject.toml").write_text(textwrap.dedent(config))
328+
329+
result = runner.invoke(cli, [tmp_path.as_posix()])
330+
assert result.exit_code == ExitCode.OK
331+
332+
lines = result.output.split("\n")
333+
task_names = [re.findall("task_[a|b]", line) for line in lines]
334+
task_names = [name[0][-1] for name in task_names if name != []]
335+
expected = ["a", "b"] if sort_table else ["b", "a"]
336+
assert expected == task_names
337+
338+
309339
@pytest.mark.end_to_end
310340
def test_execute_w_partialed_functions(tmp_path, runner):
311341
"""Test with partialed function which make it harder to extract info.

0 commit comments

Comments
 (0)