Skip to content

Commit

Permalink
refactor(*): dedup while keeping order using dict.fromkeys()
Browse files Browse the repository at this point in the history
Previously, I was not in favor of the approach because it was a quite
new feature in Python 3.7+ (CPython 3.6+). Newness always means
confusion to some extent. But it is no longer a new feature after all
these years. So I am going to adopt such an elegant approach.

Signed-off-by: Rongrong <i@rong.moe>
  • Loading branch information
Rongronggg9 committed Apr 25, 2024
1 parent 84bd8bc commit 386fd42
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __compare_version(version1: str, version2: str) -> tuple[int, list[Union[int
os.path.join(os.path.abspath('.'), '.env'))
if is_self_run_as_a_whole_package:
dot_env_paths = (os.path.normpath(os.path.join(self_path, '..', '.env')),) + dot_env_paths
for dot_env_path in sorted(set(dot_env_paths), key=dot_env_paths.index):
for dot_env_path in dict.fromkeys(dot_env_paths): # remove duplicates while keeping the order
if os.path.isfile(dot_env_path):
load_dotenv(dot_env_path, override=True)
logger.info(f'Found .env file at "{dot_env_path}", loaded')
Expand Down
4 changes: 2 additions & 2 deletions src/parsing/medium.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ class Medium(AbstractMedium):
def __init__(self, urls: Union[str, list[str]], type_fallback_urls: Optional[Union[str, list[str]]] = None):
super().__init__()
urls = urls if isinstance(urls, list) else [urls]
# dedup, should not use a set because sequence is important
self.urls: list[str] = sorted(set(urls), key=urls.index)
# dedup while keeping the order
self.urls: list[str] = list(dict.fromkeys(urls))
self.original_urls: tuple[str, ...] = tuple(self.urls)
self.chosen_url: Optional[str] = self.urls[0]
self._server_change_count: int = 0
Expand Down

0 comments on commit 386fd42

Please sign in to comment.