I'm building a web app with the purpose to register all the calls made by users. A call can have a service, number called and a cost. The code provided below is currently working (with the numbers turned into regexes), but it's somewhat of a mess and I would like to optimize it. Since this code requires to be exactly in this order to work, when I used a dictionary to store the regex expressions and services, it didn't work. Here is my current code with the regex used:
if (service=='R'):
return "ROAMING"
elif (service=='V O') or ((service=='S') and (float(cost)==0.0)):
return "ONNET"
elif (service=="") and (nr_called==""):
return "INTERNET"
elif (service=='I')or (service=='ROAMING - MMS'):
return "OTHERSERV"
elif (service=='Internet') or (service=='WAP') or service==('BLACKBERRY.NET') or (service=='ROAMING - INTERNET')or (service=='ROAMING - BLACKBERRY'):
return "INTERNET"
elif ((nr_called[:6]=="003516" or nr_called[:6]=="003514" or nr_called[:6]=="003511" or nr_called[:6]=="003517" or nr_called[:6]=="003518") or (nr_called[0]=="6" or nr_called[0]=="4" or nr_called[0]=="1" or nr_called[0]=="7" or nr_called[0]=="8")) and (service!="V O"):
return "OTHERSERV"
elif (service=='Vi F') or (service=='Si'):
return "INTERNATIONAL"
elif (nr_called[0]=="9" or nr_called[:6]=="003519") and (service=="Vp F") and (float(cost)>0) :
return "INS"
elif (nr_called[:9]=="003519220" or nr_called[:8]=="00351924" or nr_called[:8]=="00351925" or nr_called[:8]=="00351926" or nr_called[:8]=="00351927" or nr_called[:4]=="9220" or nr_called[:3]=="924" or nr_called[:3]=="925" or nr_called[:3]=="926" or nr_called[:3]=="927") :
return "INS"
elif ((len(nr_called)==9) and nr_called[:2]=="96") or (nr_called[:7]=="0035196"):
return "INS"
elif (nr_called[:3]=="921" or nr_called[:8]=="00351921"):
return "91"
elif (nr_called[:3]=="929" or nr_called[:8]=="00351929"):
return "93"
elif (nr_called[:3]=="922" or nr_called[:8]=="00351922"):
return "OTHERSERV"
elif (service!="Vp F") and ((nr_called[:2]=="96" ) or(nr_called[:7]=="0035196")):
return "INS"
elif (len(nr_called)==7) or (service=='V O'):
return "ONNET"
elif ((len(nr_called)==9) and nr_called[:2]=="91") or (nr_called[:7]=="0035191"):
return "91"
elif ((len(nr_called)==9) and nr_called[:2]=="93") or (nr_called[:7]=="0035193"):
return "93"
elif ((len(nr_called)==9) and nr_called[:1]=="2") or (nr_called[:5]=="00352"):
return "PT"
elif float(cost)>0:
return "OTHERSERV"
else:
return "OTHERSERV"
Number regexes:
OTHERSERV: ['(\+*0*351)?922','(\+*0*351)?[146-8]']
96: ['(\+*0*351)?92([4-7|20])','(\+*0*351)?96']
93: ['(\+*0*351)?929','(\+*0*351)?93']
91: ['(\+*0*351)?921','(\+*0*351)?91']
PT: ['(\+*0*351)?2']
I have been dwelling with the optimization for quite a while and can't figure out how can I structure this in an optimized and maintainable way, so any help is very appreciated.