Skip to content

Simplify flow func augmentation logic in connectivity module - #7367

Merged
dschult merged 2 commits into
networkx:mainfrom
elshorbagyx:refactor-connectivity
Apr 9, 2024
Merged

dschult merged 2 commits into
networkx:mainfrom
elshorbagyx:refactor-connectivity

Conversation

@elshorbagyx

@elshorbagyx elshorbagyx commented Mar 23, 2024 •

Copy link
Copy Markdown
Contributor

Following the steps of #7036 and #7166, I have refactored the if/elif statements in networkx/algorithms/connectivity/connectivity.py with match/case statements.

There was a couple of unwanted matches since they can be replaced with or , I guess they were there because of reference equality with is, but matching with name makes it easier to group similar conditions together.

@dschult

dschult commented Mar 24, 2024

Copy link
Copy Markdown
Member

Yes -- good thought!
And a user may have written their own function. so we shouldn't just raise if we don't have one of the included flow functions. The history here is helpful (and not apparent from the code -- but maybe from the "blame" or comments like this one). Originally the cutoff parameter was only present for some of the flow functions. This code was written and some elif didn't have the cutoff parameter. Then there was an effort made to ensure all flow_functions supported a cutoff kwarg. That effort was successful. And this logic was updated but not simplified.

I guess an even simpler way to handle this is to include cutoff in the kwargs dict in the previous line where it is defined. Then it is just one if statement.

Users who write their own functions already have to have the other kwargs at least in their signature (they can ignore them). So I guess that would put cutoff into that category. They'd have to at least put it in their function signature (or have a ** parameter). They don't have to do anything with it though.

@elshorbagyx

Copy link
Copy Markdown
Contributor Author

I got it, I am going to make the edits in a commit.

@elshorbagyx

elshorbagyx commented Mar 26, 2024 •

Copy link
Copy Markdown
Contributor Author

There was a problem, that the cutoff kwarg is not defined for the preflow_push flow function. So adding it directly to the kwargs dict is not going to work, so I wrote an extra check to see if it is not preflow_push. I guess the code reads like English with fewer if/elif or match statements.

@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.

The simplification of all the conditional logic seems like an improvement to me. I wonder if it's possible to use functools.partial to solve this problem? Anyways, +1 for this cleanup as-is!

@rossbar rossbar changed the title Refactor connectivity submodule with match case Simplify flow func augmentation logic in connectivity module Apr 8, 2024
@dschult
dschult merged commit 4bc8ba9 into networkx:main Apr 9, 2024
@jarrodmillman jarrodmillman added this to the 3.4 milestone Apr 9, 2024
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
…rkx#7367)

* Refactor `connectivity` submodule with match case

* Refacotr kwargs for flow functions
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.

4 participants