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

using decorators to avoid code replication #115

Open
andrea-bistacchi opened this issue Nov 4, 2024 · 8 comments
Open

using decorators to avoid code replication #115

andrea-bistacchi opened this issue Nov 4, 2024 · 8 comments
Assignees

Comments

@andrea-bistacchi
Copy link
Collaborator

andrea-bistacchi commented Nov 4, 2024

Avoiding code replication is generally achieved by using classes. However, this solution is not optimal when dealing with functions, simply defined as:

def(self, some_arguments)
    ...

In this case we might consider using decorators following this pattern:

def a_decorator(func):

    def wrapper(self, *args, **kwargs):
        # do something here, the same for all wrapped functions
        ...
        # the wrapped function goes here
        return func(self, *args, **kwargs)
        # do something here, the same for all wrapped functions
        ...

    return wrapper


@a_decorator
def a_wrapped_function(self, some_argument):
    # do something here, specific to the wrapped function
    ...
    return some_output

I will test this to unify freezing and unfreezing the GUI in functions of two_d_lines.py.

@andrea-bistacchi
Copy link
Collaborator Author

In 92dc29b I introduced the @freeze_gui decorator.

The decorator itself can be found at:

def freeze_gui(func):

The first usage of this decorator can be found at:

@freeze_gui

@r-monti14 please go ahead using the decorator in all functions in two_d_lines.py and three_d_surfaces.py, following the same pattern as above.

In addition to avoiding code repetition, this will solve #114 and similar issues.

@waqashussain117
Copy link
Collaborator

After adding the decorater and removing the functions that are already described in the decorater, the error is still there. The window freezes.

@r-monti14
Copy link
Collaborator

The decorator works with those tools that do not need a final input to interrupt the function.
Tools such as "Edit line", "Draw line" and "Split line-point", after being enhanced with the decorator, do not block the window.

@andrea-bistacchi
Copy link
Collaborator Author

With 7a98c74 I have updated both resample_line_distance and resample_line_number_points functions.

@r-monti14 please use these as a reference for all other functions in two_d_lines.py and three_d_surfaces.py.

We will think about the interactive tools Edit line, Draw line and Split line-point later on.

@r-monti14
Copy link
Collaborator

With f6d562f I have added the decorators at all the functions in two_d_lines.py (except Draw line, Edit line, Split line-point, Extend line and Measure).

@andrea-bistacchi
Copy link
Collaborator Author

Great! Have you tested all of them?

@r-monti14
Copy link
Collaborator

r-monti14 commented Nov 11, 2024

I did a quick test with all of them. I am not sure if the Snap line works correctly, but I will test everything again in detail.
Should I also add the decorators in three_d_surfaces.py even if the functions have no code for freezing?

@andrea-bistacchi
Copy link
Collaborator Author

Yes thanks. Actually I think it would be a good idea to use freezing everywhere, also in functions used to open or import data.

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

No branches or pull requests

5 participants