From 1e0da90e7b0806f7ec175c6d60b78938dd7a1634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Sun, 15 Jan 2017 13:24:46 +0000 Subject: Move the readings-to-csv conversion to the Reading class. --- glucometer.py | 4 +--- glucometerutils/common.py | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/glucometer.py b/glucometer.py index 8e19237..148daff 100755 --- a/glucometer.py +++ b/glucometer.py @@ -76,9 +76,7 @@ def main(): readings, key=lambda reading: getattr(reading, args.sort_by)) for reading in readings: - print('"%s","%.2f","%s","%s"' % ( - reading.timestamp, reading.get_value_as(unit), - reading.meal, reading.comment)) + print(reading.as_csv(unit)) elif args.action == 'datetime': if args.set == 'now': print(device.set_datetime()) diff --git a/glucometerutils/common.py b/glucometerutils/common.py index 1e7e7f8..ac5c931 100644 --- a/glucometerutils/common.py +++ b/glucometerutils/common.py @@ -84,6 +84,12 @@ class Reading(_ReadingBase): """ return convert_glucose_unit(self.value, UNIT_MGDL, to_unit) + 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.meal, self.comment) + + _MeterInfoBase = collections.namedtuple( '_MeterInfoBase', ['model', 'serial_number', 'version_info', 'native_unit']) -- cgit v1.2.3