Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added __getitem__() and filter_nodes()to HamiltonGraph #919

Merged
merged 3 commits into from
May 27, 2024

Conversation

zilto
Copy link
Collaborator

@zilto zilto commented May 27, 2024

Context

The graph_types.HamiltonGraph and graph_types.HamiltonNode are the public facing interfaces for the graph.FunctionGraph and node.Node. HamiltonNode should be accessed through a HamiltonGraph and not be constructed directly.

Motivation

The most common operations on the HamiltonGraph is selecting a single node or filtering them by a criteria. However, they're are no API for that, meaning some list comprehension or filter() are often implemented in the library code but also in the user's code. This motivated the implementation __getitem()__ and filter_nodes()

Changes

HamiltonGraph.__getitem()__ allows to get a node using it's name as key (we know the node name is unique). It does a simple dictionary lookup.

HamiltonGraph(...)["my_node"] == HamiltonNode(name="my_node", ...)

HamiltonGraph.filter_nodes() receives a filter argument which is a Callable that takes in a HamiltonNode and returns a boolean. That provides a lot of flexibility to be able to filter by name, type, tags, or any other attributes.

Other changes

Took the opportunity to implement a one line change for #865

How I tested this

  • added tests for new functionalities
  • all old tests are passing

Notes

Checklist

  • PR has an informative and human-readable title (this will be pulled into the release notes)
  • Changes are limited to a single goal (no scope creep)
  • Code passed the pre-commit check & code is left cleaner/nicer than when first encountered.
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future TODOs are captured in comments
  • Project documentation has been updated if adding/changing functionality.

@zilto zilto requested a review from elijahbenizzy May 27, 2024 14:06
@zilto zilto added the enhancement New feature or request label May 27, 2024
@zilto zilto merged commit f70af85 into main May 27, 2024
23 checks passed
@zilto zilto deleted the feat/hamilton-graph-lookup branch May 27, 2024 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants