Skip to content

Commit

Permalink
Improve contine/return on branching
Browse files Browse the repository at this point in the history
  • Loading branch information
ppizarror committed Oct 21, 2024
1 parent bcb1939 commit c2b7590
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 70 deletions.
2 changes: 1 addition & 1 deletion pygame_menu/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __repr__(self) -> str:
'class id and id __repr__ cannot be True at the same time'
if self._class_id__repr__:
return self.get_class_id()
if self._id__repr__:
elif self._id__repr__:
return sup_repr.replace(' object at ', f'["{self.get_id()}"] object at ')
return sup_repr

Expand Down
5 changes: 2 additions & 3 deletions pygame_menu/_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,7 @@ def add_callable(
assert isinstance(pass_args, bool)
if pass_args:
return self._add_decor(DECORATION_CALLABLE, prev, fun)
else:
return self._add_decor(DECORATION_CALLABLE_NO_ARGS, prev, fun)
return self._add_decor(DECORATION_CALLABLE_NO_ARGS, prev, fun)

def add_textured_polygon(
self,
Expand Down Expand Up @@ -974,7 +973,7 @@ def _draw(self, deco: List[Tuple[int, str, Any]], surface: 'pygame.Surface') ->
if not self._decor_enabled[decoid]:
continue

if dtype == DECORATION_POLYGON:
elif dtype == DECORATION_POLYGON:
points, color, filled, width, gfx, kwargs = data
points = self._update_pos_list(rect, decoid, points, **kwargs)
if gfx:
Expand Down
24 changes: 12 additions & 12 deletions pygame_menu/_scrollarea.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def get_scrollbars_from_position(
return ''
elif position == POSITION_CENTER:
raise ValueError('cannot init scrollbars from center position')
else:
raise ValueError('unknown ScrollArea position')
raise ValueError('unknown ScrollArea position')


DEFAULT_SCROLLBARS = get_scrollbars_from_position(POSITION_SOUTHEAST)
Expand Down Expand Up @@ -504,7 +503,7 @@ def draw(self, surface: 'pygame.Surface') -> 'ScrollArea':
for sbar in self._scrollbars:
if not sbar.is_visible():
continue
if sbar.get_orientation() == ORIENTATION_HORIZONTAL:
elif sbar.get_orientation() == ORIENTATION_HORIZONTAL:
if self.get_hidden_width():
sbar.draw(surface)
else:
Expand Down Expand Up @@ -592,10 +591,10 @@ def get_border_size(self) -> Tuple2IntType:
"""
if isinstance(self._border_color, pygame_menu.BaseImage): # Image
return self._border_tiles_size
else: # Color
if self._border_color is None:
return 0, 0
return self._border_width, self._border_width
# Color
if self._border_color is None:
return 0, 0
return self._border_width, self._border_width

def get_hidden_width(self) -> int:
"""
Expand Down Expand Up @@ -629,7 +628,7 @@ def get_offsets(self) -> Tuple2IntType:
for sbar in self._scrollbars:
if not sbar.is_visible():
continue
if sbar.get_orientation() == ORIENTATION_HORIZONTAL:
elif sbar.get_orientation() == ORIENTATION_HORIZONTAL:
if self.get_hidden_width():
offsets[0] = sbar.get_value() # Cannot add as each scrollbar can only affect 1 axis only
else:
Expand Down Expand Up @@ -671,6 +670,7 @@ def get_scrollbar_thickness(self, orientation: str, visible: bool = True) -> int
return int(self._rect.height - self._view_rect.height)
elif orientation == ORIENTATION_VERTICAL:
return int(self._rect.width - self._view_rect.width)
return 0

def get_world_rect(self, absolute: bool = False) -> 'pygame.Rect':
"""
Expand Down Expand Up @@ -900,7 +900,7 @@ def get_scroll_value_percentage(self, orientation: str) -> float:
for sbar in self._scrollbars:
if not sbar.is_visible():
continue
if sbar.get_orientation() == orientation:
elif sbar.get_orientation() == orientation:
return sbar.get_value_percentage()
return -1

Expand All @@ -917,7 +917,7 @@ def scroll_to(self, orientation: str, value: NumberType) -> 'ScrollArea':
for sbar in self._scrollbars:
if not sbar.is_visible():
continue
if sbar.get_orientation() == orientation:
elif sbar.get_orientation() == orientation:
v_min, v_max = sbar.get_minmax()
delta = v_max - v_min
new_value = int(min(v_min + delta * float(value), v_max))
Expand Down Expand Up @@ -967,7 +967,7 @@ def scroll_to_rect(
for sbar in self._scrollbars:
if not sbar.is_visible():
continue
if sbar.get_orientation() == ORIENTATION_HORIZONTAL and self.get_hidden_width():
elif sbar.get_orientation() == ORIENTATION_HORIZONTAL and self.get_hidden_width():
shortest_move = min(real_rect.left - view_rect.left, real_rect.right - view_rect.right, key=abs)
value = min(sbar.get_maximum(), sbar.get_value() + shortest_move)
value = max(sbar.get_minimum(), value)
Expand Down Expand Up @@ -1218,7 +1218,7 @@ def update(self, events: EventVectorType) -> bool:
for sbar in self._scrollbars:
if not sbar.is_visible():
continue
if self.get_hidden_width() and not updated[0] and sbar.get_orientation() == ORIENTATION_HORIZONTAL:
elif self.get_hidden_width() and not updated[0] and sbar.get_orientation() == ORIENTATION_HORIZONTAL:
updated[0] = sbar.update(events)
elif self.get_hidden_height() and not updated[1] and sbar.get_orientation() == ORIENTATION_VERTICAL:
updated[1] = sbar.update(events)
Expand Down
3 changes: 1 addition & 2 deletions pygame_menu/baseimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,7 @@ def _get_position_delta(self) -> Tuple2IntType:
return rect.midbottom
elif self._drawing_position == POSITION_SOUTHEAST:
return rect.bottomright
else:
raise ValueError('unknown drawing position')
raise ValueError(f'unknown drawing position "{self._drawing_position}"')

def draw(
self,
Expand Down
2 changes: 1 addition & 1 deletion pygame_menu/examples/other/maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ def _prim(self, mazearray: Optional[_MazeType] = None, start_point: Optional[_Po
for wall_neighbour, ntype in wall_neighbours:
if wall_neighbour == (start_point or self._end_point):
continue
if mazearray[wall_neighbour[0]][wall_neighbour[1]].nodetype != 'wall':
elif mazearray[wall_neighbour[0]][wall_neighbour[1]].nodetype != 'wall':
pcount += 1
else:
neighbouring_walls.add(wall_neighbour)
Expand Down
16 changes: 7 additions & 9 deletions pygame_menu/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,8 +1981,8 @@ def _render(self) -> bool:
"""
if not self._render_enabled:
return False # Modify using Menu.disable_render() and Menu.enable_render()
t0 = time.time()
changed = False
t0: float = time.time()
changed: bool = False

if self._widgets_surface_need_update:
self._widgets_surface = None
Expand Down Expand Up @@ -2370,8 +2370,7 @@ def _default() -> bool:
_, _, i = first_widget.get_col_row_index()
return self._select(i, pos, SELECT_KEY, apply_sound)

else:
return _default()
return _default()

def _handle_joy_event(self, apply_sound: bool = False) -> bool:
"""
Expand Down Expand Up @@ -2717,8 +2716,8 @@ def update(self, events: EventVectorType) -> bool:
widget = self._current._widgets[index]
if isinstance(widget, Frame): # Frame does not accept click
continue
if (widget.is_selectable and widget.is_visible() and
widget.get_scrollarea().collide(widget, event)):
elif (widget.is_selectable and widget.is_visible() and
widget.get_scrollarea().collide(widget, event)):
sel = self._current._select(index, 1, SELECT_MOUSE_BUTTON_DOWN, True)
break

Expand Down Expand Up @@ -3344,7 +3343,7 @@ def _select(
return False

# If new widget is not selectable or visible
if not new_widget.is_selectable or not new_widget.is_visible():
elif not new_widget.is_selectable or not new_widget.is_visible():

# If it is a frame, select the first selectable object
if isinstance(new_widget, Frame):
Expand Down Expand Up @@ -3373,8 +3372,7 @@ def _select(
apply_sound, **kwargs)

# No selectable options, quit
else:
return False
return False

# Selecting widgets forces rendering
old_widget.select(False)
Expand Down
2 changes: 1 addition & 1 deletion pygame_menu/themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ def _format_color_opacity(
"""
if isinstance(color, BaseImage):
return color
if color is None and none:
elif color is None and none:
return color
color = format_color(color)
if isinstance(color, VectorInstance):
Expand Down
3 changes: 1 addition & 2 deletions pygame_menu/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ def parse_padding(padding: PaddingType) -> Tuple4IntType:
return int(padding[0]), int(padding[1]), int(padding[0]), int(padding[1])
elif len(padding) == 3:
return int(padding[0]), int(padding[1]), int(padding[2]), int(padding[1])
else:
return int(padding[0]), int(padding[1]), int(padding[2]), int(padding[3])
return int(padding[0]), int(padding[1]), int(padding[2]), int(padding[3])


def print_menu_widget_structure(
Expand Down
22 changes: 11 additions & 11 deletions pygame_menu/widgets/core/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,9 @@ def _check_mouseover(
if event.type == pygame.ACTIVEEVENT and hasattr(event, 'gain'):
if event.gain == 1:
return False
else: # Mouse out from window
check_widget_mouseleave(force=True)
return True
# Mouse is outside window
check_widget_mouseleave(force=True)
return True

if rect is None:
rect = self._mouseover_check_rect()
Expand Down Expand Up @@ -770,14 +770,14 @@ def is_visible(self, check_frame: bool = True) -> bool:
"""
if not check_frame:
return self._visible
if not self._visible:
elif not self._visible:
return False
frame = self._frame
if frame is not None:
while True:
if frame is None:
break
if not frame._visible:
elif not frame._visible:
return False
frame = frame._frame
return True
Expand Down Expand Up @@ -1047,7 +1047,7 @@ def _draw_background_color(

if bg is None:
return
if rect is None:
elif rect is None:
rect = self.get_rect(inflate=self._get_background_inflate())

# Create the background surface if none, if the rect changed, or if the background color changed
Expand Down Expand Up @@ -1256,7 +1256,7 @@ def apply(self, *args) -> Any:
self.scroll_to_widget(scroll_parent=False)
if self.readonly:
return
if self._onreturn:
elif self._onreturn:
args = list(args) + list(self._args)
try:
args.insert(0, self.get_value())
Expand Down Expand Up @@ -1290,7 +1290,7 @@ def change(self, *args) -> Any:
self.scroll_to_widget(scroll_parent=False)
if self.readonly:
return val
if self._onchange:
elif self._onchange:
args = list(args) + list(self._args)
try:
args.insert(0, self.get_value())
Expand Down Expand Up @@ -1506,7 +1506,7 @@ def scroll_to_widget(self, margin: Tuple2NumberType = (0, 0), scroll_parent: boo
"""
if self.has_attribute('ignore_scroll_to_widget'):
return self
if self._frame is not None and self._frame.is_scrollable and self._frame.get_scrollarea() is not None:
elif self._frame is not None and self._frame.is_scrollable and self._frame.get_scrollarea() is not None:
self._frame.get_scrollarea().scroll_to_rect(self.get_frame().get_rect(), margin, scroll_parent)
if self._scrollarea is not None:
rect = self.get_rect()
Expand Down Expand Up @@ -1771,7 +1771,7 @@ def get_font_color_status(self, check_selection: bool = True) -> ColorType:
if self._selected:
return self._font_readonly_selected_color
return self._font_readonly_color
if self._selected and check_selection and self._selection_effect.widget_apply_font_color:
elif self._selected and check_selection and self._selection_effect.widget_apply_font_color:
return self._font_selected_color
return self._font_color

Expand Down Expand Up @@ -2140,7 +2140,7 @@ def _scale_warn(
"""
if not self._verbose:
return
if self._scale[0] and scale:
elif self._scale[0] and scale:
warn('widget already has a scaling factor applied. Scaling has '
'been disabled')
if self._max_width[0] is not None and maxwidth:
Expand Down
10 changes: 5 additions & 5 deletions pygame_menu/widgets/widget/colorinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def update(self, events: EventVectorType) -> bool:
if self._ignores_keyboard_nonphysical() and not check_key_pressed_valid(event):
continue

if disable_remove_separator and len(input_str) > 0 and len(input_str) > cursor_pos and (
elif disable_remove_separator and len(input_str) > 0 and len(input_str) > cursor_pos and (
f'{self._separator}{self._separator}' not in input_str or
input_str[cursor_pos] == self._separator and len(input_str) == cursor_pos + 1
):
Expand Down Expand Up @@ -478,9 +478,9 @@ def update(self, events: EventVectorType) -> bool:
if int(num) > 255: # Number exceeds 25X
return False
# User adds 0 at left, example: 12 -> 012
if num != str(int(num)) and key == '0':
elif num != str(int(num)) and key == '0':
return False
if len(num) > 3: # Number like 0XXX
elif len(num) > 3: # Number like 0XXX
return False

elif self._color_type == COLORINPUT_TYPE_HEX:
Expand All @@ -494,7 +494,7 @@ def update(self, events: EventVectorType) -> bool:
continue

# Backspace button, delete text from right
if self._ctrl.back(event, self):
elif self._ctrl.back(event, self):
if cursor_pos == 1:
return True

Expand All @@ -509,7 +509,7 @@ def update(self, events: EventVectorType) -> bool:
if key in self._valid_chars:
if key == '#':
return True
if cursor_pos == 0:
elif cursor_pos == 0:
return True

# Update
Expand Down
14 changes: 7 additions & 7 deletions pygame_menu/widgets/widget/dropselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,11 +873,11 @@ def _down(self) -> None:
"""
if self.readonly:
return
if len(self._items) == 0:
elif len(self._items) == 0:
return
if not self.active:
elif not self.active:
return self._toggle_drop()
if self._index == -1:
elif self._index == -1:
self.set_value(len(self._items) - 1)
else:
if self._selection_infinite:
Expand All @@ -897,11 +897,11 @@ def _up(self) -> None:
"""
if self.readonly:
return
if len(self._items) == 0:
elif len(self._items) == 0:
return
if not self.active:
elif not self.active:
return self._toggle_drop()
if self._index == -1:
elif self._index == -1:
self.set_value(0)
else:
if self._selection_infinite:
Expand Down Expand Up @@ -1016,7 +1016,7 @@ def _toggle_drop(self) -> None:
self._check_drop_made()
if not self._selected:
return
if len(self._items) == 0:
elif len(self._items) == 0:
return
self.active = not self.active
if self.active:
Expand Down
7 changes: 3 additions & 4 deletions pygame_menu/widgets/widget/dropselect_multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def _get_current_selected_text(self) -> str:
return self._placeholder

# Apply selected format
if self._selection_placeholder_format == DROPSELECT_MULTIPLE_SFORMAT_TOTAL:
elif self._selection_placeholder_format == DROPSELECT_MULTIPLE_SFORMAT_TOTAL:
return self._placeholder_selected.format(len(self._selected_indices))

list_items = self._get_selected_items_list_str()
Expand All @@ -337,8 +337,7 @@ def _get_current_selected_text(self) -> str:
f'string (List[str]=>str), not {type(o)} type ({o} returned)'
return self._placeholder_selected.format(o)

else:
raise ValueError('invalid selection placeholder format type')
raise ValueError('invalid selection placeholder format type')

def _get_selected_items_list_str(self) -> List[str]:
"""
Expand Down Expand Up @@ -406,7 +405,7 @@ def _process_index(self) -> None:
"""
if self._index == -1:
return
if self._index in self._selected_indices:
elif self._index in self._selected_indices:
self._selected_indices.remove(self._index)
else:
if self._max_selected == 0 or len(self._selected_indices) < self._max_selected:
Expand Down
Loading

0 comments on commit c2b7590

Please sign in to comment.