From 3be9661b16b31e13bf974fde9bd67c1ec28be7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Thu, 28 Dec 2017 12:15:38 +0000 Subject: Use Python3 Enum class for measurement methods. This makes the code more idiomatic and less brittle. --- glucometerutils/common.py | 13 +++++++------ glucometerutils/drivers/fslibre.py | 8 ++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/glucometerutils/common.py b/glucometerutils/common.py index 4c6e115..31584b4 100644 --- a/glucometerutils/common.py +++ b/glucometerutils/common.py @@ -23,8 +23,9 @@ class Meal(enum.Enum): AFTER = 'After Meal' # Constants for measure method -BLOOD_SAMPLE = 'blood sample' -CGM = 'CGM' # Continuous Glucose Monitoring +class MeasurementMethod(enum.Enum): + BLOOD_SAMPLE = 'blood sample' + CGM = 'CGM' # Continuous Glucose Monitoring def convert_glucose_unit(value, from_unit, to_unit=None): @@ -54,7 +55,7 @@ _ReadingBase = collections.namedtuple( class GlucoseReading(_ReadingBase): def __new__(cls, timestamp, value, meal=Meal.NONE, comment='', - measure_method=BLOOD_SAMPLE): + measure_method=MeasurementMethod.BLOOD_SAMPLE): """Constructor for the glucose reading object. Args: @@ -87,7 +88,7 @@ class GlucoseReading(_ReadingBase): """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, - self.measure_method, self.comment) + self.measure_method.value, self.comment) class KetoneReading(_ReadingBase): def __new__(cls, timestamp, value, comment='', **kwargs): @@ -104,7 +105,7 @@ class KetoneReading(_ReadingBase): """ return super(KetoneReading, cls).__new__( cls, timestamp=timestamp, value=value, comment=comment, - measure_method=BLOOD_SAMPLE) + measure_method=MeasurementMethod.BLOOD_SAMPLE) def get_value_as(self, *args): """Returns the reading value in mmol/L.""" @@ -113,7 +114,7 @@ class KetoneReading(_ReadingBase): def as_csv(self, unit): """Returns the reading as a formatted comma-separated value string.""" return '"%s","%.2f","%s","%s"' % ( - self.timestamp, self.get_value_as(unit), self.measure_method, + self.timestamp, self.get_value_as(unit), self.measure_method.value, self.comment) diff --git a/glucometerutils/drivers/fslibre.py b/glucometerutils/drivers/fslibre.py index 52632b3..6e3988c 100644 --- a/glucometerutils/drivers/fslibre.py +++ b/glucometerutils/drivers/fslibre.py @@ -126,17 +126,17 @@ def _parse_arresult(record): if parsed_record['reading-type'] == 2: comment_parts.append('(Scan)') - measure_method = common.CGM + measure_method = common.MeasurementMethod.CGM cls = common.GlucoseReading value = parsed_record['value'] elif parsed_record['reading-type'] == 0: comment_parts.append('(Blood)') - measure_method = common.BLOOD_SAMPLE + measure_method = common.MeasurementMethod.BLOOD_SAMPLE cls = common.GlucoseReading value = parsed_record['value'] elif parsed_record['reading-type'] == 1: comment_parts.append('(Ketone)') - measure_method = common.BLOOD_SAMPLE + measure_method = common.MeasurementMethod.BLOOD_SAMPLE cls = common.KetoneReading # automatically convert the raw value in mmol/L value = _convert_ketone_unit(parsed_record['value']) @@ -226,7 +226,7 @@ class Device(freestyle.FreeStyleHidDevice): _extract_timestamp(parsed_record), parsed_record['value'], comment='(Sensor)', - measure_method=common.CGM) + measure_method=common.MeasurementMethod.CGM) # Then get the results of explicit scans and blood tests (and other # events). -- cgit v1.2.3