summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiego Elio Pettenò <flameeyes@flameeyes.eu>2018-12-12 22:12:45 +0100
committerDiego Elio Pettenò <flameeyes@flameeyes.eu>2018-12-12 22:12:45 +0100
commitf7d96c7a8e8044254a01827df77c4c9d15a71df9 (patch)
tree08f0c4687b884c1ccb9019feb27c73ff7a8aa1c8
parentMake GlucoseReading and KetoneReading use attrs, too. (diff)
downloadglucometerutils-f7d96c7a8e8044254a01827df77c4c9d15a71df9.tar
glucometerutils-f7d96c7a8e8044254a01827df77c4c9d15a71df9.tar.gz
glucometerutils-f7d96c7a8e8044254a01827df77c4c9d15a71df9.tar.bz2
glucometerutils-f7d96c7a8e8044254a01827df77c4c9d15a71df9.tar.lz
glucometerutils-f7d96c7a8e8044254a01827df77c4c9d15a71df9.tar.xz
glucometerutils-f7d96c7a8e8044254a01827df77c4c9d15a71df9.tar.zst
glucometerutils-f7d96c7a8e8044254a01827df77c4c9d15a71df9.zip
-rw-r--r--glucometerutils/common.py169
1 files changed, 86 insertions, 83 deletions
diff --git a/glucometerutils/common.py b/glucometerutils/common.py
index 9af5195..1645c8c 100644
--- a/glucometerutils/common.py
+++ b/glucometerutils/common.py
@@ -21,112 +21,115 @@ import attr
from glucometerutils import exceptions
class Unit(enum.Enum):
- MG_DL = 'mg/dL'
- MMOL_L = 'mmol/L'
+ MG_DL = 'mg/dL'
+ MMOL_L = 'mmol/L'
# Constants for meal information
class Meal(enum.Enum):
- NONE = ''
- BEFORE = 'Before Meal'
- AFTER = 'After Meal'
+ NONE = ''
+ BEFORE = 'Before Meal'
+ AFTER = 'After Meal'
# Constants for measure method
class MeasurementMethod(enum.Enum):
- BLOOD_SAMPLE = 'blood sample'
- CGM = 'CGM' # Continuous Glucose Monitoring
+ BLOOD_SAMPLE = 'blood sample'
+ CGM = 'CGM' # Continuous Glucose Monitoring
def convert_glucose_unit(value, from_unit, to_unit):
- """Convert the given value of glucose level between units.
+ """Convert the given value of glucose level between units.
- Args:
- value: The value of glucose in the current unit
- from_unit: The unit value is currently expressed in
- to_unit: The unit to conver the value to: the other if empty.
+ Args:
+ value: The value of glucose in the current unit
+ from_unit: The unit value is currently expressed in
+ to_unit: The unit to conver the value to: the other if empty.
- Returns:
- The converted representation of the blood glucose level.
- """
- from_unit = Unit(from_unit)
- to_unit = Unit(to_unit)
+ Returns:
+ The converted representation of the blood glucose level.
+ """
+ from_unit = Unit(from_unit)
+ to_unit = Unit(to_unit)
- if from_unit == to_unit:
- return value
+ if from_unit == to_unit:
+ return value
- if from_unit == Unit.MG_DL:
- return round(value / 18.0, 2)
- else:
- return round(value * 18.0, 0)
+ if from_unit == Unit.MG_DL:
+ return round(value / 18.0, 2)
+ else:
+ return round(value * 18.0, 0)
@attr.s
class GlucoseReading:
- timestamp = attr.ib() # type: datetime.datetime
- value = attr.ib() # type: float
- meal = attr.ib(
- default=Meal.NONE, validator=attr.validators.in_(Meal)) # type: Meal
- comment = attr.ib(default='') # type: Text
- measure_method = attr.ib(
- default=MeasurementMethod.BLOOD_SAMPLE,
- validator=attr.validators.in_(MeasurementMethod)) # type: MeasurementMethod
-
- def get_value_as(self, to_unit):
- """Returns the reading value as the given unit.
-
- Args:
- to_unit: (Unit) The unit to return the value to.
- """
- 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."""
- return '"%s","%.2f","%s","%s","%s"' % (
- self.timestamp, self.get_value_as(unit), self.meal.value,
- self.measure_method.value, self.comment)
+ timestamp = attr.ib() # type: datetime.datetime
+ value = attr.ib() # type: float
+ meal = attr.ib(
+ default=Meal.NONE, validator=attr.validators.in_(Meal)) # type: Meal
+ comment = attr.ib(default='') # type: Text
+ measure_method = attr.ib(
+ default=MeasurementMethod.BLOOD_SAMPLE,
+ validator=attr.validators.in_(
+ MeasurementMethod)) # type: MeasurementMethod
+
+ def get_value_as(self, to_unit):
+ """Returns the reading value as the given unit.
+
+ Args:
+ to_unit: (Unit) The unit to return the value to.
+ """
+ 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."""
+ return '"%s","%.2f","%s","%s","%s"' % (
+ self.timestamp, self.get_value_as(unit), self.meal.value,
+ self.measure_method.value, self.comment)
@attr.s
class KetoneReading:
- timestamp = attr.ib() # type: datetime.datetime
- value = attr.ib() # type: float
- comment = attr.ib(default='') # type: Text
+ timestamp = attr.ib() # type: datetime.datetime
+ value = attr.ib() # type: float
+ comment = attr.ib(default='') # type: Text
- def as_csv(self, unit):
- """Returns the reading as a formatted comma-separated value string."""
- del unit # Unused for Ketone readings.
+ def as_csv(self, unit):
+ """Returns the reading as a formatted comma-separated value string."""
+ del unit # Unused for Ketone readings.
- return '"%s","%.2f","%s","%s"' % (
- self.timestamp, self.value, MeasurementMethod.BLOOD_SAMPLE,
- self.comment)
+ return '"%s","%.2f","%s","%s"' % (
+ self.timestamp, self.value, MeasurementMethod.BLOOD_SAMPLE,
+ self.comment)
@attr.s
class MeterInfo:
- """General information aobut the meter, such as model, serial number and unit.
-
- Attributes:
- model: Human readable model name, chosen by the driver.
- serial_number: Serial number identified for the reader (or N/A if not
- available in the protocol.)
- version_info: List of strings with any version information available about
- the device. It can include hardware and software version.
- native_unite: 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=())
- native_unit = attr.ib(default=Unit.MG_DL, validator=attr.validators.in_(Unit))
-
- def __str__(self):
- version_information_string = 'N/A'
- if self.version_info:
- version_information_string = '\n '.join(self.version_info).strip()
-
- return textwrap.dedent("""\
- {model}
- Serial Number: {serial_number}
- Version Information:
- {version_information_string}
- Native Unit: {native_unit}
- """).format(model=self.model, serial_number=self.serial_number,
- version_information_string=version_information_string,
- native_unit=self.native_unit.value)
+ """General information about the meter.
+
+ Attributes:
+ model: Human readable model name, chosen by the driver.
+ serial_number: Serial number identified for the reader (or N/A if not
+ available in the protocol.)
+ version_info: List of strings with any version information available about
+ 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=())
+ native_unit = attr.ib(
+ default=Unit.MG_DL, validator=attr.validators.in_(Unit))
+
+ def __str__(self):
+ version_information_string = 'N/A'
+ if self.version_info:
+ version_information_string = '\n '.join(
+ self.version_info).strip()
+
+ return textwrap.dedent("""\
+ {model}
+ Serial Number: {serial_number}
+ Version Information:
+ {version_information_string}
+ Native Unit: {native_unit}
+ """).format(model=self.model, serial_number=self.serial_number,
+ version_information_string=version_information_string,
+ native_unit=self.native_unit.value)