Skip to content

Refactor shortest_paths logic with match - #7166

Closed
MridulS wants to merge 1 commit into
networkx:mainfrom
MridulS:match_paths
Closed

MridulS wants to merge 1 commit into
networkx:mainfrom
MridulS:match_paths

Conversation

@MridulS

@MridulS MridulS commented Dec 15, 2023 •

Copy link
Copy Markdown
Member

In the spirit of #7036 just wanted to test out how we can use structural matching in the context of networkx. I think shortest_paths is definitely a good candidate.

I'm not sure if this is indeed the best way!

Once we have some basic agreement about this we can try to spread this across networkx :)

PS: I do like how the code reads!

@dschult

dschult commented Dec 16, 2023

Copy link
Copy Markdown
Member

Nice demo!
And the function just below is right there to allow easy comparison.

I think its not the reason Guido let match into the language -- it is just replacing if/else clauses. But that's a good place to start. Some things I noticed:

  • enticing structure at the top. Looks nicely organized. By the bottom, I've scrolled the matching phrase off the screen so I can't easily see what I'm matching for each case. :)
  • It's actually one indentation level more than the if/else structure (4 vs 3). Used to be source-is-none, target-is-none, method. Now it is match-source-target, case-source-target, match-method, case-method.

I guess we could always use a 3-tuple for the outer match -- rather than nested match statements. So we could reduce it to 2 indent levels...

Fun -- Fun! :)

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think there's an opportunity to get rid of a layer of nesting here by including the method in the original match statement. AFAICT this would work for 3 of 4 cases -all those except the ones where only the target is specified, because there is special handling for the DiGraph case.

In practical terms I don't think doing so really buys anything other than a slight readability boost from one less level of indentation. Given that there's also warnings being raised for the all-pairs cases, my vote would be to hold off on making such a change until after that warning expires. All told, LGTM - thanks @MridulS !

@jarrodmillman jarrodmillman changed the title MAINT: refactor shortest_paths logic with match Refactor shortest_paths logic with match Oct 10, 2024
@MridulS

MridulS commented Feb 6, 2025

Copy link
Copy Markdown
Member Author

Let's start this fresh.

@MridulS MridulS closed this Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants