X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=string_utils.py;h=3aaf1d7efe4151c61a1739af72234abc0a69fbc3;hb=d2730e42f1160d45ab6c7780987b16ae83c616f6;hp=5eb03d275e184af8709a87da3885b5827588c501;hpb=709370b2198e09f1dbe195fe8813602a3125b7f6;p=python_utils.git diff --git a/string_utils.py b/string_utils.py index 5eb03d2..3aaf1d7 100644 --- a/string_utils.py +++ b/string_utils.py @@ -28,7 +28,7 @@ URLS_RAW_STRING = ( r"([a-z-]+://)" # scheme r"([a-z_\d-]+:[a-z_\d-]+@)?" # user:password r"(www\.)?" # www. - r"((? bool: True >>> is_none_or_empty(None) True - >>> is_none_or_empty(" ") + >>> is_none_or_empty(" \t ") True >>> is_none_or_empty('Test') False @@ -175,18 +175,22 @@ def is_string(obj: Any) -> bool: def is_empty_string(in_str: Any) -> bool: + return is_empty(in_str) + + +def is_empty(in_str: Any) -> bool: """ Checks if input is a string and empty or only whitespace. - >>> is_empty_string('') + >>> is_empty('') True - >>> is_empty_string(' \t\t ') + >>> is_empty(' \t\t ') True - >>> is_empty_string('test') + >>> is_empty('test') False - >>> is_empty_string(100.88) + >>> is_empty(100.88) False - >>> is_empty_string([1, 2, 3]) + >>> is_empty([1, 2, 3]) False """ return is_string(in_str) and in_str.strip() == "" @@ -733,8 +737,6 @@ def is_ip(in_str: Any) -> bool: """ Checks if a string is a valid ip (either v4 or v6). - *Examples:* - >>> is_ip('255.200.100.75') True >>> is_ip('2001:db8:85a3:0000:0000:8a2e:370:7334') @@ -1057,18 +1059,26 @@ def to_bool(in_str: str) -> bool: >>> to_bool('True') True + >>> to_bool('1') True + >>> to_bool('yes') True + >>> to_bool('no') False + >>> to_bool('huh?') False + + >>> to_bool('on') + True + """ if not is_string(in_str): raise ValueError(in_str) - return in_str.lower() in ("true", "1", "yes", "y", "t") + return in_str.lower() in ("true", "1", "yes", "y", "t", "on") def to_date(in_str: str) -> Optional[datetime.date]: