Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default functions to save user typing #13

Merged
merged 1 commit into from
May 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions stringtopy/__init__.py
Original file line number Diff line number Diff line change
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()