"""Helper functions for Soundcloud workflow.""" import string def has_numbers(string_: str) -> bool: return any(char.isdigit() for char in string_) def move_char_index(string_: str, char: str, new_index=-1) -> str: if char not in string_: return string_ char_list, old_index = list(string_), string_.index(char) char = char_list.pop(old_index) char_list.insert(new_index, char) return ''.join(char_list) def abbreviated_value_to_int(value: str) -> int: """Convert values like 4M, 35.6K, 10,650 to int.""" _value = value.replace('\n', '').strip().replace(' ', '') for c in string.ascii_letters.replace('K', '').replace('M', ''): _value = _value.replace(c, '') if any(c.isalpha() for c in _value): _value = _value.replace('M', '000000').replace('K', '000') formatted = int(float(move_char_index(_value, '.'))) return formatted return int(_value.replace(',', '')) if has_numbers(_value) else 0