I have a function that will do various regex-matches to find out which courier belongs to the provided tracking-id.
Right now, I'm using many elif statements which works fine but I got the feeling that this isn't very scalable in terms of maintanance.
import re
def get_courier(tracking_id):
""" Retrieves the courier from a given tracking-id """
if re.match(r'^ABC\d*$', f'{tracking_id}'):
courier = "Courier 1"
elif re.match(r'^\d*-\d$', f'{tracking_id}'):
courier = "Courier 2"
elif re.match(r'^PREF\d*$', f'{tracking_id}'):
courier = "Courier 3"
# ...
else:
courier = None
return courier
Would there be a possibility to maybe "outsource" the patterns into a file or database where I can define the patterns and their related courier?
I can't get my head around a solution that could work like this
import re
def get_courier(tracking_id):
""" Retrieves the courier from a given tracking-id """
patterns = database_select_function()
courier = re.match(patterns, tracking_id)
return courier
tracking_id?tracking_idwill be an (alphanumeric) string likeABC123456or123-456