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

PEP8 fixes on table.py #1376

Merged
merged 1 commit into from Oct 13, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
111 changes: 59 additions & 52 deletions lib/matplotlib/table.py
Expand Up @@ -31,7 +31,6 @@
from transforms import Bbox



class Cell(Rectangle):
"""
A cell is a Rectangle with some associated text.
Expand All @@ -49,18 +48,17 @@ def __init__(self, xy, width, height,

# Call base
Rectangle.__init__(self, xy, width=width, height=height,
edgecolor=edgecolor, facecolor=facecolor,
)
edgecolor=edgecolor, facecolor=facecolor)
self.set_clip_on(False)

# Create text object
if loc is None: loc = 'right'
if loc is None:
loc = 'right'
self._loc = loc
self._text = Text(x=xy[0], y=xy[1], text=text,
fontproperties=fontproperties)
self._text.set_clip_on(False)


def set_transform(self, trans):
Rectangle.set_transform(self, trans)
# the text does not get the transform!
Expand Down Expand Up @@ -93,7 +91,8 @@ def auto_set_font_size(self, renderer):

@allow_rasterization
def draw(self, renderer):
if not self.get_visible(): return
if not self.get_visible():
return
# draw the rectangle
Rectangle.draw(self, renderer)

Expand Down Expand Up @@ -134,14 +133,14 @@ def get_text_bounds(self, renderer):

def get_required_width(self, renderer):
""" Get width required for this cell. """
l,b,w,h = self.get_text_bounds(renderer)
l, b, w, h = self.get_text_bounds(renderer)
return w * (1.0 + (2.0 * self.PAD))


def set_text_props(self, **kwargs):
'update the text properties with kwargs'
self._text.update(kwargs)


class Table(Artist):
"""
Create a table of cells.
Expand All @@ -155,25 +154,24 @@ class Table(Artist):
Return value is a sequence of text, line and patch instances that make
up the table
"""
codes = {'best' : 0,
'upper right' : 1, # default
'upper left' : 2,
'lower left' : 3,
'lower right' : 4,
'center left' : 5,
'center right' : 6,
'lower center' : 7,
'upper center' : 8,
'center' : 9,

'top right' : 10,
'top left' : 11,
'bottom left' : 12,
'bottom right' : 13,
'right' : 14,
'left' : 15,
'top' : 16,
'bottom' : 17,
codes = {'best': 0,
'upper right': 1, # default
'upper left': 2,
'lower left': 3,
'lower right': 4,
'center left': 5,
'center right': 6,
'lower center': 7,
'upper center': 8,
'center': 9,
'top right': 10,
'top left': 11,
'bottom left': 12,
'bottom right': 13,
'right': 14,
'left': 15,
'top': 16,
'bottom': 17,
}

FONTSIZE = 10
Expand All @@ -184,9 +182,12 @@ def __init__(self, ax, loc=None, bbox=None):
Artist.__init__(self)

if is_string_like(loc) and loc not in self.codes:
warnings.warn('Unrecognized location %s. Falling back on bottom; valid locations are\n%s\t' %(loc, '\n\t'.join(self.codes.iterkeys())))
warnings.warn('Unrecognized location %s. Falling back on '
'bottom; valid locations are\n%s\t' %
(loc, '\n\t'.join(self.codes.iterkeys())))
loc = 'bottom'
if is_string_like(loc): loc = self.codes.get(loc, 1)
if is_string_like(loc):
loc = self.codes.get(loc, 1)
self.set_figure(ax.figure)
self._axes = ax
self._loc = loc
Expand All @@ -205,7 +206,7 @@ def __init__(self, ax, loc=None, bbox=None):

def add_cell(self, row, col, *args, **kwargs):
""" Add a cell to the table. """
xy = (0,0)
xy = (0, 0)

cell = Cell(xy, *args, **kwargs)
cell.set_figure(self.figure)
Expand All @@ -215,18 +216,21 @@ def add_cell(self, row, col, *args, **kwargs):
self._cells[(row, col)] = cell

def _approx_text_height(self):
return self.FONTSIZE/72.0*self.figure.dpi/self._axes.bbox.height * 1.2
return (self.FONTSIZE / 72.0 * self.figure.dpi /
self._axes.bbox.height * 1.2)

@allow_rasterization
def draw(self, renderer):
# Need a renderer to do hit tests on mouseevent; assume the last one will do
# Need a renderer to do hit tests on mouseevent; assume the last one
# will do
if renderer is None:
renderer = self._cachedRenderer
if renderer is None:
raise RuntimeError('No renderer defined')
self._cachedRenderer = renderer

if not self.get_visible(): return
if not self.get_visible():
return
renderer.open_group('table')
self._update_positions(renderer)

Expand All @@ -249,24 +253,25 @@ def _get_grid_bbox(self, renderer):
bbox = Bbox.union(boxes)
return bbox.inverse_transformed(self.get_transform())

def contains(self,mouseevent):
def contains(self, mouseevent):
"""Test whether the mouse event occurred in the table.

Returns T/F, {}
"""
if callable(self._contains):
return self._contains(self,mouseevent)
return self._contains(self, mouseevent)

# TODO: Return index of the cell containing the cursor so that the user
# doesn't have to bind to each one individually.
if self._cachedRenderer is not None:
boxes = [self._cells[pos].get_window_extent(self._cachedRenderer)
for pos in self._cells.iterkeys()
if pos[0] >= 0 and pos[1] >= 0]
# FIXME bbox_all is not defined.
bbox = bbox_all(boxes)
return bbox.contains(mouseevent.x,mouseevent.y),{}
return bbox.contains(mouseevent.x, mouseevent.y), {}
else:
return False,{}
return False, {}

def get_children(self):
'Return the Artists contained by the table'
Expand All @@ -276,6 +281,7 @@ def get_children(self):
def get_window_extent(self, renderer):
'Return the bounding box of the table in window coords'
boxes = [c.get_window_extent(renderer) for c in self._cells]
# FIXME bbox_all is not defined
return bbox_all(boxes)
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm, I wonder if this method is used...

Copy link
Member

Choose a reason for hiding this comment

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

According to doc/api/api_changes.rst, bbox_all() should be changed to Bbox.union()

Copy link
Member Author

Choose a reason for hiding this comment

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

I could fix that, but IMO it is better to open a ticket (easyfix tagged?), and have someone do a proper regression tests on it.

Copy link
Member

Choose a reason for hiding this comment

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

Done. Issue #1385 is tagged low_hanging_fruit.


def _do_cell_alignment(self):
Expand Down Expand Up @@ -346,7 +352,8 @@ def _auto_set_font_size(self, renderer):
cells = []
for key, cell in self._cells.iteritems():
# ignore auto-sized columns
if key[1] in self._autoColumns: continue
if key[1] in self._autoColumns:
continue
size = cell.auto_set_font_size(renderer)
fontsize = min(fontsize, size)
cells.append(cell)
Expand Down Expand Up @@ -376,8 +383,8 @@ def _offset(self, ox, oy):

for c in self._cells.itervalues():
x, y = c.get_x(), c.get_y()
c.set_x(x+ox)
c.set_y(y+oy)
c.set_x(x + ox)
c.set_y(y + oy)

def _update_positions(self, renderer):
# called from renderer to allow more precise estimates of
Expand All @@ -394,12 +401,12 @@ def _update_positions(self, renderer):
self._do_cell_alignment()

bbox = self._get_grid_bbox(renderer)
l,b,w,h = bbox.bounds
l, b, w, h = bbox.bounds

if self._bbox is not None:
# Position according to bbox
rl, rb, rw, rh = self._bbox
self.scale(rw/w, rh/h)
self.scale(rw / w, rh / h)
ox = rl - l
oy = rb - b
self._do_cell_alignment()
Expand All @@ -408,8 +415,8 @@ def _update_positions(self, renderer):
(BEST, UR, UL, LL, LR, CL, CR, LC, UC, C,
TR, TL, BL, BR, R, L, T, B) = range(len(self.codes))
# defaults for center
ox = (0.5-w/2)-l
oy = (0.5-h/2)-b
ox = (0.5 - w / 2) - l
oy = (0.5 - h / 2) - b
if self._loc in (UL, LL, CL): # left
ox = self.AXESPAD - l
if self._loc in (BEST, UR, LR, R, CR): # right
Expand All @@ -419,26 +426,26 @@ def _update_positions(self, renderer):
if self._loc in (LL, LR, LC): # lower
oy = self.AXESPAD - b
if self._loc in (LC, UC, C): # center x
ox = (0.5-w/2)-l
ox = (0.5 - w / 2) - l
if self._loc in (CL, CR, C): # center y
oy = (0.5-h/2)-b
oy = (0.5 - h / 2) - b

if self._loc in (TL, BL, L): # out left
ox = - (l + w)
ox = - (l + w)
if self._loc in (TR, BR, R): # out right
ox = 1.0 - l
if self._loc in (TR, TL, T): # out top
oy = 1.0 - b
if self._loc in (BL, BR, B): # out bottom
oy = - (b + h)
oy = - (b + h)

self._offset(ox, oy)


def get_celld(self):
'return a dict of cells in the table'
return self._cells


def table(ax,
cellText=None, cellColours=None,
cellLoc='right', colWidths=None,
Expand Down Expand Up @@ -477,7 +484,7 @@ def table(ax,

# Set colwidths if not given
if colWidths is None:
colWidths = [1.0/cols] * cols
colWidths = [1.0 / cols] * cols

# Check row and column labels
rowLabelWidth = 0
Expand Down Expand Up @@ -514,7 +521,7 @@ def table(ax,
# Add the cells
for row in xrange(rows):
for col in xrange(cols):
table.add_cell(row+offset, col,
table.add_cell(row + offset, col,
width=colWidths[col], height=height,
text=cellText[row][col],
facecolor=cellColours[row][col],
Expand All @@ -530,7 +537,7 @@ def table(ax,
# Do row labels
if rowLabels is not None:
for row in xrange(rows):
table.add_cell(row+offset, -1,
table.add_cell(row + offset, -1,
width=rowLabelWidth or 1e-15, height=height,
text=rowLabels[row], facecolor=rowColours[row],
loc=rowLoc)
Expand Down