From 7c05ab36d5ee14c084eeb716aa2c26ffefb2f8a2 Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Sun, 31 Oct 2021 22:07:48 -0400 Subject: [PATCH 1/6] Add typehints --- adafruit_fingerprint.py | 52 +++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/adafruit_fingerprint.py b/adafruit_fingerprint.py index 4e2f54f..acff818 100644 --- a/adafruit_fingerprint.py +++ b/adafruit_fingerprint.py @@ -28,6 +28,8 @@ from micropython import const try: + from typing import Tuple, List, Union + from busio import UART import struct except ImportError: import ustruct as struct @@ -111,7 +113,7 @@ class Adafruit_Fingerprint: system_id = None status_register = None - def __init__(self, uart, passwd=(0, 0, 0, 0)): + def __init__(self, uart: UART, passwd: Tuple[int, int, int, int] = (0, 0, 0, 0)): # Create object with UART for interface, and default 32-bit password self.password = passwd self._uart = uart @@ -120,7 +122,7 @@ def __init__(self, uart, passwd=(0, 0, 0, 0)): if self.read_sysparam() != OK: raise RuntimeError("Failed to read system parameters!") - def check_module(self): + def check_module(self) -> bool: """Checks the state of the fingerprint scanner module. Returns OK or error.""" self._send_packet([_GETECHO]) @@ -128,12 +130,12 @@ def check_module(self): raise RuntimeError("Something is wrong with the sensor.") return True - def verify_password(self): + def verify_password(self) -> bool: """Checks if the password/connection is correct, returns True/False""" self._send_packet([_VERIFYPASSWORD] + list(self.password)) return self._get_packet(12)[0] - def count_templates(self): + def count_templates(self) -> int: """Requests the sensor to count the number of templates and stores it in ``self.template_count``. Returns the packet error code or OK success""" self._send_packet([_TEMPLATECOUNT]) @@ -141,7 +143,7 @@ def count_templates(self): self.template_count = struct.unpack(">H", bytes(r[1:3]))[0] return r[0] - def read_sysparam(self): + def read_sysparam(self) -> int: """Returns the system parameters on success via attributes.""" self._send_packet([_READSYSPARA]) r = self._get_packet(28) @@ -156,7 +158,7 @@ def read_sysparam(self): self.baudrate = struct.unpack(">H", bytes(r[15:17]))[0] return r[0] - def set_sysparam(self, param_num, param_val): + def set_sysparam(self, param_num: int, param_val: int) -> int: """Set the system parameters (param_num)""" self._send_packet([_SETSYSPARA, param_num, param_val]) r = self._get_packet(12) @@ -170,43 +172,43 @@ def set_sysparam(self, param_num, param_val): self.data_packet_size = param_val return r[0] - def get_image(self): + def get_image(self) -> int: """Requests the sensor to take an image and store it memory, returns the packet error code or OK success""" self._send_packet([_GETIMAGE]) return self._get_packet(12)[0] - def image_2_tz(self, slot=1): + def image_2_tz(self, slot: int = 1) -> int: """Requests the sensor convert the image to a template, returns the packet error code or OK success""" self._send_packet([_IMAGE2TZ, slot]) return self._get_packet(12)[0] - def create_model(self): + def create_model(self) -> int: """Requests the sensor take the template data and turn it into a model returns the packet error code or OK success""" self._send_packet([_REGMODEL]) return self._get_packet(12)[0] - def store_model(self, location, slot=1): + def store_model(self, location: int, slot: int = 1) -> int: """Requests the sensor store the model into flash memory and assign a location. Returns the packet error code or OK success""" self._send_packet([_STORE, slot, location >> 8, location & 0xFF]) return self._get_packet(12)[0] - def delete_model(self, location): + def delete_model(self, location: int) -> int: """Requests the sensor delete a model from flash memory given by the argument location. Returns the packet error code or OK success""" self._send_packet([_DELETE, location >> 8, location & 0xFF, 0x00, 0x01]) return self._get_packet(12)[0] - def load_model(self, location, slot=1): + def load_model(self, location: int, slot: int = 1) -> int: """Requests the sensor to load a model from the given memory location to the given slot. Returns the packet error code or success""" self._send_packet([_LOAD, slot, location >> 8, location & 0xFF]) return self._get_packet(12)[0] - def get_fpdata(self, sensorbuffer="char", slot=1): + def get_fpdata(self, sensorbuffer: str = "char", slot: int = 1) -> List[int]: """Requests the sensor to transfer the fingerprint image or template. Returns the data payload only.""" if slot not in (1, 2): @@ -224,7 +226,7 @@ def get_fpdata(self, sensorbuffer="char", slot=1): self._print_debug("get_fdata res:", res, data_type="hex") return res - def send_fpdata(self, data, sensorbuffer="char", slot=1): + def send_fpdata(self, data: List[int], sensorbuffer: str = "char", slot: int = 1) -> bool: """Requests the sensor to receive data, either a fingerprint image or a character/template data. Data is the payload only.""" if slot not in (1, 2): @@ -242,13 +244,13 @@ def send_fpdata(self, data, sensorbuffer="char", slot=1): self._print_debug("sent_fdata data:", data, data_type="hex") return True - def empty_library(self): + def empty_library(self) -> int: """Requests the sensor to delete all models from flash memory. Returns the packet error code or OK success""" self._send_packet([_EMPTY]) return self._get_packet(12)[0] - def read_templates(self): + def read_templates(self) -> int: """Requests the sensor to list of all template locations in use and stores them in self.templates. Returns the packet error code or OK success""" @@ -273,7 +275,7 @@ def read_templates(self): r = temp_r return r[0] - def finger_fast_search(self): + def finger_fast_search(self) -> int: """Asks the sensor to search for a matching fingerprint template to the last model generated. Stores the location and confidence in self.finger_id and self.confidence. Returns the packet error code or OK success""" @@ -296,7 +298,7 @@ def close_uart(self): """close serial port""" self._uart.close() - def finger_search(self): + def finger_search(self) -> int: """Asks the sensor to search for a matching fingerprint starting at slot 1. Stores the location and confidence in self.finger_id and self.confidence. Returns the packet error code or OK success""" @@ -310,7 +312,7 @@ def finger_search(self): self._print_debug("finger_search packet:", r, data_type="hex") return r[0] - def compare_templates(self): + def compare_templates(self) -> int: """Compares two fingerprint templates in char buffers 1 and 2. Stores the confidence score in self.finger_id and self.confidence. Returns the packet error code or OK success""" @@ -320,7 +322,7 @@ def compare_templates(self): self._print_debug("compare_templates confidence:", self.confidence) return r[0] - def set_led(self, color=1, mode=3, speed=0x80, cycles=0): + def set_led(self, color: int = 1, mode: int = 3, speed: int = 0x80, cycles: int = 0) -> int: """LED function -- only for R503 Sensor. Parameters: See User Manual for full details color: 1=red, 2=blue, 3=purple @@ -333,7 +335,7 @@ def set_led(self, color=1, mode=3, speed=0x80, cycles=0): ################################################## - def _get_packet(self, expected): + def _get_packet(self, expected: int) -> List[int]: """Helper to parse out a packet from the UART and check structure. Returns just the data payload from the packet""" res = self._uart.read(expected) @@ -366,7 +368,7 @@ def _get_packet(self, expected): self._print_debug("_get_packet reply:", reply, data_type="hex") return reply - def _get_data(self, expected): + def _get_data(self, expected: int) -> List[int]: """Gets packet from serial and checks structure for _DATAPACKET and _ENDDATAPACKET. Alternate method for getting data such as fingerprint image, etc. Returns the data payload.""" @@ -415,7 +417,7 @@ def _get_data(self, expected): self._print_debug("_get_data reply:", reply, data_type="hex") return reply - def _send_packet(self, data): + def _send_packet(self, data: List[int]): packet = [_STARTCODE >> 8, _STARTCODE & 0xFF] packet = packet + self.address packet.append(_COMMANDPACKET) # the packet type @@ -434,7 +436,7 @@ def _send_packet(self, data): self._print_debug("_send_packet data:", packet, data_type="hex") self._uart.write(bytearray(packet)) - def _send_data(self, data): + def _send_data(self, data: List[int]): self._print_debug("_send_data length:", len(data)) self._print_debug("_send_data data:", data, data_type="hex") # self.read_sysparam() #moved this to init @@ -489,7 +491,7 @@ def soft_reset(self): if self._uart.read(1)[0] != MODULEOK: raise RuntimeError("Sensor did not send a handshake signal!") - def _print_debug(self, info, data, data_type="str"): + def _print_debug(self, info: str, data: Union[int, str], data_type: str = "str"): """Prints debugging information. This is activated by flag _debug""" if not self._debug: From 0b6bf26ac8a4fea27fe1e91546bb8c820e141113 Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Sun, 31 Oct 2021 22:08:22 -0400 Subject: [PATCH 2/6] File reformatting per pre-commit --- adafruit_fingerprint.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/adafruit_fingerprint.py b/adafruit_fingerprint.py index acff818..4f87f7d 100644 --- a/adafruit_fingerprint.py +++ b/adafruit_fingerprint.py @@ -226,7 +226,9 @@ def get_fpdata(self, sensorbuffer: str = "char", slot: int = 1) -> List[int]: self._print_debug("get_fdata res:", res, data_type="hex") return res - def send_fpdata(self, data: List[int], sensorbuffer: str = "char", slot: int = 1) -> bool: + def send_fpdata( + self, data: List[int], sensorbuffer: str = "char", slot: int = 1 + ) -> bool: """Requests the sensor to receive data, either a fingerprint image or a character/template data. Data is the payload only.""" if slot not in (1, 2): @@ -322,7 +324,9 @@ def compare_templates(self) -> int: self._print_debug("compare_templates confidence:", self.confidence) return r[0] - def set_led(self, color: int = 1, mode: int = 3, speed: int = 0x80, cycles: int = 0) -> int: + def set_led( + self, color: int = 1, mode: int = 3, speed: int = 0x80, cycles: int = 0 + ) -> int: """LED function -- only for R503 Sensor. Parameters: See User Manual for full details color: 1=red, 2=blue, 3=purple From 2492f8f891b4d35ce2b8719fcd8303fa80da4ca6 Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Sun, 31 Oct 2021 22:14:27 -0400 Subject: [PATCH 3/6] Disabled f-string suggestion for adafruit_fingerprint Was causing the pylint failure for the file --- adafruit_fingerprint.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adafruit_fingerprint.py b/adafruit_fingerprint.py index 4f87f7d..01463e8 100644 --- a/adafruit_fingerprint.py +++ b/adafruit_fingerprint.py @@ -25,6 +25,8 @@ https://github.com/adafruit/circuitpython/releases """ +# pylint: disable=consider-using-f-string + from micropython import const try: From 17dd7269bde1f031ac8a4886714d55a49583ad37 Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Sun, 31 Oct 2021 22:23:28 -0400 Subject: [PATCH 4/6] Move disabling comment to .pylintrc Guess that doesn't go there! --- .pylintrc | 2 +- adafruit_fingerprint.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.pylintrc b/.pylintrc index 0238b90..37f86ef 100644 --- a/.pylintrc +++ b/.pylintrc @@ -55,7 +55,7 @@ confidence= # no Warning level messages displayed, use"--disable=all --enable=classes # --disable=W" # disable=import-error,print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call -disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation +disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation,consider-using-f-string # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option diff --git a/adafruit_fingerprint.py b/adafruit_fingerprint.py index 01463e8..4f87f7d 100644 --- a/adafruit_fingerprint.py +++ b/adafruit_fingerprint.py @@ -25,8 +25,6 @@ https://github.com/adafruit/circuitpython/releases """ -# pylint: disable=consider-using-f-string - from micropython import const try: From 33ba2f6a2e9e2e92a6df84cc2f9186a3b21104b1 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 2 Nov 2021 12:15:13 -0400 Subject: [PATCH 5/6] Revert "Move disabling comment to .pylintrc" This reverts commit 17dd7269bde1f031ac8a4886714d55a49583ad37. --- .pylintrc | 2 +- adafruit_fingerprint.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.pylintrc b/.pylintrc index 37f86ef..0238b90 100644 --- a/.pylintrc +++ b/.pylintrc @@ -55,7 +55,7 @@ confidence= # no Warning level messages displayed, use"--disable=all --enable=classes # --disable=W" # disable=import-error,print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call -disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation,consider-using-f-string +disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option diff --git a/adafruit_fingerprint.py b/adafruit_fingerprint.py index 4f87f7d..01463e8 100644 --- a/adafruit_fingerprint.py +++ b/adafruit_fingerprint.py @@ -25,6 +25,8 @@ https://github.com/adafruit/circuitpython/releases """ +# pylint: disable=consider-using-f-string + from micropython import const try: From 223ab9901c779f12f6440084489f1ee95db1c3d5 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 2 Nov 2021 13:45:43 -0400 Subject: [PATCH 6/6] Revert "File reformatting per pre-commit" This reverts commit 0b6bf26ac8a4fea27fe1e91546bb8c820e141113. --- adafruit_fingerprint.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/adafruit_fingerprint.py b/adafruit_fingerprint.py index 01463e8..4f87f7d 100644 --- a/adafruit_fingerprint.py +++ b/adafruit_fingerprint.py @@ -25,8 +25,6 @@ https://github.com/adafruit/circuitpython/releases """ -# pylint: disable=consider-using-f-string - from micropython import const try: