Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Scripts/deduplicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

from common import is_valid_domain, write_lines

HEADER_PREFIXES = ("! ", "#", "[", ";")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This is a good optimization. I have a couple of suggestions for this new constant:

  1. Typing: For consistency with the change in update-lists.py and to improve type safety, consider adding a type hint. This aligns with modern Python practices and helps static analysis tools. You would need to import Final from typing.

    from typing import Final
    
    # ...
    
    HEADER_PREFIXES: Final[tuple[str, ...]] = ("! ", "#", "[", ";")
  2. Inconsistency: I noticed HEADER_PREFIXES here is ("! ", "#", "[", ";"), while in Scripts/update-lists.py it is ("! ", "#", "["). If this difference is intentional, a comment explaining why would be helpful. If they serve the same purpose, consider unifying them into a single constant in common.py to prevent potential discrepancies.



@dataclass(slots=True)
class Stats:
Expand All @@ -34,7 +36,7 @@ def compression_ratio(self) -> float:

def is_header(line: str) -> bool:
"""Check if line is a header/metadata line"""
return line.startswith(("! ", "#", "[", ";")) or not line
return line.startswith(HEADER_PREFIXES) or not line


def is_valid_rule(line: str) -> bool:
Expand Down
3 changes: 2 additions & 1 deletion Scripts/update-lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
SOURCES_CONFIG: Final[str] = "lists/sources-urls.json"
DEFAULT_OUTPUT: Final[str] = "lists/sources"
METADATA_FILE: Final[str] = "lists/sources-metadata.json"
HEADER_PREFIXES: Final[tuple[str, ...]] = ("! ", "#", "[")
TIMEOUT: Final[int] = 60
CHUNK_SIZE: Final[int] = 65536
MAX_CONCURRENT: Final[int] = 10
Expand Down Expand Up @@ -90,7 +91,7 @@ def count_rules(content: str) -> int:
return sum(
1
for line in io.StringIO(content)
if (stripped := line.strip()) and not stripped.startswith(("! ", "#", "["))
if (stripped := line.strip()) and not stripped.startswith(HEADER_PREFIXES)
)


Expand Down
Loading