Refactor duplicate link checker

This commit is contained in:
Matheus Felipe 2022-01-11 20:58:05 -03:00
parent 2eb6d20100
commit d728eeddbc
No known key found for this signature in database
GPG key ID: AA785C523274872F

View file

@ -2,7 +2,7 @@
import sys import sys
import re import re
from typing import List from typing import List, Tuple
def find_links_in_text(text: str) -> List[str]: def find_links_in_text(text: str) -> List[str]:
@ -32,10 +32,11 @@ def find_links_in_file(filename: str) -> List[str]:
return links return links
def check_duplicate_links(links: List[str]) -> bool: def check_duplicate_links(links: List[str]) -> Tuple[bool, List]:
"""Check for duplicated links and return True or False.""" """Check for duplicated links.
print('Checking for duplicated links...') Returns a tuple with True or False and duplicate list.
"""
seen = {} seen = {}
duplicates = [] duplicates = []
@ -48,13 +49,10 @@ def check_duplicate_links(links: List[str]) -> bool:
if seen[link] == 1: if seen[link] == 1:
duplicates.append(link) duplicates.append(link)
if not duplicates: if duplicates:
print(f'No duplicate links.')
else:
print(f'Found duplicate links: {duplicates}')
has_duplicate = True has_duplicate = True
return has_duplicate return (has_duplicate, duplicates)
if __name__ == '__main__': if __name__ == '__main__':
@ -66,4 +64,11 @@ if __name__ == '__main__':
links = find_links_in_file(sys.argv[1]) links = find_links_in_file(sys.argv[1])
has_duplicate = check_duplicate_links(links) print('Checking for duplicate links...')
has_duplicate_link, duplicates_links = check_duplicate_links(links)
if has_duplicate_link:
print(f'Found duplicate links: {duplicates_links}')
else:
print('No duplicate links.')