Skip to content
Open
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
41 changes: 27 additions & 14 deletions filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,53 @@ 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 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
]

if type(scopes) is list:
[out.extend(_country_filter(src, scope, out)) for scope in scopes]
else:
out = _country_filter(src, scopes, out)
# Sort by country name for consistent ordering
return sorted(filtered_entries, key=lambda x: x["country"])

return out
return []


def main():
args = []
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
Expand Down