From 2c9501b641f1924335833814c1d6455ef7952b66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Thu, 28 Dec 2017 12:10:12 +0000 Subject: Use Python3 Enum class for meal flags. This makes the code more idiomatic and less brittle. --- glucometerutils/common.py | 13 +++++++------ glucometerutils/drivers/accuchek_reports.py | 6 +++--- glucometerutils/drivers/otultra2.py | 6 +++--- glucometerutils/drivers/otverio2015.py | 6 +++--- glucometerutils/drivers/sdcodefree.py | 6 +++--- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/glucometerutils/common.py b/glucometerutils/common.py index 9350983..4c6e115 100644 --- a/glucometerutils/common.py +++ b/glucometerutils/common.py @@ -17,9 +17,10 @@ class Unit(enum.Enum): MMOL_L = 'mmol/L' # Constants for meal information -NO_MEAL = '' -BEFORE_MEAL = 'Before Meal' -AFTER_MEAL = 'After Meal' +class Meal(enum.Enum): + NONE = '' + BEFORE = 'Before Meal' + AFTER = 'After Meal' # Constants for measure method BLOOD_SAMPLE = 'blood sample' @@ -52,7 +53,7 @@ _ReadingBase = collections.namedtuple( '_ReadingBase', ['timestamp', 'value', 'comment', 'measure_method']) class GlucoseReading(_ReadingBase): - def __new__(cls, timestamp, value, meal=NO_MEAL, comment='', + def __new__(cls, timestamp, value, meal=Meal.NONE, comment='', measure_method=BLOOD_SAMPLE): """Constructor for the glucose reading object. @@ -85,8 +86,8 @@ class GlucoseReading(_ReadingBase): def as_csv(self, unit): """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, self.measure_method, - self.comment) + self.timestamp, self.get_value_as(unit), self.meal.value, + self.measure_method, self.comment) class KetoneReading(_ReadingBase): def __new__(cls, timestamp, value, comment='', **kwargs): diff --git a/glucometerutils/drivers/accuchek_reports.py b/glucometerutils/drivers/accuchek_reports.py index 03eeddd..d55ac0f 100644 --- a/glucometerutils/drivers/accuchek_reports.py +++ b/glucometerutils/drivers/accuchek_reports.py @@ -117,11 +117,11 @@ class Device(object): if record[_AFTER_MEAL_CSV_KEY] and record[_BEFORE_MEAL_CSV_KEY]: raise InvalidResponse('Reading cannot be before and after meal.') elif record[_AFTER_MEAL_CSV_KEY]: - return common.AFTER_MEAL + return common.Meal.AFTER elif record[_BEFORE_MEAL_CSV_KEY]: - return common.BEFORE_MEAL + return common.Meal.BEFORE else: - return common.NO_MEAL + return common.Meal.NONE def get_readings(self): for record in self._get_records_reader(): diff --git a/glucometerutils/drivers/otultra2.py b/glucometerutils/drivers/otultra2.py index f44bc15..762991e 100644 --- a/glucometerutils/drivers/otultra2.py +++ b/glucometerutils/drivers/otultra2.py @@ -27,9 +27,9 @@ from glucometerutils.support import serial # The following two hashes are taken directly from LifeScan's documentation _MEAL_CODES = { - 'N': common.NO_MEAL, - 'B': common.BEFORE_MEAL, - 'A': common.AFTER_MEAL, + 'N': common.Meal.NONE, + 'B': common.Meal.BEFORE, + 'A': common.Meal.AFTER, } _COMMENT_CODES = { diff --git a/glucometerutils/drivers/otverio2015.py b/glucometerutils/drivers/otverio2015.py index c416678..5925a58 100644 --- a/glucometerutils/drivers/otverio2015.py +++ b/glucometerutils/drivers/otverio2015.py @@ -76,9 +76,9 @@ _READ_RECORD_REQUEST_SUFFIX = b'\x00' _MEMORY_ERASE_REQUEST = b'\x04\x1a' _MEAL_CODES = { - 0x00: common.NO_MEAL, - 0x01: common.BEFORE_MEAL, - 0x02: common.AFTER_MEAL, + 0x00: common.Meal.NONE, + 0x01: common.Meal.BEFORE, + 0x02: common.Meal.AFTER, } def _extract_message(register): diff --git a/glucometerutils/drivers/sdcodefree.py b/glucometerutils/drivers/sdcodefree.py index 4939361..4a375bd 100644 --- a/glucometerutils/drivers/sdcodefree.py +++ b/glucometerutils/drivers/sdcodefree.py @@ -65,9 +65,9 @@ _ReadingRecord = collections.namedtuple( _STRUCT_READING = struct.Struct('>BBBBBBBHB') _MEAL_FLAG = { - 0x00: common.NO_MEAL, - 0x10: common.BEFORE_MEAL, - 0x20: common.AFTER_MEAL + 0x00: common.Meal.NONE, + 0x10: common.Meal.BEFORE, + 0x20: common.Meal.AFTER, } def parse_reading(msgdata): -- cgit v1.2.3