Files
saladeaula.digital/layercake/layercake/strutils.py
2025-07-14 11:09:16 -03:00

26 lines
663 B
Python

import hashlib
import random
import string
def first_word(s: str) -> str:
"""Returns the first word from a string."""
return s.split(' ', 1)[0]
def truncate_str(s: str, maxlen: int = 30) -> str:
"""Truncates a string to fit within a maximum length, adding '...' if truncated."""
if len(s) <= maxlen:
return s
return s[: maxlen - 3] + '...'
def random_str(maxlen: int = 10) -> str:
"""Returns a random string of letters."""
return ''.join(random.choice(string.ascii_letters) for _ in range(maxlen))
def md5_hash(s: str) -> str:
"""Computes the MD5 hash of a string."""
return hashlib.md5(s.encode()).hexdigest()