From b7ecdd9ea16748970aa7c67b07456e9ce0b4944b Mon Sep 17 00:00:00 2001 From: Ceyda Duzgec Date: Wed, 10 Sep 2025 12:11:22 +0300 Subject: [PATCH 1/2] Update filter.py --- filter.py | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/filter.py b/filter.py index cfbbea4f..11dcc5a2 100644 --- a/filter.py +++ b/filter.py @@ -10,32 +10,46 @@ def _country_filter(src, scope, out): :arg src: source dictionary :arg scope: source selector + :arg out: unused parameter (kept for compatibility) """ - def filter(entry, item): + def filter_entry(entry, item): matching = entry["country"] + return item.lower() == matching.lower() - return item == matching or item == matching.lower() or item == matching.upper() - - return [entry for entry in src if filter(entry, scope)] + return [entry for entry in src if filter_entry(entry, scope)] def country_filter(src, scopes): """ - Either make multiple data searches or - execute one. {NEEDS IMPROVEMENT, O(kN) => O(n)} + Filter data by country(ies) with improved efficiency and no duplicates :arg src: source dictionary - :arg scopes: source selectors + :arg scopes: source selectors (string or list) """ - out = [] - - if type(scopes) is list: - [out.extend(_country_filter(src, scope, out)) for scope in scopes] - else: - out = _country_filter(src, scopes, out) - - return out + if not scopes: # Handle empty scopes + return src + + if isinstance(scopes, str): + return _country_filter(src, scopes, None) + + if isinstance(scopes, list): + if not scopes: # Empty list + return src + + # Convert all scopes to lowercase for case-insensitive comparison + normalized_scopes = [scope.lower() for scope in scopes] + + # Filter entries where country matches any scope (case-insensitive) + filtered_entries = [ + entry for entry in src + if entry["country"].lower() in normalized_scopes + ] + + # Sort by country name for consistent ordering + return sorted(filtered_entries, key=lambda x: x["country"]) + + return [] def main(): @@ -43,7 +57,7 @@ def main(): temp_arg = "" first_word = True - # Retrieve our selecting countries (seperated by commas) + # Retrieve our selecting countries (separated by commas) for arg in sys.argv[1:]: temp_arg += arg if first_word else " " + arg first_word = False @@ -73,4 +87,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file From e689a96a96e79b6cb385f03025709687033a25fe Mon Sep 17 00:00:00 2001 From: Ceyda Duzgec Date: Wed, 10 Sep 2025 12:12:54 +0300 Subject: [PATCH 2/2] Fix black issue --- filter.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/filter.py b/filter.py index 11dcc5a2..74d27909 100644 --- a/filter.py +++ b/filter.py @@ -29,26 +29,25 @@ def country_filter(src, scopes): """ if not scopes: # Handle empty scopes return src - + if isinstance(scopes, str): return _country_filter(src, scopes, None) - + if isinstance(scopes, list): if not scopes: # Empty list return src - + # Convert all scopes to lowercase for case-insensitive comparison normalized_scopes = [scope.lower() for scope in scopes] - + # Filter entries where country matches any scope (case-insensitive) filtered_entries = [ - entry for entry in src - if entry["country"].lower() in normalized_scopes + entry for entry in src if entry["country"].lower() in normalized_scopes ] - + # Sort by country name for consistent ordering return sorted(filtered_entries, key=lambda x: x["country"]) - + return [] @@ -87,4 +86,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main()