From c32a9cc1653661abf03e129893be6efa4118bb3e Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 19 Aug 2013 21:23:06 +0000 Subject: Python code cleanups In response to running pylint on the code. --- diff --git a/PascalTriangle.activity/pascaltriangle.py b/PascalTriangle.activity/pascaltriangle.py index bda6cf5..d22aa26 100755 --- a/PascalTriangle.activity/pascaltriangle.py +++ b/PascalTriangle.activity/pascaltriangle.py @@ -60,10 +60,10 @@ class PascalTriangleActivity(activity.Activity): Gdk.EventMask.KEY_PRESS_MASK) drawing_area.set_can_focus(True) drawing_area.connect('button-press-event', - self._drawing_area_button_press_cb, None) + self.__drawing_area_button_press_cb, None) drawing_area.connect('key-press-event', - self._drawing_area_key_press_cb, None) - drawing_area.connect('draw', self._drawing_area_draw_cb, None) + self.__drawing_area_key_press_cb, None) + drawing_area.connect('draw', self.__drawing_area_draw_cb, None) # Create an overlay and a slider to allow the triangle size to be # adjusted. This is the number of cells on the triangle's base @@ -87,7 +87,7 @@ class PascalTriangleActivity(activity.Activity): overlay.add_overlay(slider) self._triangle_size = int(slider.get_value()) - slider.connect('value-changed', self._slider_value_changed_cb, None) + slider.connect('value-changed', self.__slider_value_changed_cb, None) # Parent and show the drawing area. self.set_canvas(overlay) @@ -98,14 +98,20 @@ class PascalTriangleActivity(activity.Activity): self._show_hints = False self._alert = None + # Various initial declarations. + self._padding = 10.0 # Cairo units; padding around the drawing area + self._blank_cells = [] + self._current_cell = (-1, -1) + self._current_cell_text = '' + # Start a new game. - self._start_game() + self.start_game() """ Start a new game, clearing the previous game. """ - def _start_game(self): + def start_game(self): # Focus the drawing area so it can receive keyboard events. self._drawing_area.grab_focus() @@ -114,9 +120,6 @@ class PascalTriangleActivity(activity.Activity): self.remove_alert(self._alert) self._alert = None - # Padding around the edge of the drawing area. - self._padding = 10.0 # Cairo units - # Set the currently selected cell (which the user's clicked on). # Default to no selection (-1, -1). self._current_cell = (-1, -1) @@ -149,7 +152,7 @@ class PascalTriangleActivity(activity.Activity): # Generate a number of coordinates for blank cells, between 1 cell and # the entire triangle. num_blanks = random.randint(1, self._calculate_number_of_cells()) - for _ in range(num_blanks): + for i in range(num_blanks): row_index = random.randint(0, self._triangle_size - 1) column_index = random.randint(0, row_index) blank_cells.append((row_index, column_index)) @@ -173,7 +176,7 @@ class PascalTriangleActivity(activity.Activity): return num / denom - def _drawing_area_button_press_cb(self, widget, event, data = None): + def __drawing_area_button_press_cb(self, widget, event, data = None): # Check whether the click fell within a cell; if so, change the cell # selection. if event.type != Gdk.EventType.BUTTON_PRESS: @@ -194,13 +197,11 @@ class PascalTriangleActivity(activity.Activity): row_order = row_index + 1 for column_index in range(row_order): index = (row_index, column_index) - (cell_x, cell_y) = self._calculate_cell_position(base_width, + cell_position = self._calculate_cell_position(base_width, cell_width, cell_height, index) - actual_radius_sq = (cell_x - event.x) ** 2 + \ - (cell_y - event.y) ** 2 - - if actual_radius_sq <= radius ** 2: + if self._is_cursor_in_radius(radius, cell_position, + (event.x, event.y)): # Found the cell. self._update_current_cell(index) return True @@ -210,7 +211,13 @@ class PascalTriangleActivity(activity.Activity): return True - def _drawing_area_key_press_cb(self, widget, event, data = None): + def _is_cursor_in_radius(self, radius, cell_position, cursor_position): + actual_radius_sq = (cell_position[0] - cursor_position[0]) ** 2 + \ + (cell_position[1] - cursor_position[1]) ** 2 + return (actual_radius_sq <= radius ** 2) + + + def __drawing_area_key_press_cb(self, widget, event, data = None): if event.type != Gdk.EventType.KEY_PRESS: return False @@ -244,7 +251,7 @@ class PascalTriangleActivity(activity.Activity): widget.queue_draw() # Check whether the answer is correct. - self._check_current_cell_text(widget) + self._check_current_cell_text() return True # Otherwise, handle the control character @@ -258,7 +265,7 @@ class PascalTriangleActivity(activity.Activity): return True - def _drawing_area_draw_cb(self, widget, ctx, data = None): + def __drawing_area_draw_cb(self, widget, ctx, data = None): # Widget allocation and sizes. The cell_height is calculated weirdly # because the cells interlock as they tesselate; so for 2 rows, the # bottom third of the top row overlaps with the top third of the bottom @@ -369,7 +376,7 @@ class PascalTriangleActivity(activity.Activity): expected value. If so, also check to see if the user's filled out all blank cells and hence has won. """ - def _check_current_cell_text(self, widget): + def _check_current_cell_text(self): # Check whether the answer is correct. If so, change the cell to be # uneditable. expected_num = self._calculate_pascal_number(self._current_cell) @@ -391,15 +398,15 @@ class PascalTriangleActivity(activity.Activity): alert.add_button(Gtk.ResponseType.ACCEPT, _('New Game'), icon) icon.show() - alert.connect('response', self._alert_response_cb) + alert.connect('response', self.__alert_response_cb) alert.show() self._alert = alert self.add_alert(alert) - def _alert_response_cb(self, alert, response_id): - self._start_game() + def __alert_response_cb(self, alert, response_id): + self.start_game() def get_show_hints(self): @@ -412,13 +419,13 @@ class PascalTriangleActivity(activity.Activity): show_hints = property(get_show_hints, set_show_hints) - def _slider_value_changed_cb(self, widget, data = None): + def __slider_value_changed_cb(self, widget, data = None): new_triangle_size = int(widget.get_value()) if new_triangle_size != self._triangle_size: # Start a new game with the new triangle size. self._triangle_size = new_triangle_size - self._start_game() + self.start_game() class NewGameButton(ToolButton): @@ -430,7 +437,7 @@ class NewGameButton(ToolButton): parent_activity) def __new_game_button_clicked_cb(self, button, parent_activity): - parent_activity._start_game() + parent_activity.start_game() class HintButton(ToggleToolButton): -- cgit v0.9.1