Skip to content

Commit

Permalink
use --name instead of 'Node^name=..'
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Dec 16, 2022
1 parent f1cbe93 commit 838b0e6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 37 deletions.
2 changes: 1 addition & 1 deletion tests/integration_tests/test_single_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_run_dry():
'params.yaml:MyCustomNode',
'--outs',
'nodes/MyCustomNode/outs.json',
'zntrack run test_single_node.ExampleNode01^name=MyCustomNode']
'zntrack run test_single_node.ExampleNode01 --name=MyCustomNode']


def test_run_no_exec(proj_path):
Expand Down
20 changes: 5 additions & 15 deletions tests/unit_tests/core/test_core_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,7 @@ def test_matmul_not_supported():

def test_write_graph():
example = ExampleDVCOutsNode()

with patch.object(ExampleDVCOutsNode, "save") as save_mock, patch(
"zntrack.core.base._handle_nodes_as_methods",
) as handle_znnodes_mock:
# Patch the methods that write to disk
script = example.write_graph(dry_run=True)

assert save_mock.called
assert handle_znnodes_mock.called
script = example.write_graph(dry_run=True)

expected_script = [
"stage",
Expand All @@ -337,13 +329,11 @@ def test_write_graph():
"--force",
"--outs",
"example.dat",
(
f'{utils.config.interpreter} -c "from test_core_base import'
" ExampleDVCOutsNode;"
" ExampleDVCOutsNode.load(name='ExampleDVCOutsNode').run_and_save()\" "
),
'zntrack run test_core_base.ExampleDVCOutsNode --name=ExampleDVCOutsNode'
]

assert script == expected_script


def test__handle_nodes_as_methods():
example = ExampleDVCOutsNode()
Expand All @@ -352,7 +342,7 @@ def test__handle_nodes_as_methods():
_handle_nodes_as_methods({"example": example})

write_graph_mock.assert_called_with(
run=True, call_args=f".load(name='{example.node_name}').save(hash_only=True)"
run=True, call_args=f"--name={example.node_name} --hash-only"
)

with patch.object(ExampleDVCOutsNode, "write_graph") as write_graph_mock:
Expand Down
8 changes: 4 additions & 4 deletions tests/unit_tests/core/test_dvcgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_prepare_dvc_script():
nb_name=None,
module="src.file",
func_or_cls="MyNode",
call_args="^name=MyNode",
call_args="--name=MyNode",
)

assert script == [
Expand All @@ -183,7 +183,7 @@ def test_prepare_dvc_script():
"--force",
"--deps",
"file.txt",
'zntrack run src.file.MyNode^name=MyNode',
'zntrack run src.file.MyNode --name=MyNode',
]

script = prepare_dvc_script(
Expand All @@ -193,7 +193,7 @@ def test_prepare_dvc_script():
nb_name="notebook.ipynb",
module="src.file",
func_or_cls="MyNode",
call_args="^name=MyNode",
call_args="--name=MyNode",
)

assert script == [
Expand All @@ -208,7 +208,7 @@ def test_prepare_dvc_script():
"file.txt",
"--deps",
"src/file.py",
'zntrack run src.file.MyNode^name=MyNode',
'zntrack run src.file.MyNode --name=MyNode',
]


Expand Down
22 changes: 9 additions & 13 deletions zntrack/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,33 @@ def version_callback(value: bool) -> None:

@app.callback()
def main(
version: bool = typer.Option(
None, "--version", callback=version_callback, is_eager=True
),
version: bool = typer.Option(
None, "--version", callback=version_callback, is_eager=True
),
) -> None:
"""ZnTrack CLI callback."""
_ = version # this would be greyed out otherwise


@app.command()
def run(cmd: str) -> None:
def run(node: str, name: str = None, hash_only: bool = False) -> None:
"""Execute a ZnTrack Node.
Use as `zntrack run module.Node^name=nodename` or in `zntrack run module.nodified`.
Use as `zntrack run module.Node --name node_name`.
"""
sys.path.append(pathlib.Path.cwd().as_posix())

cmd, *kwargs = cmd.split("^")
hash_only = "hash_only" in kwargs
if hash_only:
kwargs.remove("hash_only")
kwargs = [tuple(arg.split("=")) for arg in kwargs]
package_and_module, cls = cmd.split(".", 1)
package_and_module, cls = node.split(".", 1)
module = importlib.import_module(package_and_module)

cls = getattr(module, cls)

try:
node = cls.load(**dict(kwargs))
node = cls.load(name=name)
if hash_only:
node.save(hash_only=True)
else:
node.run_and_save()
except AttributeError:
cls(exec_func=True, **dict(kwargs))
# @nodify function
cls(exec_func=True)
4 changes: 2 additions & 2 deletions zntrack/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _handle_nodes_as_methods(nodes: dict):
if node is not None:
node.write_graph(
run=True,
call_args=f"^name={node.node_name}^hash_only"
call_args=f"--name={node.node_name} --hash-only"
)


Expand Down Expand Up @@ -651,7 +651,7 @@ def write_graph( # noqa: C901
custom_args += pair

if call_args is None:
call_args = f"^name={self.node_name}"
call_args = f"--name={self.node_name}"

script = prepare_dvc_script(
node_name=self.node_name,
Expand Down
6 changes: 4 additions & 2 deletions zntrack/core/dvcgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def prepare_dvc_script(
nb_name,
module,
func_or_cls,
call_args="",
call_args=None,
) -> list:
"""Prepare the dvc cmd to be called by subprocess.
Expand Down Expand Up @@ -172,7 +172,9 @@ def prepare_dvc_script(
if nb_name is not None:
script += ["--deps", utils.module_to_path(module).as_posix()]

import_str = f"""zntrack run {module}.{func_or_cls}{call_args}"""
import_str = f"zntrack run {module}.{func_or_cls}"
if call_args is not None:
import_str += f" {call_args}"

script += [import_str]
log.debug(f"dvc script: {' '.join([str(x) for x in script])}")
Expand Down

3 comments on commit 838b0e6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Report for Python 3.10

name min max mean stddev median iqr outliers ops rounds iterations
0 tests/benchmark/test_benchmark.py::test_InputOutput_write_graph 8.7299e-05 0.000353795 9.12418e-05 6.00552e-06 9.0199e-05 1.999e-06 85;342 10959.9 2627 1
1 tests/benchmark/test_benchmark.py::test_InputOutput_load_lazy 8.93e-05 0.000137599 9.17005e-05 3.00603e-06 9.1099e-05 1.12575e-06 69;113 10905.1 1469 1
2 tests/benchmark/test_benchmark.py::test_NodeCollector_load_lazy 9.08e-05 0.000383497 9.39303e-05 9.72569e-06 9.2999e-05 1.099e-06 19;84 10646.2 1263 1
3 tests/benchmark/test_benchmark.py::test_NodeCollector_load 9.1099e-05 0.021113 0.000113783 0.000598336 9.2999e-05 1.2e-06 2;94 8788.64 1284 1
4 tests/benchmark/test_benchmark.py::test_InputOutput_load 9.16e-05 0.000173599 9.40953e-05 4.10898e-06 9.34e-05 1e-06 62;103 10627.5 1564 1
5 tests/benchmark/test_benchmark.py::test_NodeCollector_run_and_save 0.000318198 0.168298 0.00322511 0.0210082 0.000337397 2.96005e-05 1;9 310.067 64 1
6 tests/benchmark/test_benchmark.py::test_InputOutput_run_and_save 0.00139479 0.00266168 0.00151056 0.000102125 0.00149204 6.83e-05 22;15 662.005 326 1
7 tests/benchmark/test_benchmark.py::test_NodeCollector_write_graph 0.140202 0.38143 0.215353 0.0859995 0.195005 0.0463584 1;1 4.64353 6 1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Report for Python 3.8

name min max mean stddev median iqr outliers ops rounds iterations
0 tests/benchmark/test_benchmark.py::test_InputOutput_write_graph 8.6701e-05 0.00155071 9.05102e-05 2.89665e-05 8.9101e-05 1.5e-06 6;262 11048.5 2574 1
1 tests/benchmark/test_benchmark.py::test_InputOutput_load_lazy 9.3201e-05 0.0114534 0.000103151 0.000290798 9.5e-05 1.101e-06 1;107 9694.5 1526 1
2 tests/benchmark/test_benchmark.py::test_InputOutput_load 9.58e-05 0.0131449 0.000144051 0.000517522 9.86505e-05 2.1005e-06 19;182 6941.97 1492 1
3 tests/benchmark/test_benchmark.py::test_NodeCollector_load 9.7601e-05 0.00228781 0.000102481 5.73339e-05 0.0001004 1.7e-06 4;67 9757.88 1470 1
4 tests/benchmark/test_benchmark.py::test_NodeCollector_load_lazy 9.8801e-05 0.000148201 0.000101837 2.70665e-06 0.0001014 1.5e-06 71;67 9819.57 1440 1
5 tests/benchmark/test_benchmark.py::test_NodeCollector_run_and_save 0.000353702 0.000506003 0.000373136 3.11976e-05 0.000362002 1.46503e-05 5;6 2679.99 53 1
6 tests/benchmark/test_benchmark.py::test_InputOutput_run_and_save 0.00150991 0.00221091 0.00161677 8.59929e-05 0.00159711 6.68007e-05 47;21 618.518 329 1
7 tests/benchmark/test_benchmark.py::test_NodeCollector_write_graph 0.158216 0.339062 0.222266 0.0573577 0.212645 0.0665848 2;0 4.49911 8 1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Report for Python 3.9

name min max mean stddev median iqr outliers ops rounds iterations
0 tests/benchmark/test_benchmark.py::test_InputOutput_write_graph 8.9301e-05 0.000248603 9.20015e-05 4.24689e-06 9.1101e-05 1.5505e-06 95;176 10869.4 2268 1
1 tests/benchmark/test_benchmark.py::test_InputOutput_load 0.000104801 0.000434406 0.00010673 9.35894e-06 0.000106101 7.01e-07 14;97 9369.47 1406 1
2 tests/benchmark/test_benchmark.py::test_InputOutput_load_lazy 0.000105301 0.000410005 0.000107596 1.28874e-05 0.000106601 6e-07 9;101 9294.01 1391 1
3 tests/benchmark/test_benchmark.py::test_NodeCollector_load_lazy 0.000105801 0.000460006 0.000107929 1.38828e-05 0.000106901 6e-07 7;91 9265.39 1228 1
4 tests/benchmark/test_benchmark.py::test_NodeCollector_load 0.000105901 0.00919102 0.000118516 0.000276704 0.000107102 6.495e-07 4;99 8437.64 1272 1
5 tests/benchmark/test_benchmark.py::test_NodeCollector_run_and_save 0.000374605 0.0074558 0.00092109 0.00137398 0.000430305 0.000230153 7;10 1085.67 81 1
6 tests/benchmark/test_benchmark.py::test_InputOutput_run_and_save 0.00165432 0.00274104 0.00178835 0.000117572 0.00175332 8.74018e-05 43;29 559.176 323 1
7 tests/benchmark/test_benchmark.py::test_NodeCollector_write_graph 0.128027 0.348615 0.230512 0.078933 0.224015 0.0759932 2;0 4.33817 5 1

Please sign in to comment.