Skip to content

Commit ce76606

Browse files
authored
Merge pull request #31 from tekktrik/doc/add-typing
Add typing, update documentation
2 parents fb786ea + e49d3ad commit ce76606

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed

adafruit_pyoa.py

+53-22
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
from adafruit_button import Button
4949
import terminalio
5050

51+
try:
52+
from typing import Dict, Optional, List
53+
except ImportError:
54+
pass
55+
5156
__version__ = "0.0.0-auto.0"
5257
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PYOA.git"
5358

@@ -56,7 +61,7 @@ class PYOA_Graphics:
5661
# pylint: disable=too-many-instance-attributes
5762
"""A choose your own adventure game framework."""
5863

59-
def __init__(self):
64+
def __init__(self) -> None:
6065
self.root_group = displayio.Group()
6166
self._display = board.DISPLAY
6267
self._background_group = displayio.Group()
@@ -113,7 +118,7 @@ def __init__(self):
113118
self._right_button = None
114119
self._middle_button = None
115120

116-
def load_game(self, game_directory):
121+
def load_game(self, game_directory: str) -> None:
117122
"""Load a game.
118123
119124
:param str game_directory: where the game files are stored
@@ -183,7 +188,7 @@ def load_game(self, game_directory):
183188
except OSError as err:
184189
raise OSError("Could not open game file " + self._gamefilename) from err
185190

186-
def _fade_to_black(self):
191+
def _fade_to_black(self) -> None:
187192
"""Turn down the lights."""
188193
if self.mouse_cursor:
189194
self.mouse_cursor.is_hidden = True
@@ -196,10 +201,11 @@ def _fade_to_black(self):
196201
if self.mouse_cursor:
197202
self.mouse_cursor.is_hidden = False
198203

199-
def _display_buttons(self, card):
204+
def _display_buttons(self, card: Dict[str, str]) -> None:
200205
"""Display the buttons of a card.
201206
202207
:param card: The active card
208+
:type card: dict(str, str)
203209
"""
204210
button01_text = card.get("button01_text", None)
205211
button02_text = card.get("button02_text", None)
@@ -213,17 +219,19 @@ def _display_buttons(self, card):
213219
self._button_group.append(self._right_button)
214220
self._button_group.append(self._left_button)
215221

216-
def _display_background_for(self, card):
222+
def _display_background_for(self, card: Dict[str, str]) -> None:
217223
"""If there's a background on card, display it.
218224
219225
:param card: The active card
226+
:type card: dict(str, str)
220227
"""
221228
self.set_background(card.get("background_image", None), with_fade=False)
222229

223-
def _display_text_for(self, card):
230+
def _display_text_for(self, card: Dict[str, str]) -> None:
224231
"""Display the main text of a card.
225232
226233
:param card: The active card
234+
:type card: dict(str, str)
227235
"""
228236
text = card.get("text", None)
229237
text_color = card.get("text_color", 0x0) # default to black
@@ -245,10 +253,11 @@ def _display_text_for(self, card):
245253

246254
self.set_text(text, text_color, background_color=text_background_color)
247255

248-
def _play_sound_for(self, card):
256+
def _play_sound_for(self, card: Dict[str, str]) -> None:
249257
"""If there's a sound, start playing it.
250258
251259
:param card: The active card
260+
:type card: dict(str, str)
252261
"""
253262
sound = card.get("sound", None)
254263
loop = card.get("sound_repeat", False)
@@ -257,12 +266,13 @@ def _play_sound_for(self, card):
257266
print("Loop:", loop)
258267
self.play_sound(sound, wait_to_finish=False, loop=loop)
259268

260-
def _wait_for_press(self, card):
269+
def _wait_for_press(self, card: Dict[str, str]) -> str:
261270
"""Wait for a button to be pressed.
262271
263272
:param card: The active card
264-
265-
Return the id of the destination card.
273+
:type card: dict(str, str)
274+
:return: The id of the destination card
275+
:rtype: str
266276
"""
267277
button01_text = card.get("button01_text", None)
268278
button02_text = card.get("button02_text", None)
@@ -294,10 +304,12 @@ def _wait_for_press(self, card):
294304
return card.get("button02_goto_card_id", None)
295305
time.sleep(0.1)
296306

297-
def display_card(self, card_num):
307+
def display_card(self, card_num: int) -> int:
298308
"""Display and handle input on a card.
299309
300310
:param int card_num: the index of the card to process
311+
:return: the card number of the selected card
312+
:rtype: int
301313
"""
302314
card = self._game[card_num]
303315
print(card)
@@ -332,11 +344,18 @@ def display_card(self, card_num):
332344
"Could not find card with matching 'card_id': ", destination_card_id
333345
)
334346

335-
def play_sound(self, filename, *, wait_to_finish=True, loop=False):
347+
def play_sound(
348+
self,
349+
filename: Optional[str],
350+
*,
351+
wait_to_finish: bool = True,
352+
loop: bool = False
353+
) -> None:
336354
"""Play a sound
337355
338-
:param Union(None,str) filename: The filename of the sound to play. Use `None` to stop
356+
:param filename: The filename of the sound to play. Use `None` to stop
339357
playing anything.
358+
:type filename: str or None
340359
:param bool wait_to_finish: Whether playing the sound should block
341360
:param bool loop: Whether the sound should loop
342361
"""
@@ -367,12 +386,20 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
367386
self._wavfile = None
368387
self._speaker_enable.value = False
369388

370-
def set_text(self, text, color, background_color=None):
389+
def set_text(
390+
self,
391+
text: Optional[str],
392+
color: Optional[str],
393+
background_color: Optional[int] = None,
394+
) -> None:
371395
"""Display the test for a card.
372396
373-
:param Union(None,str) text: the text to display
374-
:param Union(None,int) color: the text color
375-
397+
:param text: the text to display
398+
:type text: str or None
399+
:param color: the text color
400+
:type color: str or None
401+
:param background_color: the background color of the text
402+
:type background_color: int or None
376403
"""
377404
if self._text_group:
378405
self._text_group.pop()
@@ -400,10 +427,13 @@ def set_text(self, text, color, background_color=None):
400427
self._text.background_color = background_color
401428
self._text_group.append(self._text)
402429

403-
def set_background(self, filename, *, with_fade=True):
430+
def set_background(
431+
self, filename: Optional[str], *, with_fade: bool = True
432+
) -> None:
404433
"""The background image to a bitmap file.
405434
406-
:param Union(None,str) filename: The filename of the chosen background
435+
:param filename: The filename of the chosen background
436+
:type filename: str or None
407437
:param bool with_fade: If `True` fade out the backlight while loading the new background
408438
and fade in the backlight once completed.
409439
"""
@@ -424,7 +454,7 @@ def set_background(self, filename, *, with_fade=True):
424454
self._display.refresh(target_frames_per_second=60)
425455
self.backlight_fade(1.0)
426456

427-
def backlight_fade(self, to_light):
457+
def backlight_fade(self, to_light: float) -> None:
428458
"""
429459
Adjust the TFT backlight. Fade from the current value to the ``to_light`` value
430460
@@ -444,12 +474,13 @@ def backlight_fade(self, to_light):
444474

445475
# return a list of lines with wordwrapping
446476
@staticmethod
447-
def wrap_nicely(string, max_chars):
477+
def wrap_nicely(string: str, max_chars: int) -> List[str]:
448478
"""A helper that will return a list of lines with word-break wrapping.
449479
450480
:param str string: The text to be wrapped.
451481
:param int max_chars: The maximum number of characters on a line before wrapping.
452-
482+
:return: The list of lines
483+
:rtype: list(str)
453484
"""
454485
# string = string.replace('\n', '').replace('\r', '') # strip confusing newlines
455486
words = string.split(" ")

0 commit comments

Comments
 (0)