Skip to content

Conversation

@networmix
Copy link
Owner

Summary

  • add ngraph.cli module with a basic command-line interface
  • expose CLI via __main__ and package __init__
  • support serializing Results to JSON
  • register console script in pyproject.toml
  • test CLI behaviour

Testing

  • ruff check ngraph/cli.py ngraph/__init__.py ngraph/__main__.py ngraph/results.py tests/test_cli.py
  • black --check ngraph/cli.py ngraph/__init__.py ngraph/__main__.py ngraph/results.py tests/test_cli.py
  • isort --check-only ngraph/cli.py ngraph/__init__.py ngraph/__main__.py ngraph/results.py tests/test_cli.py
  • pytest -q

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a command-line interface for NetGraph, allowing users to run scenario files and serialize results to JSON.

  • Add ngraph.cli module with a run subcommand for executing scenarios and outputting JSON.
  • Expose CLI entry points in __main__.py, __init__.py, and register the ngraph console script in pyproject.toml.
  • Extend Results with a to_dict method and add tests covering file- and stdout-based CLI output.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_cli.py Add tests for run subcommand writing to file and stdout
pyproject.toml Register ngraph console script under [project.scripts]
ngraph/results.py Implement to_dict for serializing all stored results
ngraph/cli.py Define main and _run_scenario for CLI behavior and JSON output
ngraph/main.py Enable python -m ngraph entry point
ngraph/init.py Expose cli in package exports


def to_dict(self) -> Dict[str, Dict[str, Any]]:
"""Return a dictionary representation of all stored results."""
return {step: data.copy() for step, data in self._store.items()}
Copy link

Copilot AI May 18, 2025

Choose a reason for hiding this comment

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

The to_dict method performs a shallow copy of each inner result dict. If nested mutable values are present, consider using copy.deepcopy or documenting that nested structures remain shared.

Suggested change
return {step: data.copy() for step, data in self._store.items()}
return {step: copy.deepcopy(data) for step, data in self._store.items()}

Copilot uses AI. Check for mistakes.


def test_cli_run_file(tmp_path: Path) -> None:
scenario = Path("tests/scenarios/scenario_1.yaml")
Copy link

Copilot AI May 18, 2025

Choose a reason for hiding this comment

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

Hardcoding the scenario path assumes the current working directory; consider deriving it with Path(__file__).parent / "scenarios" / "scenario_1.yaml" for more robust test location resolution.

Suggested change
scenario = Path("tests/scenarios/scenario_1.yaml")
scenario = Path(__file__).parent / "scenarios" / "scenario_1.yaml"

Copilot uses AI. Check for mistakes.
results_dict: Dict[str, Dict[str, Any]] = scenario.results.to_dict()
json_str = json.dumps(results_dict, indent=2, default=str)
if output:
output.write_text(json_str)
Copy link

Copilot AI May 18, 2025

Choose a reason for hiding this comment

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

[nitpick] Writing JSON without a trailing newline can break POSIX conventions; consider appending "\n" to json_str when calling write_text.

Suggested change
output.write_text(json_str)
output.write_text(json_str + "\n")

Copilot uses AI. Check for mistakes.
@networmix networmix merged commit 75d22fb into main May 18, 2025
2 checks passed
@networmix networmix deleted the codex/create-cli-tool-with-run-command branch May 18, 2025 17:38
networmix added a commit that referenced this pull request Jun 2, 2025
networmix added a commit that referenced this pull request Jun 2, 2025
* adding transforms

* Fix floating point rounding in Demand.place (#65)

* Add CLI tool with run command (#67)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants