diff options
author | Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com> | 2018-08-21 12:54:39 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2018-08-21 12:54:39 +0000 |
commit | 95c0824278c2e273a3017b7c9b8cf6a026950f74 (patch) | |
tree | 7885197fbde549981f2adad38a289bbb02b41a86 /yardstick/common | |
parent | eea4e0e9e2261ef27b5884c51d763b71ff324fb9 (diff) | |
parent | 7022312148a5644239ec81e16b9b9c692c62c7ff (diff) |
Merge "Convert IXIA latency statistics to integer"
Diffstat (limited to 'yardstick/common')
-rw-r--r-- | yardstick/common/exceptions.py | 4 | ||||
-rw-r--r-- | yardstick/common/utils.py | 22 |
2 files changed, 26 insertions, 0 deletions
diff --git a/yardstick/common/exceptions.py b/yardstick/common/exceptions.py index b39a0af9c..10c1f3f27 100644 --- a/yardstick/common/exceptions.py +++ b/yardstick/common/exceptions.py @@ -79,6 +79,10 @@ class FunctionNotImplemented(YardstickException): '"%(class_name)" class.') +class InvalidType(YardstickException): + message = 'Type "%(type_to_convert)s" is not valid' + + class InfluxDBConfigurationMissing(YardstickException): message = ('InfluxDB configuration is not available. Add "influxdb" as ' 'a dispatcher and the configuration section') diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index c019cd264..31885c073 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -21,6 +21,7 @@ import importlib import ipaddress import logging import os +import pydoc import random import re import signal @@ -578,3 +579,24 @@ def send_socket_command(host, port, command): finally: sock.close() return ret + + +def safe_cast(value, type_to_convert, default_value): + """Convert value to type, in case of error return default_value + + :param value: value to convert + :param type_to_convert: type to convert, could be "type" or "string" + :param default_value: default value to return + :return: converted value or default_value + """ + if isinstance(type_to_convert, type): + _type = type_to_convert + else: + _type = pydoc.locate(type_to_convert) + if not _type: + raise exceptions.InvalidType(type_to_convert=type_to_convert) + + try: + return _type(value) + except ValueError: + return default_value |