Skip to content

Commit

Permalink
Merge pull request #3 from tekktrik/feature/add-typing
Browse files Browse the repository at this point in the history
Add Type Hints
  • Loading branch information
kattni authored Nov 12, 2021
2 parents edee62c + 3caa8fe commit 3c88501
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions adafruit_pixelbuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
"""

try:
from typing import Optional, Tuple, Union, Sequence

ColorUnion = Union[int, Tuple[int, int, int], Tuple[int, int, int, int]]
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Pixelbuf.git"

Expand All @@ -37,12 +44,12 @@ class PixelBuf: # pylint: disable=too-many-instance-attributes

def __init__( # pylint: disable=too-many-locals,too-many-arguments
self,
n,
byteorder="BGR",
brightness=1.0,
auto_write=False,
header=None,
trailer=None,
n: int,
byteorder: str = "BGR",
brightness: float = 1.0,
auto_write: bool = False,
header: Optional[bytes] = None,
trailer: Optional[bytes] = None,
):

bpp, byteorder_tuple, has_white, dotstar_mode = self.parse_byteorder(byteorder)
Expand Down Expand Up @@ -94,7 +101,7 @@ def __init__( # pylint: disable=too-many-locals,too-many-arguments
self.auto_write = auto_write

@staticmethod
def parse_byteorder(byteorder):
def parse_byteorder(byteorder: str) -> Tuple[int, str, bool, bool]:
"""
Parse a Byteorder string for validity and determine bpp, byte order, and
dostar brightness bits.
Expand All @@ -106,8 +113,8 @@ def parse_byteorder(byteorder):
W - White
P - PWM (PWM Duty cycle for pixel - dotstars 0 - 1.0)
:param: ~str bpp: bpp string.
:return: ~tuple: bpp, byteorder, has_white, dotstar_mode
:param ~str bpp: bpp string.
:return ~tuple: bpp, byteorder, has_white, dotstar_mode
"""
bpp = len(byteorder)
dotstar_mode = False
Expand Down Expand Up @@ -153,7 +160,7 @@ def brightness(self):
return self._brightness

@brightness.setter
def brightness(self, value):
def brightness(self, value: float):
value = min(max(value, 0.0), 1.0)
change = value - self._brightness
if -0.001 < change < 0.001:
Expand Down Expand Up @@ -196,7 +203,7 @@ def show(self):
"""
return self._transmit(self._post_brightness_buffer)

def fill(self, color):
def fill(self, color: ColorUnion):
"""
Fills the given pixelbuf with the given color.
:param pixelbuf: A pixel object.
Expand All @@ -208,7 +215,7 @@ def fill(self, color):
if self.auto_write:
self.show()

def _parse_color(self, value):
def _parse_color(self, value: ColorUnion) -> Tuple[int, int, int, int]:
r = 0
g = 0
b = 0
Expand Down Expand Up @@ -258,7 +265,7 @@ def _parse_color(self, value):
return (r, g, b, w)

def _set_item(
self, index, r, g, b, w
self, index: int, r: int, g: int, b: int, w: int
): # pylint: disable=too-many-locals,too-many-branches,too-many-arguments
if index < 0:
index += len(self)
Expand Down Expand Up @@ -289,7 +296,9 @@ def _set_item(
b * self._brightness
)

def __setitem__(self, index, val):
def __setitem__(
self, index: Union[int, slice], val: Union[ColorUnion, Sequence[ColorUnion]]
):
if isinstance(index, slice):
start, stop, step = index.indices(self._pixels)
for val_i, in_i in enumerate(range(start, stop, step)):
Expand All @@ -302,7 +311,7 @@ def __setitem__(self, index, val):
if self.auto_write:
self.show()

def _getitem(self, index):
def _getitem(self, index: int):
start = self._offset + (index * self._bpp)
buffer = (
self._pre_brightness_buffer
Expand All @@ -322,7 +331,7 @@ def _getitem(self, index):
)
return value

def __getitem__(self, index):
def __getitem__(self, index: Union[int, slice]):
if isinstance(index, slice):
out = []
for in_i in range(
Expand All @@ -336,5 +345,5 @@ def __getitem__(self, index):
raise IndexError
return self._getitem(index)

def _transmit(self, buffer):
def _transmit(self, buffer: bytearray):
raise NotImplementedError("Must be subclassed")

0 comments on commit 3c88501

Please sign in to comment.