Any comments / improvements welcome on this simple function.
tests.py
@pytest.mark.parametrize([
# path
'/c/target-uri',
'/c/uk/indiana/target-uri',
'/c/uk/indiana/target-uri/persons',
'/c/uk/florida/target-uri/persons',
])
def test_extract_uri(self, path):
assert extract_uri(path) == 'target-uri'
main.py
from states import state_names
def extract_uri(path: str):
exclude_path_snippets = ['persons']
def filter_fn(element: str):
return all([
element.lower() not in state_names,
element.lower() not in exclude_path_snippets,
len(element) > 2, # exclude 'c' or 'uk'
])
path_snippets: List[str] = path.split('/')
filtered = list(filter(filter_fn, path_snippets))
if len(filtered) != 1:
raise Exception(f"Failed to extract uri from {path}")
return filtered[0]