From 25d8e64a0676b78c42a210846b31d0279664e112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Wed, 12 Dec 2018 21:29:59 +0000 Subject: Sprinkle more type annotations throughout the codebase. --- glucometerutils/common.py | 13 ++++++++----- glucometerutils/support/freestyle.py | 14 ++++++++++++++ glucometerutils/support/hiddevice.py | 5 ++++- glucometerutils/support/lifescan.py | 1 + glucometerutils/support/lifescan_binary_protocol.py | 1 + glucometerutils/support/serial.py | 3 ++- 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/glucometerutils/common.py b/glucometerutils/common.py index 1645c8c..8ffa724 100644 --- a/glucometerutils/common.py +++ b/glucometerutils/common.py @@ -12,7 +12,7 @@ import enum import textwrap try: - from typing import Text + from typing import Sequence, Text except: pass @@ -37,6 +37,7 @@ class MeasurementMethod(enum.Enum): def convert_glucose_unit(value, from_unit, to_unit): + # type: (float, Unit, Unit) -> float """Convert the given value of glucose level between units. Args: @@ -72,6 +73,7 @@ class GlucoseReading: MeasurementMethod)) # type: MeasurementMethod def get_value_as(self, to_unit): + # type: (Unit) -> float """Returns the reading value as the given unit. Args: @@ -80,6 +82,7 @@ class GlucoseReading: return convert_glucose_unit(self.value, Unit.MG_DL, to_unit) def as_csv(self, unit): + # type: (Unit) -> Text """Returns the reading as a formatted comma-separated value string.""" return '"%s","%.2f","%s","%s","%s"' % ( self.timestamp, self.get_value_as(unit), self.meal.value, @@ -112,11 +115,11 @@ class MeterInfo: the device. It can include hardware and software version. native_unit: One of the Unit values to identify the meter native unit. """ - model = attr.ib() - serial_number = attr.ib(default='N/A') - version_info = attr.ib(default=()) + model = attr.ib() # Text + serial_number = attr.ib(default='N/A') # Text + version_info = attr.ib(default=()) # Sequence[Text] native_unit = attr.ib( - default=Unit.MG_DL, validator=attr.validators.in_(Unit)) + default=Unit.MG_DL, validator=attr.validators.in_(Unit)) # Unit def __str__(self): version_information_string = 'N/A' diff --git a/glucometerutils/support/freestyle.py b/glucometerutils/support/freestyle.py index 3770b04..5822d18 100644 --- a/glucometerutils/support/freestyle.py +++ b/glucometerutils/support/freestyle.py @@ -16,6 +16,11 @@ import datetime import logging import re +try: + from typing import Iterator, List, Text, Tuple +except: + pass + import construct from glucometerutils import exceptions @@ -44,6 +49,7 @@ _MULTIRECORDS_FORMAT = re.compile( def _verify_checksum(message, expected_checksum_hex): + # type: (Text, Text) -> None """Calculate the simple checksum of the message and compare with expected. Args: @@ -94,6 +100,7 @@ class FreeStyleHidDevice(hiddevice.HidDevice): pass def _send_command(self, message_type, command): + # type: (int, bytes) -> None """Send a raw command to the device. Args: @@ -108,6 +115,7 @@ class FreeStyleHidDevice(hiddevice.HidDevice): self._write(usb_packet) def _read_response(self): + # type: () -> Tuple[int, bytes] """Read the response from the device and extracts it.""" usb_packet = self._read() @@ -128,6 +136,7 @@ class FreeStyleHidDevice(hiddevice.HidDevice): return (message_type, bytes(message_content)) def _send_text_command(self, command): + # type: (bytes) -> Text """Send a command to the device that expects a text reply.""" self._send_command(self.TEXT_CMD, command) @@ -162,14 +171,17 @@ class FreeStyleHidDevice(hiddevice.HidDevice): # protocol, but not many. Only provide here those that do seep to change # between them. def _get_version(self): + # type: () -> Text """Return the software version of the device.""" return self._send_text_command(b'$swver?').rstrip('\r\n') def get_serial_number(self): + # type: () -> Text """Returns the serial number of the device.""" return self._send_text_command(b'$serlnum?').rstrip('\r\n') def get_datetime(self): + # type: () -> datetime.datetime """Gets the date and time as reported by the device. This is one of the few commands that appear common to many of the @@ -185,6 +197,7 @@ class FreeStyleHidDevice(hiddevice.HidDevice): return datetime.datetime(year + 2000, month, day, hour, minute) def set_datetime(self, date=datetime.datetime.now()): + # type: (datetime.datetime) -> datetime.datetime """Sets the date and time of the device.""" # The format used by the FreeStyle devices is not composable based on @@ -205,6 +218,7 @@ class FreeStyleHidDevice(hiddevice.HidDevice): raise NotImplementedError def _get_multirecord(self, command): + # type: (bytes) -> Iterator[List[Text]] """Queries for, and returns, "multirecords" results. Multirecords are used for querying events, readings, history and similar diff --git a/glucometerutils/support/hiddevice.py b/glucometerutils/support/hiddevice.py index 07596c2..585882e 100644 --- a/glucometerutils/support/hiddevice.py +++ b/glucometerutils/support/hiddevice.py @@ -10,7 +10,7 @@ import logging import os try: - from typing import Optional + from typing import Optional, Text except: pass @@ -48,6 +48,7 @@ class HidDevice(object): TIMEOUT_MS = None # type: Optional[int] def __init__(self, device): + # type: (Optional[Text]) -> None if None in (self.USB_VENDOR_ID, self.USB_PRODUCT_ID) and not device: raise exceptions.CommandLineError( '--device parameter is required, should point to a /dev/hidraw ' @@ -79,12 +80,14 @@ class HidDevice(object): message='Unable to connect to meter: %s.' % e) def _write(self, report): + # type: (bytes) -> None """Writes a report to the HID handle.""" if self.handle_.write(report) < 0: raise exceptions.CommandError() def _read(self, size=64): + # type: (int) -> bytes """Read a report from the HID handle. This is important as it handles the one incompatible interface between diff --git a/glucometerutils/support/lifescan.py b/glucometerutils/support/lifescan.py index 353db4d..4df0b8f 100644 --- a/glucometerutils/support/lifescan.py +++ b/glucometerutils/support/lifescan.py @@ -28,6 +28,7 @@ class MalformedCommand(exceptions.InvalidResponse): def crc_ccitt(data): + # type: (bytes) -> int """Calculate the CRC-16-CCITT with LifeScan's common seed. Args: diff --git a/glucometerutils/support/lifescan_binary_protocol.py b/glucometerutils/support/lifescan_binary_protocol.py index 610b5ea..8b37726 100644 --- a/glucometerutils/support/lifescan_binary_protocol.py +++ b/glucometerutils/support/lifescan_binary_protocol.py @@ -28,6 +28,7 @@ _LINK_CONTROL = construct.BitStruct( ) def LifeScanPacket(include_link_control): + # type: (bool) -> construct.Struct if include_link_control: link_control_construct = _LINK_CONTROL else: diff --git a/glucometerutils/support/serial.py b/glucometerutils/support/serial.py index 9d7694d..c46d819 100644 --- a/glucometerutils/support/serial.py +++ b/glucometerutils/support/serial.py @@ -10,7 +10,7 @@ __license__ = 'MIT' import logging try: - from typing import Text + from typing import Optional, Text except: pass @@ -51,6 +51,7 @@ class SerialDevice(object): TIMEOUT = 1 # type: float def __init__(self, device): + # type: (Optional[Text]) -> None assert self.BAUDRATE is not None if not device and self.DEFAULT_CABLE_ID: -- cgit v1.2.3