Skip to content

Backend class for better code reuse between backend modules #8773

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

Merged
merged 1 commit into from
Jul 24, 2017

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Jun 18, 2017

This is the second part of #8771, split out for simpler review (note that it also includes the first part, which is the object of #8772; thus, I would suggest specifically looking at the diff of the last commit).

Currently, backend modules need to define a number of classes (FigureCanvas, FigureManager) and functions (new_figure_manager, new_figure_manager_given_figure, show, draw_if_interactive) with specified names, which leads to some serious copy-pasting between the modules, as well as seemingly unused imports (see e.g. "not used" comment in backend_qt5agg.py). Instead, use a class-based approach: the _Backend class can be subclassed to define these functions (and provides some templates for them); then a special decorator re-exports the attributes and methods of the _Backend class to the backend-defining module.

@anntzer anntzer changed the title Backend class Backend class for better code reuse between backend modules Jun 18, 2017
@anntzer anntzer mentioned this pull request Jun 18, 2017
6 tasks
@anntzer anntzer force-pushed the backend-class branch 5 times, most recently from bf03f1e to 544823a Compare June 18, 2017 22:23
@tacaswell
Copy link
Member

How does this interact with #4143 ?

FigureManager = FigureManagerPgf


def draw_if_interactive():
Copy link
Member

Choose a reason for hiding this comment

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

Should this be a class method on the preceding class?

Copy link
Contributor Author

@anntzer anntzer Jun 19, 2017

Choose a reason for hiding this comment

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

Either way is technically fine (this def will just override the one exported by the decorator) but actually I can just leave trigger_manager_draw as None and this way we'll get a do-nothing draw_if_interactive.

@tacaswell
Copy link
Member

Given how disruptive this change could be, I am surprisingly 👍 on this 😄 .

This is a very elegant solution to a problem that has been bugging me for years.

@tacaswell
Copy link
Member

And to answer my own question, I think this is mostly orthogonal.

@anntzer
Copy link
Contributor Author

anntzer commented Jun 19, 2017

Re #4143 (MEP27): this doesn't really change anything, MEP27 can still go in (modulo conflicts, but they're already there anyways) (and I don't really have an opinion regarding MEP27). This is really just a way to 1. use some private "templates" to define helper functions and 2. do from other_backend import draw_if_interactive, show, ... without appearing to have a bunch of unneeded imports.

@anntzer anntzer force-pushed the backend-class branch 3 times, most recently from 811b2d0 to 05aae1e Compare June 19, 2017 05:48
@anntzer anntzer mentioned this pull request Jun 19, 2017
# each draw command. Instead, we mark the figure as invalid, so that it
# will be redrawn as soon as the event loop resumes via PyOS_InputHook.
# This function should be called after each draw event, even if
# matplotlib is not running interactively.
Copy link
Member

Choose a reason for hiding this comment

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

This comment, taken from the original, has me puzzled. It seems to be contradicted by the code, which does nothing unless is_interactive() returns True. This is not changed by your refactor, so if the comment is misleading now, then it was also misleading before; or I am just missing its point in both cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to leave it as it is for now...

@efiring
Copy link
Member

efiring commented Jun 19, 2017

I certainly welcome the reduced duplication (which always bothered me also) and reduced LOC. And the removal of some cruft, like the import of ctypes!

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jun 19, 2017
@anntzer anntzer force-pushed the backend-class branch 2 times, most recently from 795c171 to 1c3da14 Compare June 20, 2017 19:28
@anntzer anntzer force-pushed the backend-class branch 2 times, most recently from a422dca to 2254671 Compare July 1, 2017 22:50
@tacaswell
Copy link
Member

@anntzer can you rebase this now that #8772 is in?

@anntzer
Copy link
Contributor Author

anntzer commented Jul 9, 2017

done

Copy link
Contributor

@dopplershift dopplershift left a comment

Choose a reason for hiding this comment

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

Looks like a nice clean up. I don't see anything that bothers me.

@tacaswell
Copy link
Member

I am going to give this one more read through before pushing the merge button myself, but happy if someone else also reviews this and merges.

@anntzer
Copy link
Contributor Author

anntzer commented Jul 23, 2017

Rebased.

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

Successfully merging this pull request may close these issues.

4 participants