Skip to content

Commit

Permalink
PEP572 Use Python's assignment expression
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Apr 8, 2024
1 parent 5cda34f commit 90f9bd0
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 29 deletions.
3 changes: 1 addition & 2 deletions buildconfig/msysio.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ def print_(*args, **kwds):

stream = kwds.get('file', sys.stdout)
sep = kwds.get('sep', ' ')
end = kwds.get('end', '\n')

if args:
stream.write(sep.join([str(arg) for arg in args]))
if end:
if end := kwds.get('end', '\n'):
stream.write(end)
try:
stream.flush()
Expand Down
3 changes: 1 addition & 2 deletions src_py/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,7 @@ def remove_internal(self, sprite):
:param sprite: The sprite we are removing.
"""
lost_rect = self.spritedict[sprite]
if lost_rect:
if lost_rect := self.spritedict[sprite]:
self.lostsprites.append(lost_rect)
del self.spritedict[sprite]

Expand Down
3 changes: 1 addition & 2 deletions src_py/sysfont.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ def _font_finder_darwin():
"/System/Library/Fonts/Supplemental",
]

username = os.getenv("USER")
if username:
if username := os.getenv("USER"):
locations.append(f"/Users/{username}/Library/Fonts")

strange_root = "/System/Library/Assets/com_apple_MobileAsset_Font3"
Expand Down
18 changes: 6 additions & 12 deletions test/controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,19 @@ def _get_first_controller(self):
return controller.Controller(i)

def test_construction(self):
c = self._get_first_controller()
if c:
if c := self._get_first_controller():
self.assertIsInstance(c, controller.Controller)
else:
self.skipTest("No controller connected")

def test__auto_init(self):
c = self._get_first_controller()
if c:
if c := self._get_first_controller():
self.assertTrue(c.get_init())
else:
self.skipTest("No controller connected")

def test_get_init(self):
c = self._get_first_controller()
if c:
if c := self._get_first_controller():
self.assertTrue(c.get_init())
c.quit()
self.assertFalse(c.get_init())
Expand All @@ -111,25 +108,22 @@ def test_from_joystick(self):
self.assertIsInstance(c, controller.Controller)

def test_as_joystick(self):
c = self._get_first_controller()
if c:
if c := self._get_first_controller():
joy = c.as_joystick()
self.assertIsInstance(joy, type(pygame.joystick.Joystick(0)))
else:
self.skipTest("No controller connected")

def test_get_mapping(self):
c = self._get_first_controller()
if c:
if c := self._get_first_controller():
mapping = c.get_mapping()
self.assertIsInstance(mapping, dict)
self.assertIsNotNone(mapping["a"])
else:
self.skipTest("No controller connected")

def test_set_mapping(self):
c = self._get_first_controller()
if c:
if c := self._get_first_controller():
mapping = c.get_mapping()
mapping["a"] = "b3"
mapping["y"] = "b0"
Expand Down
3 changes: 1 addition & 2 deletions test/mask_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def random_mask(size=(100, 100)):

def maskFromSurface(surface, threshold=127):
mask = pygame.Mask(surface.get_size())
key = surface.get_colorkey()
if key:
if key := surface.get_colorkey():
for y in range(surface.get_height()):
for x in range(surface.get_width()):
if surface.get_at((x + 0.1, y + 0.1)) != key:
Expand Down
10 changes: 3 additions & 7 deletions test/mixer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ def test_set_soundfont(self):
mixer.init()

# test that initially, get_soundfont returns only real files
initial_sf = mixer.get_soundfont()
if initial_sf is not None:
if (initial_sf := mixer.get_soundfont()) is not None:
for i in initial_sf.split(";"):
os.path.exists(i)

Expand Down Expand Up @@ -384,8 +383,7 @@ def _test_array_argument(self, format, a, test_pass):
)
a2 = array(snd)
a3 = a.astype(a2.dtype)
lshift = abs(format) - 8 * a.itemsize
if lshift >= 0:
if (lshift := abs(format) - 8 * a.itemsize) >= 0:
# This is asymmetric with respect to downcasting.
a3 <<= lshift
self.assertTrue(all_(a2 == a3), "Format %i, dtype %s" % (format, a.dtype))
Expand Down Expand Up @@ -552,9 +550,7 @@ def test_find_channel(self):
filename = example_path(os.path.join("data", "house_lo.wav"))
sound = mixer.Sound(file=filename)

num_channels = mixer.get_num_channels()

if num_channels > 0:
if (num_channels := mixer.get_num_channels()) > 0:
found_channel = mixer.find_channel()
self.assertIsNotNone(found_channel)

Expand Down
3 changes: 1 addition & 2 deletions test/test_utils/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ def combine_results(all_results, t):


def get_test_results(raw_return):
test_results = TEST_RESULTS_RE.search(raw_return)
if test_results:
if test_results := TEST_RESULTS_RE.search(raw_return):
try:
return eval(test_results.group(1))
except:
Expand Down

0 comments on commit 90f9bd0

Please sign in to comment.