34 lines
635 B
Python
34 lines
635 B
Python
"""Utilities"""
|
|
|
|
import re
|
|
|
|
|
|
def letters_only(s: str) -> str:
|
|
return re.compile('[^a-zA-Z]').sub('', s)
|
|
|
|
|
|
def words_only(s: str) -> str:
|
|
words = [letters_only(x) for x in s.split()]
|
|
r = []
|
|
for w in words:
|
|
if w:
|
|
r.append(w)
|
|
return ' '.join(r)
|
|
|
|
|
|
def file_name(s: str, ext='txt') -> str:
|
|
return '%s.%s' % (s.lower().replace(' ', '_'), ext)
|
|
|
|
|
|
def dedupe(strings: list[str]) -> list[str]:
|
|
seen = set()
|
|
r = []
|
|
for s in strings:
|
|
if s not in seen:
|
|
r.append(s)
|
|
seen.add(s)
|
|
return r
|
|
|
|
|
|
def header(s: str) -> str:
|
|
return words_only(s).lower().title()
|