Improve persistent after actually using it.
[python_utils.git] / string_utils.py
index 5eb03d275e184af8709a87da3885b5827588c501..3aaf1d7efe4151c61a1739af72234abc0a69fbc3 100644 (file)
@@ -28,7 +28,7 @@ URLS_RAW_STRING = (
     r"([a-z-]+://)"  # scheme
     r"([a-z_\d-]+:[a-z_\d-]+@)?"  # user:password
     r"(www\.)?"  # www.
-    r"((?<!\.)[a-z\d]+[a-z\d.-]+\.[a-z]{2,6}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|localhost)"  # domain
+    r"((?<!\.)[a-z\d]+[a-z\d.-]+\.[a-z]{2,6}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|localhost)" # domain
     r"(:\d{2,})?"  # port number
     r"(/[a-z\d_%+-]*)*"  # folders
     r"(\.[a-z\d_%+-]+)*"  # file extension
@@ -150,7 +150,7 @@ def is_none_or_empty(in_str: Optional[str]) -> 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]: