@@ -10,40 +10,54 @@ def _country_filter(src, scope, out):
1010
1111 :arg src: source dictionary
1212 :arg scope: source selector
13+ :arg out: unused parameter (kept for compatibility)
1314 """
1415
15- def filter (entry , item ):
16+ def filter_entry (entry , item ):
1617 matching = entry ["country" ]
18+ return item .lower () == matching .lower ()
1719
18- return item == matching or item == matching .lower () or item == matching .upper ()
19-
20- return [entry for entry in src if filter (entry , scope )]
20+ return [entry for entry in src if filter_entry (entry , scope )]
2121
2222
2323def country_filter (src , scopes ):
2424 """
25- Either make multiple data searches or
26- execute one. {NEEDS IMPROVEMENT, O(kN) => O(n)}
25+ Filter data by country(ies) with improved efficiency and no duplicates
2726
2827 :arg src: source dictionary
29- :arg scopes: source selectors
28+ :arg scopes: source selectors (string or list)
3029 """
31- out = []
32-
33- if type (scopes ) is list :
34- [out .extend (_country_filter (src , scope , out )) for scope in scopes ]
35- else :
36- out = _country_filter (src , scopes , out )
37-
38- return out
30+ if not scopes : # Handle empty scopes
31+ return src
32+
33+ if isinstance (scopes , str ):
34+ return _country_filter (src , scopes , None )
35+
36+ if isinstance (scopes , list ):
37+ if not scopes : # Empty list
38+ return src
39+
40+ # Convert all scopes to lowercase for case-insensitive comparison
41+ normalized_scopes = [scope .lower () for scope in scopes ]
42+
43+ # Filter entries where country matches any scope (case-insensitive)
44+ filtered_entries = [
45+ entry for entry in src
46+ if entry ["country" ].lower () in normalized_scopes
47+ ]
48+
49+ # Sort by country name for consistent ordering
50+ return sorted (filtered_entries , key = lambda x : x ["country" ])
51+
52+ return []
3953
4054
4155def main ():
4256 args = []
4357 temp_arg = ""
4458 first_word = True
4559
46- # Retrieve our selecting countries (seperated by commas)
60+ # Retrieve our selecting countries (separated by commas)
4761 for arg in sys .argv [1 :]:
4862 temp_arg += arg if first_word else " " + arg
4963 first_word = False
@@ -73,4 +87,4 @@ def main():
7387
7488
7589if __name__ == "__main__" :
76- main ()
90+ main ()
0 commit comments