From e1358b6f05802e61ae1ccfd60b052ca6ac624e99 Mon Sep 17 00:00:00 2001 From: James Tocknell Date: Sun, 26 Jan 2020 20:33:51 +1100 Subject: [PATCH] Add option of returning None on failure --- pylintrc | 2 +- stringtopy/__init__.py | 33 ++++++++++++++++++++++++--------- tests/test_float.py | 5 +++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/pylintrc b/pylintrc index 5afabe2..29a4e08 100644 --- a/pylintrc +++ b/pylintrc @@ -61,7 +61,7 @@ evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / stateme # --enable=similarities". If you want to run only the classes checker, but have # no Warning level messages displayed, use"--disable=all --enable=classes # --disable=W" -disable=invalid-name,bad-continuation,too-many-arguments,too-many-locals,too-many-statements,redefined-builtin +disable=invalid-name,bad-continuation,too-many-arguments,too-many-locals,too-many-statements,redefined-builtin,no-else-return [SIMILARITIES] diff --git a/stringtopy/__init__.py b/stringtopy/__init__.py index e2350f0..eedb0cd 100644 --- a/stringtopy/__init__.py +++ b/stringtopy/__init__.py @@ -19,30 +19,42 @@ BOOLEAN_FALSE = {'0', 'no', 'false', 'off', } -def str_to_float_converter(): +def str_to_float_converter(use_none_on_fail=False): """ - Returns a human friendly float converter, currently takes no arguments. + Returns a human friendly float converter, can use use_none_on_fail to + return None if value cannot be converted. """ def str_to_float(s): """ Convert a string to a float """ - return float(Fraction(s)) + try: + return float(Fraction(s)) + except ValueError: + if use_none_on_fail: + return None + raise return str_to_float -def str_to_int_converter(): +def str_to_int_converter(use_none_on_fail=False): """ - Returns a human friendly int converter, currently takes no arguments. + Returns a human friendly int converter, can use use_none_on_fail to return + None if value cannot be converted. """ def str_to_int(s): """ Convert a string to a int """ - frac = Fraction(s) - if frac.denominator == 1: - return int(frac) - raise ValueError("{} is not an integer".format(frac)) + try: + frac = Fraction(s) + if frac.denominator == 1: + return int(frac) + raise ValueError("{} is not an integer".format(frac)) + except ValueError: + if use_none_on_fail: + return None + raise return str_to_int @@ -79,6 +91,9 @@ def str_to_bool_converter( ) def str_to_bool(s): + """ + Convert a string to a bool, based on settings + """ s = s.strip().lower() if s in boolean_true: return True diff --git a/tests/test_float.py b/tests/test_float.py index 4a080e7..ff62782 100644 --- a/tests/test_float.py +++ b/tests/test_float.py @@ -1,12 +1,17 @@ from __future__ import division import pytest +from stringtopy import str_to_float_converter def test_not_float(default_str_to_float): with pytest.raises(ValueError): default_str_to_float("this is not a float") +def test_not_float_with_none(): + str_to_float = str_to_float_converter(use_none_on_fail=True) + assert str_to_float("this is not a float") is None + def test_decimal(default_str_to_float): assert 1.234 == default_str_to_float("1.234")