Skip to content

Commit

Permalink
Added __slots__ to all Python interface classes
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavye Mathur <bhavyemathur@gmail.com>
  • Loading branch information
BhavyeMathur committed Jan 15, 2024
1 parent dd44976 commit 31ecf73
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 7 deletions.
Binary file modified binaries/lib-macos/libgoopylib.dylib
Binary file not shown.
4 changes: 3 additions & 1 deletion goopylib/color/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import Union
from typing import Union, NoReturn

# pylint: disable-next=no-name-in-module, import-error
import goopylib.ext.color as _color
Expand Down Expand Up @@ -36,6 +36,8 @@ class Color:
Color(60, 180, 90)
"""

__slots__ = ["_color"]

def __init__(self, *args) -> None:
"""
Create colors by passing RGB arguments or a hexstring.
Expand Down
2 changes: 2 additions & 0 deletions goopylib/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Window:
title: displayed in the title bar
"""

__slots__ = ["_window", "_background", "_camera"]

def __init__(self, width: int, height: int, title: str = "goopylib Window") -> None:
"""
Creates a window that can be used to draw graphics and widgets.
Expand Down
6 changes: 6 additions & 0 deletions goopylib/layout/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@


class _LRTB:

__slots__ = ["_left", "_right", "_top", "_bottom", "_x", "_y"]

def __init__(self, left: int, right: int, top: int, bottom: int):
self._left = left
self._right = right
Expand Down Expand Up @@ -232,6 +235,9 @@ def translate(self, dx: int, dy: int) -> None:


class _Dimension(int):

__slots__ = ["_dimension", "_unit"]

def __new__(cls, value: Union[int, str]) -> int:
x = int.__new__(cls, _Dimension._parse_dimension(value)[0])
return x
Expand Down
3 changes: 3 additions & 0 deletions goopylib/layout/div.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Div:
_context_tree: List[Div] = []
_instances: List[Div] = [] # could consider making a dictionary

__slots__ = ["_width", "_height", "_min_width", "_min_height", "_max_width", "_max_height", "_margin", "_padding",
"_flex", "_children", "_parent", "_classes", "_layer", "_margin_box", "_padding_box", "_content_box"]

def __init__(self,
width: Union[int, str],
height: Union[int, str],
Expand Down
3 changes: 3 additions & 0 deletions goopylib/layout/flex.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Flex:
A class storing a container's flex properties
"""

__slots__ = ["_wrap", "_align", "_cross_align", "_item_align", "_row_gap", "_column_gap", "_direction", "_order",
"_grow", "_cross_align_self"]

def __init__(self,
wrap: _FLEX_WRAP_TYPE = "nowrap",
align: _FLEX_ALIGN_TYPE = "start",
Expand Down
2 changes: 2 additions & 0 deletions goopylib/maths/easing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
class EasingType:
"""Generic Easing Type"""

__slots__ = []

def __call__(self, t: float) -> float:
"""
Raises:
Expand Down
6 changes: 6 additions & 0 deletions goopylib/maths/packing/packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Item:
Class representing a rectangular object to be packed.
"""

__slots__ = ["_width", "_height", "_long_side", "_short_side", "_x", "_y", "_rotated", "_id"]

items: int = 0

def __init__(self, width: float, height: float) -> None:
Expand Down Expand Up @@ -168,6 +170,8 @@ class Bin:
Class representing the maximum area in which to pack Items
"""

__slots__ = ["_width", "_height", "_items", "_id"]

bins: int = 0

def __init__(self, width: float, height: float) -> None:
Expand Down Expand Up @@ -233,6 +237,8 @@ class PackingAlgorithm:
bin_height: the maximum height of the packed bin
"""

__slots__ = ["_bin_width", "_bin_height", "_bins"]

def __init__(self, bin_width: float, bin_height: float):
"""
A class representing a rectangle packing algorithm.
Expand Down
10 changes: 10 additions & 0 deletions goopylib/maths/packing/shelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Shelf:
bin: the bin associated with the shelf
"""

__slots__ = ["width", "height", "packed_width", "available_width", "vertical_offset", "is_open", "bin", "items"]

def __init__(self, vertical_offset: float, bin: ShelvedBin) -> None:
"""
Class representing a single shelf (row) for shelf-based packing algorithms
Expand Down Expand Up @@ -108,6 +110,8 @@ class ShelvedBin(Bin):
height: of the bin
"""

__slots__ = ["open_shelf", "shelves"]

def __init__(self, width: float, height: float) -> None:
"""
Class representing the maximum area in which to pack Items broken down into shelves
Expand Down Expand Up @@ -238,6 +242,8 @@ class NextFit(ShelfPackingAlgorithm):
bin_height: the maximum height of the packed bin
"""

__slots__ = ["_shelf"]

def __init__(self, bin_width: float, bin_height: float):
"""
A class representing a shelf-based, next-fit packing algorithm.
Expand Down Expand Up @@ -285,6 +291,8 @@ class FirstFit(ShelfPackingAlgorithm):
bin_height: the maximum height of the packed bin
"""

__slots__ = ["_shelf"]

def __init__(self, bin_width: float, bin_height: float):
"""
A class representing a shelf-based, first-fit packing algorithm.
Expand Down Expand Up @@ -344,6 +352,8 @@ class ScoredFit(ShelfPackingAlgorithm):
bin_height: the maximum height of the packed bin
"""

__slots__ = ["_shelf", "_scoring_func"]

def __init__(self, bin_width: float, bin_height: float, scoring_function: Callable[[Shelf, Item], float]):
"""
A class representing a shelf-based, scored-fit packing algorithm.
Expand Down
15 changes: 12 additions & 3 deletions goopylib/objects/renderable/renderable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import Tuple
from typing import Tuple, Union

from goopylib.core.window import Window

Expand All @@ -15,6 +15,8 @@ class Renderable:
The methods provided can be called by any other goopylib objects.
"""

__slots__ = ["_renderable", "_window"]

# pylint: disable-next=super-init-not-called
def __init__(self) -> None:
"""
Expand All @@ -25,7 +27,7 @@ def __init__(self) -> None:
NotImplementedError: cannot directly initialize a Renderable
"""
self._renderable = None
self.window: Window = None
self._window: Union[Window, None] = None

def __repr__(self) -> str:
return self._renderable.__repr__()
Expand All @@ -43,7 +45,7 @@ def draw(self, window: Window) -> Renderable:
"""
if isinstance(window, Window):
self._renderable.draw(window._window)
self.window = window
self._window = window
else:
self._renderable.draw(window)
return self
Expand Down Expand Up @@ -130,6 +132,13 @@ def set_size(self, width: float, height: float) -> None:
"""
return self._renderable.set_size(width, height)

@property
def window(self) -> Union[Window, None]:
"""
The window the object is drawn to (or None)
"""
return self._window

@property
def x(self) -> float:
"""
Expand Down
2 changes: 2 additions & 0 deletions goopylib/scene/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class Camera:
TypeError: arguments must be numbers
"""

__slots__ = ["_camera"]

def __init__(self, left: float, right: float, bottom: float, top: float) -> None:
"""
An orthographic camera associated with a Window.
Expand Down
2 changes: 2 additions & 0 deletions goopylib/scene/camera_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class CameraController:
TypeError: window must be a goopylib Window
"""

__slots__ = ["_window", "_controller"]

def __init__(self, window: Window) -> None:
"""
Controller class that automatically manages camera movement, rotation, and zoom.
Expand Down
2 changes: 1 addition & 1 deletion src/goopylib/core/Core.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define GP_LOGGING_LEVEL 6
#define GP_LOGGING_LEVEL 3

#include "Core.h"
#include "src/goopylib/core/Window.h"
Expand Down
2 changes: 1 addition & 1 deletion src/goopylib/core/VertexArray.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define GP_LOGGING_LEVEL 6
#define GP_LOGGING_LEVEL 3

#include "src/goopylib/core/VertexArray.h"

Expand Down
2 changes: 1 addition & 1 deletion src/goopylib/scene/Renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define GP_LOGGING_LEVEL 6
#define GP_LOGGING_LEVEL 3

#include "Renderer.h"

Expand Down

0 comments on commit 31ecf73

Please sign in to comment.