8000 pep8ify backends. · matplotlib/matplotlib@ff006ad · GitHub
[go: up one dir, main page]

Skip to content

Commit ff006ad

Browse files
committed
pep8ify backends.
Some (clearly intended as internal) globals were removed without deprecation as they cannot be properly deprecated; see changelog. PIXELS_PER_INCH is not used anywhere.
1 parent ebca3f2 commit ff006ad

File tree

8 files changed

+110
-102
lines changed

8 files changed

+110
-102
lines changed

.flake8

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ per-file-ignores =
3535

3636
lib/matplotlib/_cm.py: E202, E203, E302
3737
lib/matplotlib/_mathtext_data.py: E203, E261
38-
lib/matplotlib/backends/_backend_tk.py: E501
39-
lib/matplotlib/backends/backend_agg.py: E302
40-
lib/matplotlib/backends/backend_cairo.py: E203, E221, E402
41-
lib/matplotlib/backends/backend_gtk3.py: E203, E221, E225, E251, E501
42-
lib/matplotlib/backends/qt_editor/_formlayout.py: E501
4338
lib/matplotlib/font_manager.py: E203, E221, E251, E501
4439
lib/matplotlib/fontconfig_pattern.py: E201, E203, E221, E222, E225
4540
lib/matplotlib/mathtext.py: E201, E202, E203, E211, E221, E222, E225, E251, E301, E402
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Removals
2+
````````
3+
4+
The following API elements have been removed:
5+
6+
- ``backend_gtk3.PIXELS_PER_INCH``,
7+
- ``backend_pgf.re_escapetext``, ``backend_pgf.re_mathdefault``.
8+
9+
Deprecations
10+
````````````
11+
12+
The following API elements are deprecated:
13+
14+
- ``backend_pgf.repl_escapetext``, ``backend_pgf.repl_mathdefault``.

lib/matplotlib/backends/_backend_tk.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import numpy as np
1212

1313
import matplotlib
14-
from matplotlib import backend_tools, rcParams, cbook
14+
from matplotlib import backend_tools, cbook, rcParams
1515
from matplotlib.backend_bases import (
1616
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
1717
StatusbarBase, TimerBase, ToolContainerBase, cursors)
@@ -42,10 +42,6 @@ def _restore_foreground_window_at_end():
4242

4343
backend_version = tk.TkVersion
4444

45-
# the true dots per inch on the screen; should be display dependent
46-
# see http://groups.google.com/groups?q=screen+dpi+x11&hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=7077.26e81ad5%40swift.cs.tcd.ie&rnum=5 for some info about screen dpi
47-
PIXELS_PER_INCH = 75
48-
4945
cursord = {
5046
cursors.MOVE: "fleur",
5147
cursors.HAND: "hand2",
@@ -208,11 +204,13 @@ def __init__(self, figure, master=None, resize_callback=None):
208204
self._tkcanvas.bind("<Enter>", self.enter_notify_event)
209205
self._tkcanvas.bind("<Leave>", self.leave_notify_event)
210206
self._tkcanvas.bind("<KeyRelease>", self.key_release)
211-
for name in "<Button-1>", "<Button-2>", "<Button-3>":
207+
for name in ["<Button-1>", "<Button-2>", "<Button-3>"]:
212208
self._tkcanvas.bind(name, self.button_press_event)
213-
for name in "<Double-Button-1>", "<Double-Button-2>", "<Double-Button-3>":
209+
for name in [
210+
"<Double-Button-1>", "<Double-Button-2>", "<Double-Button-3>"]:
214211
self._tkcanvas.bind(name, self.button_dblclick_event)
215-
for name in "<ButtonRelease-1>", "<ButtonRelease-2>", "<ButtonRelease-3>":
212+
for name in [
213+
"<ButtonRelease-1>", "<ButtonRelease-2>", "<ButtonRelease-3>"]:
216214
self._tkcanvas.bind(name, self.button_release_event)
217215

218216
# Mouse wheel on Linux generates button 4/5 events

lib/matplotlib/backends/backend_agg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
backend_version = 'v2.2'
5353

54+
5455
def get_hinting_flag():
5556
mapping = {
5657
True: LOAD_FORCE_AUTOHINT,

lib/matplotlib/backends/backend_cairo.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
"cairo backend requires that pycairo>=1.11.0 or cairocffi"
2424
"is installed")
2525

26-
backend_version = cairo.version
27-
2826
from .. import cbook
2927
from matplotlib.backend_bases import (
3028
_Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase,
@@ -35,6 +33,9 @@
3533
from matplotlib.transforms import Affine2D
3634

3735

36+
backend_version = cairo.version
37+
38+
3839
if cairo.__name__ == "cairocffi":
3940
# Convert a pycairo context to a cairocffi one.
4041
def _to_context(ctx):
@@ -71,31 +72,31 @@ def _append_path(ctx, path, transform, clip=None):
7172

7273
class RendererCairo(RendererBase):
7374
fontweights = {
74-
100 : cairo.FONT_WEIGHT_NORMAL,
75-
200 : cairo.FONT_WEIGHT_NORMAL,
76-
300 : cairo.FONT_WEIGHT_NORMAL,
77-
400 : cairo.FONT_WEIGHT_NORMAL,
78-
500 : cairo.FONT_WEIGHT_NORMAL,
79-
600 : cairo.FONT_WEIGHT_BOLD,
80-
700 : cairo.FONT_WEIGHT_BOLD,
81-
800 : cairo.FONT_WEIGHT_BOLD,
82-
900 : cairo.FONT_WEIGHT_BOLD,
83-
'ultralight' : cairo.FONT_WEIGHT_NORMAL,
84-
'light' : cairo.FONT_WEIGHT_NORMAL,
85-
'normal' : cairo.FONT_WEIGHT_NORMAL,
86-
'medium' : cairo.FONT_WEIGHT_NORMAL,
87-
'regular' : cairo.FONT_WEIGHT_NORMAL,
88-
'semibold' : cairo.FONT_WEIGHT_BOLD,
89-
'bold' : cairo.FONT_WEIGHT_BOLD,
90-
'heavy' : cairo.FONT_WEIGHT_BOLD,
91-
'ultrabold' : cairo.FONT_WEIGHT_BOLD,
92-
'black' : cairo.FONT_WEIGHT_BOLD,
93-
}
75+
100: cairo.FONT_WEIGHT_NORMAL,
76+
200: cairo.FONT_WEIGHT_NORMAL,
77+
300: cairo.FONT_WEIGHT_NORMAL,
78+
400: cairo.FONT_WEIGHT_NORMAL,
79+
500: cairo.FONT_WEIGHT_NORMAL,
80+
600: cairo.FONT_WEIGHT_BOLD,
81+
700: cairo.FONT_WEIGHT_BOLD,
82+
800: cairo.FONT_WEIGHT_BOLD,
83+
900: cairo.FONT_WEIGHT_BOLD,
84+
'ultralight': cairo.FONT_WEIGHT_NORMAL,
85+
'light': cairo.FONT_WEIGHT_NORMAL,
86+
'normal': cairo.FONT_WEIGHT_NORMAL,
87+
'medium': cairo.FONT_WEIGHT_NORMAL,
88+
'regular': cairo.FONT_WEIGHT_NORMAL,
89+
'semibold': cairo.FONT_WEIGHT_BOLD,
90+
'bold': cairo.FONT_WEIGHT_BOLD,
91+
'heavy': cairo.FONT_WEIGHT_BOLD,
92+
'ultrabold': cairo.FONT_WEIGHT_BOLD,
93+
'black': cairo.FONT_WEIGHT_BOLD,
94+
}
9495
fontangles = {
95-
'italic' : cairo.FONT_SLANT_ITALIC,
96-
'normal' : cairo.FONT_SLANT_NORMAL,
97-
'oblique' : cairo.FONT_SLANT_OBLIQUE,
98-
}
96+
'italic': cairo.FONT_SLANT_ITALIC,
97+
'normal': cairo.FONT_SLANT_NORMAL,
98+
'oblique': cairo.FONT_SLANT_OBLIQUE,
99+
}
99100

100101
def __init__(self, dpi):
101102
self.dpi = dpi
@@ -113,7 +114,7 @@ def set_ctx_from_surface(self, surface):
113114
# for PDF/PS/SVG surfaces, which have no way to report their extents.
114115

115116
def set_width_height(self, width, height):
116-
self.width = width
117+
self.width = width
117118
self.height = height
118119

119120
def _fill_and_stroke(self, ctx, fill_c, alpha, alpha_overrides):
@@ -305,16 +306,16 @@ def points_to_pixels(self, points):
305306

306307
class GraphicsContextCairo(GraphicsContextBase):
307308
_joind = {
308-
'bevel' : cairo.LINE_JOIN_BEVEL,
309-
'miter' : cairo.LINE_JOIN_MITER,
310-
'round' : cairo.LINE_JOIN_ROUND,
311-
}
309+
'bevel': cairo.LINE_JOIN_BEVEL,
310+
'miter': cairo.LINE_JOIN_MITER,
311+
'round': cairo.LINE_JOIN_ROUND,
312+
}
312313

313314
_capd = {
314-
'butt' : cairo.LINE_CAP_BUTT,
315-
'projecting' : cairo.LINE_CAP_SQUARE,
316-
'round' : cairo.LINE_CAP_ROUND,
317-
}
315+
'butt': cairo.LINE_CAP_BUTT,
316+
'projecting': cairo.LINE_CAP_SQUARE,
317+
'round': cairo.LINE_CAP_ROUND,
318+
}
318319

319320
def __init__(self, renderer):
320321
GraphicsContextBase.__init__(self)

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,13 @@
3636
backend_version = "%s.%s.%s" % (
3737
Gtk.get_major_version(), Gtk.get_micro_version(), Gtk.get_minor_version())
3838

39-
# the true dots per inch on the screen; should be display dependent
40-
# see http://groups.google.com/groups?q=screen+dpi+x11&hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=7077.26e81ad5%40swift.cs.tcd.ie&rnum=5 for some info about screen dpi
41-
PIXELS_PER_INCH = 96
42-
4339
try:
4440
cursord = {
45-
cursors.MOVE : Gdk.Cursor.new(Gdk.CursorType.FLEUR),
46-
cursors.HAND : Gdk.Cursor.new(Gdk.CursorType.HAND2),
47-
cursors.POINTER : Gdk.Cursor.new(Gdk.CursorType.LEFT_PTR),
48-
cursors.SELECT_REGION : Gdk.Cursor.new(Gdk.CursorType.TCROSS),
49-
cursors.WAIT : Gdk.Cursor.new(Gdk.CursorType.WATCH),
41+
cursors.MOVE: Gdk.Cursor.new(Gdk.CursorType.FLEUR),
42+
cursors.HAND: Gdk.Cursor.new(Gdk.CursorType.HAND2),
43+
cursors.POINTER: Gdk.Cursor.new(Gdk.CursorType.LEFT_PTR),
44+
cursors.SELECT_REGION: Gdk.Cursor.new(Gdk.CursorType.TCROSS),
45+
cursors.WAIT: Gdk.Cursor.new(Gdk.CursorType.WATCH),
5046
}
5147
except TypeError as exc:
5248
# Happens when running headless. Convert to ImportError to cooperate with
@@ -154,23 +150,23 @@ class FigureCanvasGTK3(Gtk.DrawingArea, FigureCanvasBase):
154150

155151
# Setting this as a static constant prevents
156152
# this resulting expression from leaking
157-
event_mask = (Gdk.EventMask.BUTTON_PRESS_MASK |
158-
Gdk.EventMask.BUTTON_RELEASE_MASK |
159-
Gdk.EventMask.EXPOSURE_MASK |
160-
Gdk.EventMask.KEY_PRESS_MASK |
161-
Gdk.EventMask.KEY_RELEASE_MASK |
162-
Gdk.EventMask.ENTER_NOTIFY_MASK |
163-
Gdk.EventMask.LEAVE_NOTIFY_MASK |
164-
Gdk.EventMask.POINTER_MOTION_MASK |
165-
Gdk.EventMask.POINTER_MOTION_HINT_MASK|
166-
Gdk.EventMask.SCROLL_MASK)
153+
event_mask = (Gdk.EventMask.BUTTON_PRESS_MASK
154+
| Gdk.EventMask.BUTTON_RELEASE_MASK
155+
| Gdk.EventMask.EXPOSURE_MASK
156+
| Gdk.EventMask.KEY_PRESS_MASK
157+
| Gdk.EventMask.KEY_RELEASE_MASK
158+
| Gdk.EventMask.ENTER_NOTIFY_MASK
159+
| Gdk.EventMask.LEAVE_NOTIFY_MASK
160+
| Gdk.EventMask.POINTER_MOTION_MASK
161+
| Gdk.EventMask.POINTER_MOTION_HINT_MASK
162+
| Gdk.EventMask.SCROLL_MASK)
167163

168164
def __init__(self, figure):
169165
FigureCanvasBase.__init__(self, figure)
170166
GObject.GObject.__init__(self)
171167

172-
self._idle_draw_id = 0
173-
self._lastCursor = None
168+
self._idle_draw_id = 0
169+
self._lastCursor = None
174170

175171
self.connect('scroll_event', self.scroll_event)
176172
self.connect('button_press_event', self.button_press_event)
@@ -200,25 +196,24 @@ def scroll_event(self, widget, event):
200196
x = event.x
201197
# flipy so y=0 is bottom of canvas
202198
y = self.get_allocation().height - event.y
203-
if event.direction==Gdk.ScrollDirection.UP:
204-
step = 1
205-
else:
206-
step = -1
199+
step = 1 if event.direction == Gdk.ScrollDirection.UP else -1
207200
FigureCanvasBase.scroll_event(self, x, y, step, guiEvent=event)
208201
return False # finish event propagation?
209202

210203
def button_press_event(self, widget, event):
211204
x = event.x
212205
# flipy so y=0 is bottom of canvas
213206
y = self.get_allocation().height - event.y
214-
FigureCanvasBase.button_press_event(self, x, y, event.button, guiEvent=event)
207+
FigureCanvasBase.button_press_event(
208+
self, x, y, event.button, guiEvent=event)
215209
return False # finish event propagation?
216210

217211
def button_release_event(self, widget, event):
218212
x = event.x
219213
# flipy so y=0 is bottom of canvas
220214
y = self.get_allocation().height - event.y
221-
FigureCanvasBase.button_release_event(self, x, y, event.button, guiEvent=event)
215< 10000 /td>+
FigureCanvasBase.button_release_event(
216+
self, x, y, event.button, guiEvent=event)
222217
return False # finish event propagation?
223218

224219
def key_press_event(self, widget, event):
@@ -472,7 +467,8 @@ def set_cursor(self, cursor):
472467
Gtk.main_iteration()
473468

474469
def draw_rubberband(self, event, x0, y0, x1, y1):
475-
'adapted from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/189744'
470+
# adapted from
471+
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/189744
476472
self.ctx = self.canvas.get_property("window").cairo_create()
477473

478474
# todo: instead of redrawing the entire figure, copy the part of
@@ -622,14 +618,14 @@ class FileChooserDialog(Gtk.FileChooserDialog):
622618
selected and presents the user with a menu of supported image formats
623619
"""
624620
def __init__(self,
625-
title = 'Save file',
626-
parent = None,
627-
action = Gtk.FileChooserAction.SAVE,
628-
buttons = (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
629-
Gtk.STOCK_SAVE, Gtk.ResponseType.OK),
630-
path = None,
631-
filetypes = [],
632-
default_filetype = None,
621+
title='Save file',
622+
parent=None,
623+
action=Gtk.FileChooserAction.SAVE,
624+
buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
625+
Gtk.STOCK_SAVE, Gtk.ResponseType.OK),
626+
path=None,
627+
filetypes=[],
628+
default_filetype=None,
633629
):
634630
super().__init__(title, parent, action, buttons)
635631
self.set_default_response(Gtk.ResponseType.OK)
@@ -961,15 +957,11 @@ def error_msg_gtk(msg, parent=None):
961957
parent = parent.get_toplevel()
962958
if not parent.is_toplevel():
963959
parent = None
964-
965960
if not isinstance(msg, str):
966961
msg = ','.join(map(str, msg))
967-
968962
dialog = Gtk.MessageDialog(
969-
parent = parent,
970-
type = Gtk.MessageType.ERROR,
971-
buttons = Gtk.ButtonsType.OK,
972-
message_format = msg)
963+
parent=parent, type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK,
964+
message_format=msg)
973965
dialog.run()
974966
dialog.destroy()
975967

lib/matplotlib/backends/backend_pgf.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,45 @@ def get_preamble():
6868

6969
NO_ESCAPE = r"(?<!\\)(?:\\\\)*"
7070
re_mathsep = re.compile(NO_ESCAPE + r"\$")
71-
re_escapetext = re.compile(NO_ESCAPE + "([_^$%])")
72-
re_mathdefault = re.compile(NO_ESCAPE + r"(\\mathdefault)")
7371

7472

73+
@cbook.deprecated("3.2")
7574
def repl_escapetext(m):
7675
return "\\" + m.group(1)
7776

7877

78+
@cbook.deprecated("3.2")
7979
def repl_mathdefault(m):
8080
return m.group(0)[:-len(m.group(1))]
8181

8282

83+
_replace_escapetext = functools.partial(
84+
# When the next character is _, ^, $, or % (not preceded by an escape),
85+
# insert a backslash.
86+
re.compile(NO_ESCAPE + "(?=[_^$%])").sub, "\\\\")
87+
_replace_mathdefault = functools.partial(
88+
# Replace \mathdefault (when not preceded by an escape) by empty string.
89+
re.compile(NO_ESCAPE + r"(\\mathdefault)").sub, "")
90+
91+
8392
def common_texification(text):
8493
"""
8594
Do some necessary and/or useful substitutions for texts to be included in
8695
LaTeX documents.
8796
"""
88-
8997
# Sometimes, matplotlib adds the unknown command \mathdefault.
9098
# Not using \mathnormal instead since this looks odd for the latex cm font.
91-
text = re_mathdefault.sub(repl_mathdefault, text)
92-
99+
text = _replace_mathdefault(text)
93100
# split text into normaltext and inline math parts
94101
parts = re_mathsep.split(text)
95102
for i, s in enumerate(parts):
96103
if not i % 2:
97104
# textmode replacements
98-
s = re_escapetext.sub(repl_escapetext, s)
105+
s = _replace_escapetext(s)
99106
else:
100107
# mathmode replacements
101108
s = r"\(\displaystyle %s\)" % s
102109
parts[i] = s
103-
104110
return "".join(parts)
105111

106112

0 commit comments

Comments
 (0)
0