cornsnake.util_string
1import re 2 3""" 4Check if a text string is empty. The `is_empty` function checks if a text string is None or contains only whitespace or a hyphen. 5 6[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_string.html) 7""" 8 9 10def is_empty(text): 11 """ 12 Function to check if a text string is empty or '-'. 13 14 Args: 15 text (str): The text string to check. 16 17 Returns: 18 bool: True if the text is empty (None or contains only whitespace or a hyphen), False otherwise. 19 """ 20 if text is None: 21 return True 22 stripped = text.strip() 23 return stripped == "" or stripped == "-" 24 25 26def replace_keep_case(word__for_regex, replacement, text): 27 """ 28 Replace ocurrences of 'word__for_regex' in 'text', with 'replacement', trying to maintain the same casing. 29 30 - supports lower, titla and upper casing 31 """ 32 33 def func(match): 34 g = match.group() 35 if g.islower(): 36 return replacement.lower() 37 if g.istitle(): 38 return replacement.title() 39 if g.isupper(): 40 return replacement.upper() 41 return replacement 42 43 return re.sub(word__for_regex, func, text, flags=re.I)
def
is_empty(text):
11def is_empty(text): 12 """ 13 Function to check if a text string is empty or '-'. 14 15 Args: 16 text (str): The text string to check. 17 18 Returns: 19 bool: True if the text is empty (None or contains only whitespace or a hyphen), False otherwise. 20 """ 21 if text is None: 22 return True 23 stripped = text.strip() 24 return stripped == "" or stripped == "-"
Function to check if a text string is empty or '-'.
Args: text (str): The text string to check.
Returns: bool: True if the text is empty (None or contains only whitespace or a hyphen), False otherwise.
def
replace_keep_case(word__for_regex, replacement, text):
27def replace_keep_case(word__for_regex, replacement, text): 28 """ 29 Replace ocurrences of 'word__for_regex' in 'text', with 'replacement', trying to maintain the same casing. 30 31 - supports lower, titla and upper casing 32 """ 33 34 def func(match): 35 g = match.group() 36 if g.islower(): 37 return replacement.lower() 38 if g.istitle(): 39 return replacement.title() 40 if g.isupper(): 41 return replacement.upper() 42 return replacement 43 44 return re.sub(word__for_regex, func, text, flags=re.I)
Replace ocurrences of 'word__for_regex' in 'text', with 'replacement', trying to maintain the same casing.
- supports lower, titla and upper casing