From 3d10e3eae9bad78b6602e68364536362d178a239 Mon Sep 17 00:00:00 2001 From: Sergei Trofimov Date: Wed, 16 Aug 2017 15:58:10 +0100 Subject: [PATCH] utils/types: numeric expeanded to handle percentages numeric() conversion function is expanded to handle percentage values in the form "10.123%". The result is the numeric part converted to a float and divided by 100, e.g. 0.10123. --- devlib/utils/types.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/devlib/utils/types.py b/devlib/utils/types.py index be30bfc..645328d 100644 --- a/devlib/utils/types.py +++ b/devlib/utils/types.py @@ -68,6 +68,15 @@ def numeric(value): """ if isinstance(value, int): return value + + if isinstance(value, basestring): + value = value.strip() + if value.endswith('%'): + try: + return float(value.rstrip('%')) / 100 + except ValueError: + raise ValueError('Not numeric: {}'.format(value)) + try: fvalue = float(value) except ValueError: