Skip to content

Commit bce7f10

Browse files
committed
Add more resources.
1 parent 3849aea commit bce7f10

8 files changed

+172
-8
lines changed

collec.ipynb

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import inspect"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 11,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"def fibonacci(n):\n",
19+
" \"\"\"Return the nth fibonacci number.\"\"\"\n",
20+
" if n < 2:\n",
21+
" return n\n",
22+
" return fibonacci(n-1) + fibonacci(n-2)"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 12,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"source = inspect.getsource(fibonacci)"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 13,
37+
"metadata": {},
38+
"outputs": [
39+
{
40+
"data": {
41+
"text/plain": [
42+
"'Return the nth fibonacci number.'"
43+
]
44+
},
45+
"execution_count": 13,
46+
"metadata": {},
47+
"output_type": "execute_result"
48+
}
49+
],
50+
"source": [
51+
"inspect.getdoc(fibonacci)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 5,
57+
"metadata": {},
58+
"outputs": [
59+
{
60+
"name": "stdout",
61+
"output_type": "stream",
62+
"text": [
63+
"def fibonacci(n):\n",
64+
" if n < 2:\n",
65+
" return n\n",
66+
" return fibonacci(n-1) + fibonacci(n-2)\n",
67+
"\n"
68+
]
69+
}
70+
],
71+
"source": [
72+
"print(source)"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {},
79+
"outputs": [],
80+
"source": []
81+
}
82+
],
83+
"metadata": {
84+
"kernelspec": {
85+
"display_name": "pytask",
86+
"language": "python",
87+
"name": "python3"
88+
},
89+
"language_info": {
90+
"codemirror_mode": {
91+
"name": "ipython",
92+
"version": 3
93+
},
94+
"file_extension": ".py",
95+
"mimetype": "text/x-python",
96+
"name": "python",
97+
"nbconvert_exporter": "python",
98+
"pygments_lexer": "ipython3",
99+
"version": "3.11.7"
100+
}
101+
},
102+
"nbformat": 4,
103+
"nbformat_minor": 2
104+
}

collect_app.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@
1818
from textual.widgets import Header
1919
from textual.widgets import Static
2020
from textual.widgets import Tree
21+
from rich._inspect import Inspect
2122

2223
if TYPE_CHECKING:
2324
from textual.widgets.tree import TreeNode
2425
from _pytask.node_protocols import PTask
2526

2627

28+
class Task(NamedTuple):
29+
"""A task."""
30+
31+
task: PTask
32+
reasons_rerun: list[str]
33+
34+
2735
class Module(NamedTuple):
2836
"""A module with tasks."""
2937

@@ -39,7 +47,8 @@ def show_node(self, node: TreeNode) -> None:
3947
return
4048
if isinstance(pytask_node, RenderableType):
4149
self.update(pytask_node)
42-
# self.update(Inspect(pytask_node, all=False))
50+
return
51+
self.update(Inspect(pytask_node, all=False))
4352

4453

4554
class CollectionApp(App):

src/_pytask/collect_command.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
if TYPE_CHECKING:
4545
from textual.widgets.tree import TreeNode
4646
from typing import NoReturn
47+
import networkx as nx
48+
49+
50+
class Task(NamedTuple):
51+
"""A task."""
52+
53+
task: PTask
54+
reasons_rerun: list[str]
4755

4856

4957
class Module(NamedTuple):
@@ -185,7 +193,7 @@ def _select_tasks_by_expressions_and_marker(session: Session) -> list[PTask]:
185193
return [task for task in session.tasks if task.signature in remaining]
186194

187195

188-
def _organize_tasks(tasks: list[PTaskWithPath]) -> list[Module]:
196+
def _organize_tasks(tasks: list[PTaskWithPath], dag: nx.DiGraph) -> list[Module]:
189197
"""Organize tasks in a dictionary.
190198
191199
The dictionary has file names as keys and then a dictionary with task names and
@@ -197,11 +205,22 @@ def _organize_tasks(tasks: list[PTaskWithPath]) -> list[Module]:
197205
module = name_to_module.get(
198206
task.path.as_posix(), Module(task.path.as_posix(), [])
199207
)
208+
reasons = _find_reasons_to_rerun(task, dag)
209+
task_wrap = Task(task=task, reasons_rerun=[])
200210
module.tasks.append(task)
201211
name_to_module[module.name] = module
202212
return [name_to_module[name] for name in sorted(name_to_module)]
203213

204214

215+
def _find_reasons_to_rerun(task: PTask, dag: nx.DiGraph) -> list[str]:
216+
"""Find the reasons to rerun a task."""
217+
reasons = []
218+
for task in dag.nodes:
219+
if node.task == task:
220+
reasons.append(node.name)
221+
return reasons
222+
223+
205224
def _print_collected_tasks(
206225
dictionary: dict[Path, list[PTaskWithPath]],
207226
show_nodes: bool,

src/_pytask/nodes.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
from rich._inspect import Inspect
2525
from rich.console import Console, ConsoleOptions, RenderResult, group
2626
from rich.panel import Panel
27+
from rich.syntax import Syntax
28+
from rich.table import Table
29+
from _pytask.tree_util import tree_leaves
30+
from rich import box
2731

2832

2933
if TYPE_CHECKING:
@@ -149,12 +153,15 @@ def execute(self, **kwargs: Any) -> Any:
149153
return self.function(**kwargs)
150154

151155
def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
152-
@group()
153-
def _():
154-
yield self.name
155-
yield self.function.__doc__ or ""
156-
157-
yield Panel(_())
156+
table = Table("", "", title="Information", show_header=False, expand=True, box=box.ROUNDED)
157+
table.add_row("Name", self.name)
158+
table.add_row("Path", self.path.as_posix())
159+
table.add_row("# Dependecies", str(len(tree_leaves(self.depends_on))))
160+
table.add_row("# Products", str(len(tree_leaves(self.produces))))
161+
162+
yield table
163+
yield Panel(inspect.getdoc(self.function), title="Docstring")
164+
yield Panel(Syntax(inspect.getsource(self.function), lexer="python"), title="Source Code")
158165

159166

160167
@define(kw_only=True)

tasks/out.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World!

tasks/out_2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World!

tasks/task_example.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from pathlib import Path
2+
from typing import Annotated
3+
from pytask import Product
4+
5+
6+
7+
def task_example(path: Path = Path("out.txt")) -> Annotated[Path, Path("out_2.txt")]:
8+
"""Compute the square of 2.
9+
10+
Example
11+
-------
12+
>>> task_example(2)
13+
4
14+
15+
"""
16+
return path.read_text()

tasks/task_module.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pathlib import Path
2+
from typing import Annotated
3+
from pytask import Product
4+
5+
6+
def task_example(path: Annotated[Path, Product] = Path("out.txt")) -> None:
7+
path.write_text("Hello World!")

0 commit comments

Comments
 (0)