From 6eae24cb3eee69461ff29fec9798bcd18083ea18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Wed, 12 Dec 2018 20:35:37 +0000 Subject: Use [attrs](http://www.attrs.org/) instead of namedtuple for MeterInfo. This avoids having the separate _MeterInfoBase class, and reads much nicer. --- glucometerutils/common.py | 36 ++++++++++++++++++------------------ setup.py | 3 +++ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/glucometerutils/common.py b/glucometerutils/common.py index ba4f018..16115c0 100644 --- a/glucometerutils/common.py +++ b/glucometerutils/common.py @@ -10,6 +10,8 @@ import collections import enum import textwrap +import attr + from glucometerutils import exceptions class Unit(enum.Enum): @@ -117,24 +119,22 @@ class KetoneReading(_ReadingBase): self.timestamp, self.get_value_as(unit), self.measure_method.value, self.comment) - -_MeterInfoBase = collections.namedtuple( - '_MeterInfoBase', ['model', 'serial_number', 'version_info', 'native_unit']) - -class MeterInfo(_MeterInfoBase): - def __new__(cls, model, serial_number='N/A', version_info=(), - 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) Native unit of the device for display. - """ - return super(MeterInfo, cls).__new__( - cls, model=model, serial_number=serial_number, version_info=version_info, - native_unit=native_unit) +@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' diff --git a/setup.py b/setup.py index 0a9f258..072b29b 100644 --- a/setup.py +++ b/setup.py @@ -44,6 +44,9 @@ setup( data_files = [ ('lib/udev/rules', ['udev/69-glucometerutils.rules']), ], + install_require = [ + 'attrs', + ], tests_require = test_required, extras_require = { # These are all the drivers' dependencies. Optional dependencies are -- cgit v1.2.3