Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/PascalTriangle.activity/pascaltriangle.py
diff options
context:
space:
mode:
authorPhilip Withnall <philip@tecnocode.co.uk>2013-08-20 13:55:21 (GMT)
committer Philip Withnall <philip@tecnocode.co.uk>2013-08-20 13:55:21 (GMT)
commita8ef14287160cf5c70be712e0ff4fc6495eb6412 (patch)
tree0eb9ff386871006b906efa069ed55aa060d18ded /PascalTriangle.activity/pascaltriangle.py
parent9f834e103326d0fa31ec3f50f7e7e82adc55e74c (diff)
Reformat code for PEP8 compliance
Diffstat (limited to 'PascalTriangle.activity/pascaltriangle.py')
-rw-r--r--PascalTriangle.activity/pascaltriangle.py91
1 files changed, 48 insertions, 43 deletions
diff --git a/PascalTriangle.activity/pascaltriangle.py b/PascalTriangle.activity/pascaltriangle.py
index e478821..ad1dc0e 100644
--- a/PascalTriangle.activity/pascaltriangle.py
+++ b/PascalTriangle.activity/pascaltriangle.py
@@ -25,7 +25,8 @@ from sugar3.graphics.toolbarbox import ToolbarButton
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.toggletoolbutton import ToggleToolButton
-import math, random
+import math
+import random
from gi.repository import Gtk, Gdk
import cairo
from gettext import gettext as _
@@ -34,9 +35,9 @@ from gettext import gettext as _
class PascalTriangleActivity(activity.Activity):
"""Pascal's Triangle arithmetic activity.
- This is a simple Sugar activity which presents Pascal's Triangle in the form
- of a game, requiring the user to fill in blank cells in the triangle until
- it is complete.
+ This is a simple Sugar activity which presents Pascal's Triangle in the
+ form of a game, requiring the user to fill in blank cells in the triangle
+ until it is complete.
It supports multiple sizes of triangle, and also supports highlighting the
cells which contribute to the currently selected one.
@@ -72,12 +73,12 @@ class PascalTriangleActivity(activity.Activity):
main_toolbar.insert(separator, -1)
slider = Gtk.HScale()
- slider.props.digits = 0 # integers only
+ slider.props.digits = 0 # integers only
slider.props.draw_value = False
slider.props.has_origin = False
slider.set_range(2, 10)
slider.set_increments(1, 2)
- slider.set_value(5) # initial triangle size
+ slider.set_value(5) # initial triangle size
slider.set_size_request(150, 15)
slider.show()
@@ -98,7 +99,7 @@ class PascalTriangleActivity(activity.Activity):
# Create a new GTK+ drawing area
drawing_area = Gtk.DrawingArea()
- drawing_area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | \
+ drawing_area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK |
Gdk.EventMask.KEY_PRESS_MASK)
drawing_area.set_can_focus(True)
drawing_area.connect('button-press-event',
@@ -127,7 +128,7 @@ class PascalTriangleActivity(activity.Activity):
self._alert = None
# Various initial declarations.
- self._padding = 10.0 # Cairo units; padding around the drawing area
+ self._padding = 10.0 # Cairo units; padding around the drawing area
self._blank_cells = []
self._current_cell = (-1, -1)
self._current_cell_text = ''
@@ -165,7 +166,7 @@ class PascalTriangleActivity(activity.Activity):
def _calculate_number_of_cells(self):
"""Calculate the number of cells in the triangle.
-
+
This is the Nth triangle number, where N is the triangle_size. The
formula for this is 1/2*N*(N+1).
"""
@@ -173,7 +174,7 @@ class PascalTriangleActivity(activity.Activity):
def _generate_blank_cell_list(self):
"""Generate a non-empty list of random cell indices.
-
+
All cells are guaranteed to exist in the current triangle and are
guaranteed to be unique.
"""
@@ -193,7 +194,7 @@ class PascalTriangleActivity(activity.Activity):
def _calculate_pascal_number(self, index):
"""Calculate the Pascal number for the (row, column) cell.
-
+
row and column are both 0-based. This is equivalent to calculating the
binomial coefficient of (row choose column).
"""
@@ -204,7 +205,7 @@ class PascalTriangleActivity(activity.Activity):
denom = math.factorial(column) * math.factorial(row - column)
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):
"""Handle a mouse button press in the drawing area."""
# Check whether the click fell within a cell; if so, change the cell
# selection.
@@ -227,7 +228,9 @@ class PascalTriangleActivity(activity.Activity):
for column_index in range(row_order):
index = (row_index, column_index)
cell_position = self._calculate_cell_position(base_width,
- cell_width, cell_height, index)
+ cell_width,
+ cell_height,
+ index)
if self._is_cursor_in_radius(radius, cell_position,
(event.x, event.y)):
@@ -246,7 +249,7 @@ class PascalTriangleActivity(activity.Activity):
(cell_position[1] - cursor_position[1]) ** 2
return (actual_radius_sq <= radius ** 2)
- def __drawing_area_key_press_cb(self, widget, event, data = None):
+ def __drawing_area_key_press_cb(self, widget, event, data=None):
"""Handle a keyboard button press in the drawing area."""
if event.type != Gdk.EventType.KEY_PRESS:
return False
@@ -294,7 +297,7 @@ class PascalTriangleActivity(activity.Activity):
# If the key pressed wasn't a digit or control character, ignore it.
return True
- def __drawing_area_draw_cb(self, widget, ctx, data = None):
+ def __drawing_area_draw_cb(self, widget, ctx, data=None):
"""Redraw the drawing area and all its contents."""
# Widget allocation and sizes. The cell_height is calculated weirdly
# because the cells interlock as they tesselate; so for 2 rows, the
@@ -319,7 +322,9 @@ class PascalTriangleActivity(activity.Activity):
# Calculate the cell position.
(cell_x, cell_y) = self._calculate_cell_position(base_width,
- cell_width, cell_height, index)
+ cell_width,
+ cell_height,
+ index)
# Move to the cell position and draw the cell.
ctx.move_to(cell_x, cell_y)
@@ -330,41 +335,41 @@ class PascalTriangleActivity(activity.Activity):
def _calculate_cell_position(self, base_width, cell_width,
cell_height, index):
"""Calculate the cell position.
-
+
Add an offset every odd row so the triangle is balanced. Each row is
only 2/3 of cell_height because the hexagons interlock as they
tesselate.
"""
- cell_x = self._padding + \
- base_width / 2.0 - (cell_width / 2.0 * index[0]) + \
- cell_width * index[1]
- cell_y = self._padding + cell_height / 2.0 + \
- (cell_height * index[0] * (2.0 / 3.0))
+ cell_x = (self._padding +
+ base_width / 2.0 - (cell_width / 2.0 * index[0]) +
+ cell_width * index[1])
+ cell_y = (self._padding + cell_height / 2.0 +
+ (cell_height * index[0] * (2.0 / 3.0)))
return (cell_x, cell_y)
def _get_cell_background(self, index):
"""Get the background colour to use for the given cell."""
if index == self._current_cell:
# Currently selected cell.
- return cairo.SolidPattern(0.541, 0.886, 0.204) # green
- elif self._show_hints and self._current_cell != (-1, -1) and \
- (self._current_cell[1] == 0 or \
- self._current_cell[1] == self._current_cell[0]) and \
- (index[1] == 0 or index[1] == index[0]):
+ return cairo.SolidPattern(0.541, 0.886, 0.204) # green
+ elif (self._show_hints and self._current_cell != (-1, -1) and
+ (self._current_cell[1] == 0 or
+ self._current_cell[1] == self._current_cell[0]) and
+ (index[1] == 0 or index[1] == index[0])):
# Hint all edge cells if the currently selected cell is on an edge.
- return cairo.SolidPattern(0.447, 0.624, 0.812) # blue
- elif self._show_hints and index[0] == self._current_cell[0] - 1 and \
- (index[1] == self._current_cell[1] - 1 or \
- index[1] == self._current_cell[1]):
+ return cairo.SolidPattern(0.447, 0.624, 0.812) # blue
+ elif (self._show_hints and index[0] == self._current_cell[0] - 1 and
+ (index[1] == self._current_cell[1] - 1 or
+ index[1] == self._current_cell[1])):
# Hint the two cells above the currently selected cell.
- return cairo.SolidPattern(0.988, 0.914, 0.310) # yellow
+ return cairo.SolidPattern(0.988, 0.914, 0.310) # yellow
else:
# Non-selected, normal cell background.
- return cairo.SolidPattern(1.0, 1.0, 1.0) # white
+ return cairo.SolidPattern(1.0, 1.0, 1.0) # white
def _draw_cell(self, ctx, index, cell_width, cell_height):
"""Draw a single cell.
-
+
This draws the indexth cell at the current position in the given Cairo
context. The cell width and height are as given.
"""
@@ -390,17 +395,17 @@ class PascalTriangleActivity(activity.Activity):
cell_text = None
if not index in self._blank_cells:
cell_text = str(self._calculate_pascal_number(index))
- ctx.set_source_rgb(0.0, 0.0, 0.0) # black
+ ctx.set_source_rgb(0.0, 0.0, 0.0) # black
elif index != self._current_cell:
# TRANS: This is the text shown in cells which haven't yet
# been filled in by the user.
cell_text = _('?')
- ctx.set_source_rgb(0.4, 0.4, 0.4) # grey
+ ctx.set_source_rgb(0.4, 0.4, 0.4) # grey
else:
cell_text = self._current_cell_text
- ctx.set_source_rgb(0.8, 0.0, 0.0) # red
+ ctx.set_source_rgb(0.8, 0.0, 0.0) # red
- if cell_text != None:
+ if cell_text is not None:
# Rule of thumb to scale the font size with the cells.
font_size = int(50.0 / (float(self._triangle_size) / 5.0))
@@ -413,8 +418,8 @@ class PascalTriangleActivity(activity.Activity):
def _check_current_cell_text(self):
"""Check the user-entered text for the current cell.
- If it matches the expected value, also check to see if the user's filled
- out all blank cells and hence has won.
+ If it matches the expected value, also check to see if the user's
+ filled in all blank cells and hence has won.
"""
# Check whether the answer is correct. If so, change the cell to be
@@ -430,11 +435,11 @@ class PascalTriangleActivity(activity.Activity):
alert.props.title = _('You\'ve won!')
alert.props.msg = _('Well done! You\'ve completed the Pascal '
'Triangle. Do you want to play again?')
- icon = Icon(icon_name = 'emblem-favorite')
+ icon = Icon(icon_name='emblem-favorite')
alert.props.icon = icon
icon.show()
- icon = Icon(icon_name = 'add')
+ icon = Icon(icon_name='add')
alert.add_button(Gtk.ResponseType.ACCEPT, _('New Game'), icon)
icon.show()
@@ -459,7 +464,7 @@ 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):
"""Handle value changes on the triangle size slider."""
new_triangle_size = int(widget.get_value())