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

Edge class, less looping #3

Open
lmmx opened this issue Apr 23, 2021 · 0 comments
Open

Edge class, less looping #3

lmmx opened this issue Apr 23, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request invalid This doesn't seem right

Comments

@lmmx
Copy link
Owner

lmmx commented Apr 23, 2021

generate_candidate_edge in spans.py should be a class CandidateEdge, and rather than returning None as the silent negative case if a negative if statement (awful double negative!) it should set a property is_valid based on the three test conditions

  • in fact, is_valid can be a method which hides the 3 conditions out of the control flow of the init method

...and then the init method should end with a simple if self.is_valid(): before setting value (if not valid then the value was initialised as None)

  • set value as None at the class level to make it default
  • check generate_candidate_edge is only used in spans.assemble_spans
  • rather than if edge is not None (which itself could just be if edge), use a listcomp
  • in fact the whole block could be passed to the method at once rather than repeatedly looping in and passing the same parameters, so the listcomp will probably have a couple of layers

The syntax for this is [x for x in top_level_list for sublist in top_level_list] so here it’d be:

candidate_edges = [CandidateEdge(cinfo_i, cinfo_list[j]) for i, cinfo_i in enumerate(cinfo_list) for j in range(i) if CandidateEdge(cinfo_i, cinfo_list[j]).is_valid]

However this instantiates the Edge class twice! This could in fact be a good use for the walrus operator, though it’d technically become a generator expression within a listcomp (the listcomp is just the is_valid filter part)

candidate_edges = [
  edge for edge in [
    CandidateEdge(cinfo_i, cinfo_list[j])
    for i, cinfo_i in enumerate(cinfo_list)
    for j in range(i)
  ]
  if edge.is_valid()
]
  • Also think about renaming cinfo_list to contours? Double check what’s actually in the variable (it’s been named to emphasise it’s a list but type annotations could do that)
@lmmx lmmx added enhancement New feature or request invalid This doesn't seem right labels Apr 23, 2021
@lmmx lmmx self-assigned this Sep 8, 2024
@lmmx lmmx moved this to 🔮 Future in Page Dewarp Release Planner Sep 8, 2024
@lmmx lmmx moved this from 🔮 Future to 💡 Idea in Page Dewarp Release Planner Sep 8, 2024
@lmmx lmmx moved this to 🐣 Hatching in Planner Sep 10, 2024
@lmmx lmmx moved this from 🐣 Hatching to 🔙 Backlog in Planner Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request invalid This doesn't seem right
Projects
Status: 💡 Idea
Status: 📚 Shelved
Development

No branches or pull requests

1 participant