From 5d6b2075c98ea6f69bb5c096236ce1fd172ac606 Mon Sep 17 00:00:00 2001 From: Dorian Scholz Date: Tue, 30 May 2017 13:22:25 +0800 Subject: Fix IndexError and KeyError: even if rapid-acting-flag is set double-rapid-acting-insulin record does not always exist, so make it optional and provide a default value --- glucometerutils/drivers/fslibre.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/glucometerutils/drivers/fslibre.py b/glucometerutils/drivers/fslibre.py index 47d2a8b..b364cc2 100644 --- a/glucometerutils/drivers/fslibre.py +++ b/glucometerutils/drivers/fslibre.py @@ -68,10 +68,12 @@ def _parse_record(record, entry_map): if not record: return {} - return { - key: int(record[idx]) - for idx, key in entry_map - } + try: + return { + key: int(record[idx]) for idx, key in entry_map + } + except IndexError: + return {} def _extract_timestamp(parsed_record): @@ -145,7 +147,9 @@ def _parse_arresult(record): comment_parts.append('Long-acting insulin') if parsed_record['rapid-acting-flag']: - if parsed_record['double-rapid-acting-insulin']: + # provide default value, as this record does not always exist + # (even if rapid-acting-flag is set) + if parsed_record.get('double-rapid-acting-insulin', 0): comment_parts.append( 'Rapid-acting insulin (%d)' % (parsed_record['double-rapid-acting-insulin']/2)) -- cgit v1.2.3