From 56a1ec2dbdb260bbe9da526d70df065ab61654c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Kvalsvik?= Date: Mon, 22 Apr 2019 18:34:29 +0200 Subject: [PATCH] Reorder attribte updates in basicobject.load There are far more scalar elements than boolean elements, so it makes sense to in the if-else chain check the scalar first. On top of that, raise a ValueError should the value type of the attribute not be one of the three blessed values. --- python/dlisio/plumbing/basicobject.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/python/dlisio/plumbing/basicobject.py b/python/dlisio/plumbing/basicobject.py index 8f41bd8e2..01829ddcc 100644 --- a/python/dlisio/plumbing/basicobject.py +++ b/python/dlisio/plumbing/basicobject.py @@ -136,15 +136,21 @@ def load(cls, obj, name = None): if value is None: continue attr, value_type = attrs[label] - if value_type == ValueTypeBoolean: - if value[0]: setattr(self, attr, True) - else: setattr(self, attr, False) - - elif value_type == ValueTypeScalar: + if value_type == ValueTypeScalar: setattr(self, attr, value[0]) elif value_type == ValueTypeVector: setattr(self, attr, value) + elif value_type == ValueTypeBoolean: + setattr(self, attr, bool(value[0])) + + else: + problem = 'unknown value extraction descriptor {}' + solution = 'should be either scalar, vector, or boolean' + msg = ', '.join((problem.format(value_type), solution)) + raise ValueError(msg) + + self.stripspaces() return self