Skip to content

Commit

Permalink
Add default functions to save user typing
Browse files Browse the repository at this point in the history
  • Loading branch information
aragilar committed May 24, 2020
1 parent e1358b6 commit ae895a3
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions stringtopy/__init__.py
Expand Up @@ -24,7 +24,7 @@ def str_to_float_converter(use_none_on_fail=False):
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):
def str_to_float_func(s):
"""
Convert a string to a float
"""
Expand All @@ -34,15 +34,15 @@ def str_to_float(s):
if use_none_on_fail:
return None
raise
return str_to_float
return str_to_float_func


def str_to_int_converter(use_none_on_fail=False):
"""
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):
def str_to_int_func(s):
"""
Convert a string to a int
"""
Expand All @@ -55,7 +55,7 @@ def str_to_int(s):
if use_none_on_fail:
return None
raise
return str_to_int
return str_to_int_func


def str_to_bool_converter(
Expand Down Expand Up @@ -90,7 +90,7 @@ def str_to_bool_converter(
"{} are both True and False".format(boolean_true & boolean_false)
)

def str_to_bool(s):
def str_to_bool_func(s):
"""
Convert a string to a bool, based on settings
"""
Expand All @@ -100,4 +100,11 @@ def str_to_bool(s):
if s in boolean_false:
return False
raise ValueError("{} is neither True nor False.".format(s))
return str_to_bool
return str_to_bool_func


# versions using defaults so that users can import the actual functions, rather
# than creating their own
str_to_float = str_to_float_converter()
str_to_int = str_to_int_converter()
str_to_bool = str_to_bool_converter()

0 comments on commit ae895a3

Please sign in to comment.