summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiego Elio Pettenò <flameeyes@flameeyes.eu>2017-12-28 13:06:02 +0100
committerDiego Elio Pettenò <flameeyes@flameeyes.eu>2017-12-28 14:12:03 +0100
commitf85da4c81d576b772e594718ad57a316cf544ef3 (patch)
tree4c963940c2da4447e51789feaacb79c442355bb5
parentRaise minimum Python version to 3.4, to allow using the enum module. (diff)
downloadglucometerutils-f85da4c81d576b772e594718ad57a316cf544ef3.tar
glucometerutils-f85da4c81d576b772e594718ad57a316cf544ef3.tar.gz
glucometerutils-f85da4c81d576b772e594718ad57a316cf544ef3.tar.bz2
glucometerutils-f85da4c81d576b772e594718ad57a316cf544ef3.tar.lz
glucometerutils-f85da4c81d576b772e594718ad57a316cf544ef3.tar.xz
glucometerutils-f85da4c81d576b772e594718ad57a316cf544ef3.tar.zst
glucometerutils-f85da4c81d576b772e594718ad57a316cf544ef3.zip
Diffstat (limited to '')
-rwxr-xr-xglucometer.py3
-rw-r--r--glucometerutils/common.py26
-rw-r--r--glucometerutils/drivers/accuchek_reports.py4
-rw-r--r--glucometerutils/drivers/fsinsulinx.py2
-rw-r--r--glucometerutils/drivers/fslibre.py2
-rw-r--r--glucometerutils/drivers/fsoptium.py8
-rw-r--r--glucometerutils/drivers/fsprecisionneo.py2
-rw-r--r--glucometerutils/drivers/otultra2.py8
-rw-r--r--glucometerutils/drivers/otultraeasy.py4
-rw-r--r--glucometerutils/drivers/otverio2015.py4
-rw-r--r--glucometerutils/drivers/sdcodefree.py2
-rw-r--r--test/test_common.py14
12 files changed, 36 insertions, 43 deletions
diff --git a/glucometer.py b/glucometer.py
index 9ec781c..abed647 100755
--- a/glucometer.py
+++ b/glucometer.py
@@ -48,7 +48,8 @@ def main():
parser_dump = subparsers.add_parser(
'dump', help='Dump the readings stored in the device.')
parser_dump.add_argument(
- '--unit', action='store', choices=common.VALID_UNITS,
+ '--unit', action='store',
+ choices=[unit.value for unit in common.Unit],
help='Select the unit to use for the dumped data.')
parser_dump.add_argument(
'--sort-by', action='store', default='timestamp',
diff --git a/glucometerutils/common.py b/glucometerutils/common.py
index 3f61932..9350983 100644
--- a/glucometerutils/common.py
+++ b/glucometerutils/common.py
@@ -7,15 +7,14 @@ __copyright__ = 'Copyright © 2013, Diego Elio Pettenò'
__license__ = 'MIT'
import collections
+import enum
import textwrap
from glucometerutils import exceptions
-# Constants for units
-UNIT_MGDL = 'mg/dL'
-UNIT_MMOLL = 'mmol/L'
-
-VALID_UNITS = [UNIT_MGDL, UNIT_MMOLL]
+class Unit(enum.Enum):
+ MG_DL = 'mg/dL'
+ MMOL_L = 'mmol/L'
# Constants for meal information
NO_MEAL = ''
@@ -41,17 +40,10 @@ def convert_glucose_unit(value, from_unit, to_unit=None):
Raises:
exceptions.InvalidGlucoseUnit: If the parameters are incorrect.
"""
- if from_unit not in VALID_UNITS:
- raise exceptions.InvalidGlucoseUnit(from_unit)
-
if from_unit == to_unit:
return value
- if to_unit is not None:
- if to_unit not in VALID_UNITS:
- raise exceptions.InvalidGlucoseUnit(to_unit)
-
- if from_unit is UNIT_MGDL:
+ if from_unit is Unit.MG_DL:
return round(value / 18.0, 2)
else:
return round(value * 18.0, 0)
@@ -86,9 +78,9 @@ class GlucoseReading(_ReadingBase):
"""Returns the reading value as the given unit.
Args:
- to_unit: (UNIT_MGDL|UNIT_MMOLL) The unit to return the value to.
+ to_unit: (Unit) The unit to return the value to.
"""
- return convert_glucose_unit(self.value, UNIT_MGDL, to_unit)
+ return convert_glucose_unit(self.value, Unit.MG_DL, to_unit)
def as_csv(self, unit):
"""Returns the reading as a formatted comma-separated value string."""
@@ -129,14 +121,14 @@ _MeterInfoBase = collections.namedtuple(
class MeterInfo(_MeterInfoBase):
def __new__(cls, model, serial_number='N/A', version_info=(),
- native_unit=UNIT_MGDL):
+ native_unit=Unit.MG_DL):
"""Construct a meter information object.
Args:
model: (string) Human-readable model name, depending on driver.
serial_number: (string) Optional serial number to identify the device.
version_info: (list(string)) Optional hardware/software version information.
- native_unit: (UNIT_MGDL|UNIT_MMOLL) Native unit of the device for display.
+ native_unit: (Unit) Native unit of the device for display.
"""
return super(MeterInfo, cls).__new__(
cls, model=model, serial_number=serial_number, version_info=version_info,
diff --git a/glucometerutils/drivers/accuchek_reports.py b/glucometerutils/drivers/accuchek_reports.py
index 2ae41ba..03eeddd 100644
--- a/glucometerutils/drivers/accuchek_reports.py
+++ b/glucometerutils/drivers/accuchek_reports.py
@@ -26,8 +26,8 @@ from glucometerutils import common
from glucometerutils import exceptions
_UNIT_MAP = {
- 'mmol/l': common.UNIT_MMOLL,
- 'mg/dl': common.UNIT_MGDL,
+ 'mmol/l': common.Unit.MMOL_L,
+ 'mg/dl': common.Unit.MG_DL,
}
_DATE_CSV_KEY = 'Date'
diff --git a/glucometerutils/drivers/fsinsulinx.py b/glucometerutils/drivers/fsinsulinx.py
index 90abc7e..15bc131 100644
--- a/glucometerutils/drivers/fsinsulinx.py
+++ b/glucometerutils/drivers/fsinsulinx.py
@@ -57,7 +57,7 @@ class Device(freestyle.FreeStyleHidDevice):
def get_glucose_unit(self):
"""Returns the glucose unit of the device."""
- return common.UNIT_MGDL
+ return common.Unit.MG_DL
def get_readings(self):
"""Iterate through the reading records in the device."""
diff --git a/glucometerutils/drivers/fslibre.py b/glucometerutils/drivers/fslibre.py
index a0f0517..52632b3 100644
--- a/glucometerutils/drivers/fslibre.py
+++ b/glucometerutils/drivers/fslibre.py
@@ -209,7 +209,7 @@ class Device(freestyle.FreeStyleHidDevice):
"""Returns the glucose unit of the device."""
# TODO(Flameeyes): figure out how to identify the actual unit on the
# device.
- return common.UNIT_MGDL
+ return common.Unit.MG_DL
def get_readings(self):
diff --git a/glucometerutils/drivers/fsoptium.py b/glucometerutils/drivers/fsoptium.py
index 121d8d6..5c57ae3 100644
--- a/glucometerutils/drivers/fsoptium.py
+++ b/glucometerutils/drivers/fsoptium.py
@@ -119,9 +119,9 @@ class Device(serial.SerialDevice):
elif parsed_line[0] == 'Ver:':
self.device_version_ = parsed_line[1]
if parsed_line[2] == 'MMOL':
- self.device_glucose_unit_ = common.UNIT_MMOLL
+ self.device_glucose_unit_ = common.Unit.MMOL_L
else: # I only have a mmol/l device, so I can't be sure.
- self.device_glucose_unit_ = common.UNIT_MGDL
+ self.device_glucose_unit_ = common.Unit.MG_DL
# There are more entries: Clock, Market, ROM and Usage, but we don't care
# for those here.
elif parsed_line[0] == 'CMD OK':
@@ -164,8 +164,8 @@ class Device(serial.SerialDevice):
"""Returns a constant representing the unit displayed by the meter.
Returns:
- common.UNIT_MGDL: if the glucometer displays in mg/dL
- common.UNIT_MMOLL: if the glucometer displays in mmol/L
+ common.Unit.MG_DL: if the glucometer displays in mg/dL
+ common.Unit.MMOL_L: if the glucometer displays in mmol/L
"""
return self.device_glucose_unit_
diff --git a/glucometerutils/drivers/fsprecisionneo.py b/glucometerutils/drivers/fsprecisionneo.py
index 6fd07c2..cb58263 100644
--- a/glucometerutils/drivers/fsprecisionneo.py
+++ b/glucometerutils/drivers/fsprecisionneo.py
@@ -66,7 +66,7 @@ class Device(freestyle.FreeStyleHidDevice):
def get_glucose_unit(self):
"""Returns the glucose unit of the device."""
- return common.UNIT_MGDL
+ return common.Unit.MG_DL
def get_readings(self):
"""Iterate through the reading records in the device."""
diff --git a/glucometerutils/drivers/otultra2.py b/glucometerutils/drivers/otultra2.py
index e9f60bb..f44bc15 100644
--- a/glucometerutils/drivers/otultra2.py
+++ b/glucometerutils/drivers/otultra2.py
@@ -262,8 +262,8 @@ class Device(serial.SerialDevice):
"""Returns a constant representing the unit displayed by the meter.
Returns:
- common.UNIT_MGDL: if the glucometer displays in mg/dL
- common.UNIT_MMOLL: if the glucometer displays in mmol/L
+ common.Unit.MG_DL: if the glucometer displays in mg/dL
+ common.Unit.MMOL_L: if the glucometer displays in mmol/L
Raises:
exceptions.InvalidGlucoseUnit: if the unit is not recognized
@@ -279,9 +279,9 @@ class Device(serial.SerialDevice):
unit = match.group(1)
if unit == 'MG/DL ':
- return common.UNIT_MGDL
+ return common.Unit.MG_DL
elif unit == 'MMOL/L':
- return common.UNIT_MMOLL
+ return common.Unit.MMOL_L
else:
raise exceptions.InvalidGlucoseUnit(string)
diff --git a/glucometerutils/drivers/otultraeasy.py b/glucometerutils/drivers/otultraeasy.py
index 461de26..0538dac 100644
--- a/glucometerutils/drivers/otultraeasy.py
+++ b/glucometerutils/drivers/otultraeasy.py
@@ -295,9 +295,9 @@ class Device(serial.SerialDevice):
response = self._read_response()
if response.data[2] == 0:
- return common.UNIT_MGDL
+ return common.Unit.MG_DL
elif response.data[2] == 1:
- return common.UNIT_MMOLL
+ return common.Unit.MMOL_L
else:
raise lifescan.MalformedCommand(
'at position PM1 invalid value %02x for unit' % response.data[2])
diff --git a/glucometerutils/drivers/otverio2015.py b/glucometerutils/drivers/otverio2015.py
index 9dc9393..c416678 100644
--- a/glucometerutils/drivers/otverio2015.py
+++ b/glucometerutils/drivers/otverio2015.py
@@ -243,9 +243,9 @@ class Device(object):
def get_glucose_unit(self):
unit_value = self._read_parameter(_PARAMETER_KEY_UNIT)
if unit_value == b'\x00\x00\x00\x00':
- return common.UNIT_MGDL
+ return common.Unit.MG_DL
elif unit_value == b'\x01\x00\x00\x00':
- return common.UNIT_MMOLL
+ return common.Unit.MMOL_L
else:
raise exceptions.InvalidGlucoseUnit('%r' % unit_value)
diff --git a/glucometerutils/drivers/sdcodefree.py b/glucometerutils/drivers/sdcodefree.py
index 8b07ab5..4939361 100644
--- a/glucometerutils/drivers/sdcodefree.py
+++ b/glucometerutils/drivers/sdcodefree.py
@@ -164,7 +164,7 @@ class Device(serial.SerialDevice):
def get_glucose_unit(self):
# Device does not provide information on glucose unit.
- return common.UNIT_MGDL
+ return common.Unit.MG_DL
def get_datetime(self):
raise NotImplementedError
diff --git a/test/test_common.py b/test/test_common.py
index 07b65d9..febf578 100644
--- a/test/test_common.py
+++ b/test/test_common.py
@@ -19,31 +19,31 @@ class TestCommon(unittest.TestCase):
def test_glucose_conversion(self):
self.assertEqual(
5.56, common.convert_glucose_unit(
- 100, common.UNIT_MGDL, common.UNIT_MMOLL))
+ 100, common.Unit.MG_DL, common.Unit.MMOL_L))
self.assertEqual(
5.56, common.convert_glucose_unit(
- 100, common.UNIT_MGDL))
+ 100, common.Unit.MG_DL))
self.assertEqual(
180, common.convert_glucose_unit(
- 10, common.UNIT_MMOLL, common.UNIT_MGDL))
+ 10, common.Unit.MMOL_L, common.Unit.MG_DL))
self.assertEqual(
180, common.convert_glucose_unit(
- 10, common.UNIT_MMOLL))
+ 10, common.Unit.MMOL_L))
self.assertEqual(
100, common.convert_glucose_unit(
- 100, common.UNIT_MGDL, common.UNIT_MGDL))
+ 100, common.Unit.MG_DL, common.Unit.MG_DL))
self.assertEqual(
10, common.convert_glucose_unit(
- 10, common.UNIT_MMOLL, common.UNIT_MMOLL))
+ 10, common.Unit.MMOL_L, common.Unit.MMOL_L))
self.assertRaises(
exceptions.InvalidGlucoseUnit,
- common.convert_glucose_unit, common.UNIT_MMOLL, 'foo')
+ common.convert_glucose_unit, common.Unit.MMOL_L, 'foo')
if __name__ == '__main__':
unittest.main()