From 566c1497e35e05c38b2762890ee442c824c7f405 Mon Sep 17 00:00:00 2001 From: Daniel Pope Date: Fri, 15 Nov 2019 08:38:48 +0000 Subject: [PATCH] Fix flake8 errors in pgzero --- pgzero/animation.py | 6 ++++-- pgzero/builtins.py | 10 ++++++++++ pgzero/clock.py | 2 ++ pgzero/keyboard.py | 3 ++- pgzero/loaders.py | 9 ++++++--- pgzero/music.py | 1 + pgzero/ptext.py | 4 ++++ pgzero/rect.py | 18 ++++++++++++++++++ pgzero/runner.py | 25 ++++++++++--------------- pgzero/screen.py | 5 +++-- pgzero/soundfmt.py | 1 + pgzero/spellcheck.py | 2 +- pgzero/tone.py | 5 ++--- 13 files changed, 64 insertions(+), 27 deletions(-) diff --git a/pgzero/animation.py b/pgzero/animation.py index a48f6060..b89cddc3 100644 --- a/pgzero/animation.py +++ b/pgzero/animation.py @@ -49,6 +49,7 @@ def in_elastic(n): q -= 1.0 return -(pow(2, 10 * q) * sin((q - s) * (2 * pi) / p)) + @tweener def out_elastic(n): p = .3 @@ -58,6 +59,7 @@ def out_elastic(n): return 1.0 return pow(2, -10 * q) * sin((q - s) * (2 * pi) / p) + 1.0 + @tweener def in_out_elastic(n): p = .3 * 1.5 @@ -116,9 +118,9 @@ def tween(n, start, end): def tween_attr(n, start, end): if isinstance(start, tuple): - return tuple(tween(n, a, b) for a,b in zip(start, end)) + return tuple(tween(n, a, b) for a, b in zip(start, end)) elif isinstance(start, list): - return [tween(n, a, b) for a,b in zip(start, end)] + return [tween(n, a, b) for a, b in zip(start, end)] else: return tween(n, start, end) diff --git a/pgzero/builtins.py b/pgzero/builtins.py index 5ef1979a..52c50e07 100644 --- a/pgzero/builtins.py +++ b/pgzero/builtins.py @@ -13,3 +13,13 @@ from .constants import mouse, keys, keymods from .game import exit + +__all__ = [ + 'Actor', 'images', # graphics + 'sounds', 'music', 'tone', # sound + 'clock', 'animate', # timing + 'Rect', 'ZRect', # geometry + 'keyboard', 'mouse', 'keys', 'keymods', # input + 'storage', # persistence + 'exit', +] diff --git a/pgzero/clock.py b/pgzero/clock.py index 20c3bf1e..2f20a0de 100644 --- a/pgzero/clock.py +++ b/pgzero/clock.py @@ -50,6 +50,7 @@ class Event: Events are ordered by their scheduled execution time. """ + def __init__(self, time, cb, repeat=None): self.time = time self.repeat = repeat @@ -82,6 +83,7 @@ class Clock: scaling dt before passing it to tick(). """ + def __init__(self): self.t = 0 self.fired = False diff --git a/pgzero/keyboard.py b/pgzero/keyboard.py index 1a0a4353..69701d9c 100644 --- a/pgzero/keyboard.py +++ b/pgzero/keyboard.py @@ -6,6 +6,7 @@ DEPRECATED_KEY_RE = re.compile(r'[A-Z]') PREFIX_RE = re.compile(r'^K_(?!\d$)') + class Keyboard: """The current state of the keyboard. @@ -23,7 +24,7 @@ class Keyboard: def __getattr__(self, kname): # return is a reserved word, so alias enter to return if kname == 'enter': - kname = 'return' + kname = 'return' elif DEPRECATED_KEY_RE.match(kname): warn( "Uppercase keyboard attributes (eg. keyboard.%s) are " diff --git a/pgzero/loaders.py b/pgzero/loaders.py index 62bf6504..d7d2603f 100644 --- a/pgzero/loaders.py +++ b/pgzero/loaders.py @@ -2,7 +2,6 @@ import os.path import sys -from types import ModuleType import pygame.image import pygame.mixer @@ -33,6 +32,7 @@ def set_root(path): class InvalidCase(Exception): """Indicate case errors early so they don't bite cross-platform users.""" + try: import win32api except ImportError: @@ -86,6 +86,7 @@ class ResourceLoader: Dotted paths can be used to traverse directories. """ + def __init__(self, subpath): self._subpath = subpath self._cache = {} @@ -162,13 +163,14 @@ def __getattr__(self, name): def __dir__(self): standard_attributes = [key for key in self.__dict__.keys() - if not key.startswith("_")] + if not key.startswith("_")] resources = os.listdir(self._root()) resource_names = [os.path.splitext(r) for r in resources] loadable_names = [name for name, ext in resource_names - if name.isidentifier() and ext[1:] in self.EXTNS] + if name.isidentifier() and ext[1:] in self.EXTNS] return standard_attributes + loadable_names + class ImageLoader(ResourceLoader): EXTNS = ['png', 'gif', 'jpg', 'jpeg', 'bmp'] TYPE = 'image' @@ -232,6 +234,7 @@ def _load(self, path, fontsize=None): sounds = SoundLoader('sounds') fonts = FontLoader('fonts') + def getfont( fontname=None, fontsize=None, diff --git a/pgzero/music.py b/pgzero/music.py index c49efa2b..8e3e6fc5 100644 --- a/pgzero/music.py +++ b/pgzero/music.py @@ -27,6 +27,7 @@ class _MusicLoader(ResourceLoader): def _load(self, path): return path + _loader = _MusicLoader('music') diff --git a/pgzero/ptext.py b/pgzero/ptext.py index 30c2c8c9..28607d69 100644 --- a/pgzero/ptext.py +++ b/pgzero/ptext.py @@ -142,6 +142,7 @@ def wrap(text, fontname=None, fontsize=None, sysfontname=None, lines.append(line) return lines + _fit_cache = {} @@ -199,6 +200,7 @@ def _resolveangle(angle): angle %= 360 return int(round(angle / ANGLE_RESOLUTION_DEGREES)) * ANGLE_RESOLUTION_DEGREES + # Return the set of points in the circle radius r, using Bresenham's # circle algorithm _circle_cache = {} @@ -224,6 +226,7 @@ def _circlepoints(r): points.sort() return points + _surf_cache = {} _surf_tick_usage = {} _surf_size_total = 0 @@ -370,6 +373,7 @@ def getsurf(text, fontname=None, fontsize=None, sysfontname=None, bold=None, ita _tick += 1 return surf + _default_surf_sentinel = () diff --git a/pgzero/rect.py b/pgzero/rect.py index 6212fb66..b099b8c7 100644 --- a/pgzero/rect.py +++ b/pgzero/rect.py @@ -187,60 +187,70 @@ def __contains__(self, other): def _get_width(self): return self.w + def _set_width(self, width): self.w = width width = property(_get_width, _set_width) def _get_height(self): return self.h + def _set_height(self, height): self.h = height height = property(_get_height, _set_height) def _get_top(self): return self.y + def _set_top(self, top): self.y = top top = property(_get_top, _set_top) def _get_left(self): return self.x + def _set_left(self, left): self.x = left left = property(_get_left, _set_left) def _get_right(self): return self.x + self.w + def _set_right(self, right): self.x = right - self.w right = property(_get_right, _set_right) def _get_bottom(self): return self.y + self.h + def _set_bottom(self, bottom): self.y = bottom - self.h bottom = property(_get_bottom, _set_bottom) def _get_centerx(self): return self.x + (self.w / 2) + def _set_centerx(self, centerx): self.x = centerx - (self.w / 2) centerx = property(_get_centerx, _set_centerx) def _get_centery(self): return self.y + (self.h / 2) + def _set_centery(self, centery): self.y = centery - (self.h / 2) centery = property(_get_centery, _set_centery) def _get_topleft(self): return self.x, self.y + def _set_topleft(self, topleft): self.x, self.y = topleft topleft = property(_get_topleft, _set_topleft) def _get_topright(self): return self.x + self.w, self.y + def _set_topright(self, topright): x, y = topright self.x = x - self.w @@ -249,6 +259,7 @@ def _set_topright(self, topright): def _get_bottomleft(self): return self.x, self.y + self.h + def _set_bottomleft(self, bottomleft): x, y = bottomleft self.x = x @@ -257,6 +268,7 @@ def _set_bottomleft(self, bottomleft): def _get_bottomright(self): return self.x + self.w, self.y + self.h + def _set_bottomright(self, bottomright): x, y = bottomright self.x = x - self.w @@ -265,6 +277,7 @@ def _set_bottomright(self, bottomright): def _get_midtop(self): return self.x + self.w / 2, self.y + def _set_midtop(self, midtop): x, y = midtop self.x = x - self.w / 2 @@ -273,6 +286,7 @@ def _set_midtop(self, midtop): def _get_midleft(self): return self.x, self.y + self.h / 2 + def _set_midleft(self, midleft): x, y = midleft self.x = x @@ -281,6 +295,7 @@ def _set_midleft(self, midleft): def _get_midbottom(self): return self.x + self.w / 2, self.y + self.h + def _set_midbottom(self, midbottom): x, y = midbottom self.x = x - self.w / 2 @@ -289,6 +304,7 @@ def _set_midbottom(self, midbottom): def _get_midright(self): return self.x + self.w, self.y + self.h / 2 + def _set_midright(self, midright): x, y = midright self.x = x - self.w @@ -297,6 +313,7 @@ def _set_midright(self, midright): def _get_center(self): return self.x + self.w / 2, self.y + self.h / 2 + def _set_center(self, center): x, y = center self.x = x - self.w / 2 @@ -305,6 +322,7 @@ def _set_center(self, center): def _get_size(self): return self.w, self.h + def _set_size(self, size): self.w, self.h = size size = property(_get_size, _set_size) diff --git a/pgzero/runner.py b/pgzero/runner.py index 4fe478c2..439202e5 100644 --- a/pgzero/runner.py +++ b/pgzero/runner.py @@ -1,21 +1,17 @@ +from . import storage +from . import clock +from . import loaders +from .game import PGZeroGame, DISPLAY_FLAGS +from types import ModuleType +import argparse +import warnings +import sys +import os import pygame pygame.mixer.pre_init(frequency=22050, size=-16, channels=2) pygame.init() -import os -import sys -import warnings -import argparse -from types import ModuleType - -from .game import PGZeroGame, DISPLAY_FLAGS -from . import loaders -from . import clock -from . import builtins -from . import storage - - # The base URL for Pygame Zero documentation DOCS_URL = 'http://pygame-zero.readthedocs.io/en/stable' @@ -63,7 +59,6 @@ def _substitute_full_framework_python(): os.execv(framework_python, ['python', '-m', 'pgzero'] + sys.argv[1:]) - def main(): # Pygame won't run from a normal virtualenv copy of Python on a Mac if not _check_python_ok_for_pygame(): @@ -71,7 +66,7 @@ def main(): parser = argparse.ArgumentParser() try: - import ptpython + import ptpython # noqa: checking if this is importable except ImportError: replhelp = argparse.SUPPRESS have_repl = False diff --git a/pgzero/screen.py b/pgzero/screen.py index 113abb6b..2e7019d0 100644 --- a/pgzero/screen.py +++ b/pgzero/screen.py @@ -59,17 +59,18 @@ def filled_rect(self, rect, color): def text(self, *args, **kwargs): """Draw text to the screen.""" - #FIXME: expose ptext parameters, for autocompletion and autodoc + # FIXME: expose ptext parameters, for autocompletion and autodoc ptext.draw(*args, surf=self._surf, **kwargs) def textbox(self, *args, **kwargs): """Draw text to the screen, wrapped to fit a box""" - #FIXME: expose ptext parameters, for autocompletion and autodoc + # FIXME: expose ptext parameters, for autocompletion and autodoc ptext.drawbox(*args, surf=self._surf, **kwargs) class Screen: """Interface to the screen.""" + def __init__(self, surface): self.surface = surface self.width, self.height = surface.get_size() diff --git a/pgzero/soundfmt.py b/pgzero/soundfmt.py index b8d0a1e2..d7aef0cf 100644 --- a/pgzero/soundfmt.py +++ b/pgzero/soundfmt.py @@ -14,6 +14,7 @@ class MagicReader: """Interface to reading the magic numbers in a file's header.""" + def __init__(self, path): with open(path, 'rb') as f: self.bytes = f.read(64 * 1024) diff --git a/pgzero/spellcheck.py b/pgzero/spellcheck.py index 40f47865..6b23a027 100644 --- a/pgzero/spellcheck.py +++ b/pgzero/spellcheck.py @@ -134,7 +134,7 @@ def warn_event_handlers(self, typos, missing): for found, suggestion in typos: print(" {found} (did you mean {suggestion}?)".format( found=found, suggestion=suggestion)) - for f in missing_hooks: + for f in missing: print(" ", f) def error(self, msg, found, suggestion): diff --git a/pgzero/tone.py b/pgzero/tone.py index 04d0b58f..a7bc121e 100644 --- a/pgzero/tone.py +++ b/pgzero/tone.py @@ -20,8 +20,6 @@ on another CPU core, if present. """ - -from timeit import default_timer import re from functools import lru_cache @@ -76,6 +74,7 @@ def _play_thread(): note = _create(*args) note.play() + player_thread = Thread(target=_play_thread) player_thread.setDaemon(True) @@ -185,7 +184,7 @@ def play(pitch, duration): if np is None: - def play(hz, length): + def play(hz, length): # noqa: conditional redefinition raise RuntimeError( 'Tone generation depends on Numpy, which is not available' )