Skip to content

Commit 1b045a6

Browse files
committed
Linting
1 parent 64dd3bd commit 1b045a6

2 files changed

Lines changed: 23 additions & 32 deletions

File tree

PyPortal_Calculator/calculator.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
"""
22
Class that handles the input and calculations
33
"""
4-
5-
# Calculator Class
64
class Calculator:
75
def __init__(self, calc_display, clear_button, label_offset):
86
self._calc_display = calc_display
97
self._clear_button = clear_button
108
self._label_offset = label_offset
11-
self._all_clear()
9+
self.all_clear()
1210

13-
def _calculate(self, number_one, operator, number_two):
11+
def calculate(self, number_one, operator, number_two):
1412
result = eval(number_one + operator + number_two)
1513
if int(result) == result:
1614
result = int(result)
1715
return str(result)
1816

19-
def _all_clear(self):
17+
def all_clear(self):
2018
self._accumulator = "0"
2119
self._operator = None
2220
self._equal_pressed = False
23-
self._clear_entry()
21+
self.clear_entry()
2422

25-
def _clear_entry(self):
23+
def clear_entry(self):
2624
self._operand = None
2725
self._set_button_ce(False)
2826
self._set_text("0")
@@ -44,9 +42,9 @@ def _get_text(self):
4442
def add_input(self, input):
4543
try:
4644
if input == "AC":
47-
self._all_clear()
45+
self.all_clear()
4846
elif input == "CE":
49-
self._clear_entry()
47+
self.clear_entry()
5048
elif self._operator is None and input == "0":
5149
pass
5250
elif len(input) == 1 and 48 <= ord(input) <= 57:
@@ -66,7 +64,7 @@ def add_input(self, input):
6664
self._operand = display_text
6765
self._set_button_ce(True)
6866
self._equal_pressed = False
69-
elif input == "+" or input == "-" or input == "/" or input == "x":
67+
elif input in ('+', '-', '/', 'x'):
7068
if input == "x":
7169
input = "*"
7270
if self._equal_pressed:
@@ -76,7 +74,7 @@ def add_input(self, input):
7674
else:
7775
# Perform current calculation before changing inputs
7876
if self._operand is not None:
79-
self._accumulator = self._calculate(self._accumulator, self._operator, self._operand)
77+
self._accumulator = self.calculate(self._accumulator, self._operator, self._operand)
8078
self._set_text(self._accumulator)
8179
self._operand = None
8280
self._operator = input
@@ -88,22 +86,16 @@ def add_input(self, input):
8886
self._set_button_ce(True)
8987
self._equal_pressed = False
9088
elif input == "+/-":
91-
self._set_text(self._calculate(self._get_text(), "*", "-1"))
89+
self._set_text(self.calculate(self._get_text(), "*", "-1"))
9290
elif input == "%":
93-
self._set_text(self._calculate(self._get_text(), "/", "100"))
91+
self._set_text(self.calculate(self._get_text(), "/", "100"))
9492
elif input == "=":
9593
if self._operator is not None:
9694
if self._operand is None:
9795
self._operand = self._get_text()
98-
self._accumulator = self._calculate(self._accumulator, self._operator, self._operand)
96+
self._accumulator = self.calculate(self._accumulator, self._operator, self._operand)
9997
self._set_text(self._accumulator)
10098
self._equal_pressed = True
101-
# For Debugging
102-
print("\n----------------")
103-
print("Input: {}".format(input))
104-
print("Accumulator: {}".format(self._accumulator))
105-
print("Operator: {}".format(self._operator))
106-
print("Operand: {}".format(self._operand))
107-
except (ZeroDivisionError, RuntimeError) as error:
108-
self._all_clear()
99+
except (ZeroDivisionError, RuntimeError):
100+
self.all_clear()
109101
self._set_text("Error")

PyPortal_Calculator/code.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
PyPortal Calculator Demo
33
"""
44
import time
5+
from collections import namedtuple
56
import board
67
import displayio
7-
import os
8-
from collections import namedtuple
98
from adafruit_display_text.label import Label
109
from adafruit_bitmap_font import bitmap_font
1110
from adafruit_display_shapes.rect import Rect
1211
from adafruit_button import Button
1312
from calculator import Calculator
1413
import adafruit_touchscreen
15-
coords = namedtuple("Point", "x y")
14+
Coords = namedtuple("Point", "x y")
1615

1716
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
1817
board.TOUCH_YD, board.TOUCH_YU,
@@ -49,17 +48,17 @@
4948

5049
# Some button placement functions
5150
def button_grid(row, col):
52-
return coords(BUTTON_MARGIN * (row + 1) + BUTTON_WIDTH * row + 20,
51+
return Coords(BUTTON_MARGIN * (row + 1) + BUTTON_WIDTH * row + 20,
5352
BUTTON_MARGIN * (col + 1) + BUTTON_HEIGHT * col + 40)
5453

5554
def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
5655
pos = button_grid(row, col)
57-
button = Button(x=pos.x, y=pos.y,
58-
width=BUTTON_WIDTH * width + BUTTON_MARGIN * (width - 1), height=BUTTON_HEIGHT,
59-
label=label, label_font=font, label_color=text_color, fill_color=color,
60-
style=Button.ROUNDRECT)
61-
buttons.append(button)
62-
return button
56+
new_button = Button(x=pos.x, y=pos.y,
57+
width=BUTTON_WIDTH * width + BUTTON_MARGIN * (width - 1), height=BUTTON_HEIGHT,
58+
label=label, label_font=font, label_color=text_color, fill_color=color,
59+
style=Button.ROUNDRECT)
60+
buttons.append(new_button)
61+
return new_button
6362

6463
border = Rect(20, 8, 280, 35, fill=WHITE, outline=BLACK, stroke=2)
6564
calc_display = Label(font, text="0", color=BLACK, max_glyphs=MAX_DIGITS)

0 commit comments

Comments
 (0)