Please critique my script
Ellerbee, Edward
EEllerbee at BBandT.com
Thu Jul 14 14:03:59 EDT 2011
I've been working on this for 3 weeks as a project while I'm learning
python. It's ugly, only function in there is from a fellow lister, but
it works perfectly and does what it is intended to do.
I'd love to hear comments on how I could improve this code, what would
be good to turn into a function/class etc.
# The function of this script is to gather user data from the user to
# build appropriate local dial-peers in a cisco voice gateway.
# Data gathered from the user:
# name of file to write dial-peers to
# NPA
# NXX
# outbound port to send calls
# question if home npa local calls are 7 digit
# script navigates to nanpa.com, gathers the local calling area
# and returns the data
# the data is converted to beautiful soup (I had a problem with the xml
format)
# the data is parsed for the proper NPA/NXXs
# list is sorted, duplicates removed
# data from list is formatted and printed to the file specified
#--------Import dependencies-----------
import urllib2
from BeautifulSoup import BeautifulSoup
import re
#--------Define variables------------
count = 0
count2 = 0
count3 = 0
npalist = []
nxxlist = []
sortlist = []
sortedlist = []
npaReg = re.compile('(?<=<npa>)(...)(?=</npa>)')
nxxReg = re.compile('(?<=<nxx>)(...)(?=</nxx>)')
proxy = urllib2.ProxyHandler({'http': 'proxy.com:8080'})
# --actual proxy was removed for confidentiality--
opener = urllib2.build_opener(proxy)
#----- function to concatenate numbers - thanks to Chris Angelico -----
def combine(list_of_numbers, position):
lastprefix=tails=lastsuffix=None
result=[]
for cur in list_of_numbers:
prefix=cur[:position]; tail=cur[position];
suffix=cur[position+1:]
if prefix!=lastprefix or suffix!=lastsuffix:
if lastprefix!=None:
if len(tails)>1:
result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))
else: result.append(lastprefix+tails+lastsuffix)
lastprefix,tails,lastsuffix=prefix,"",suffix
tails+=tail
if lastprefix!=None:
if len(tails)>1:
result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))
else: result.append(lastprefix+tails+lastsuffix)
return result
#-------Gather info from user--------
x = raw_input("please enter a filename: ")
y = raw_input("please enter the npa: ")
z = raw_input("please enter the nxx: ")
p = raw_input("please enter the port: ")
q = raw_input("Is home npa local dialing 7 digit?(y/n) ")
#print x
#-------Modify user data-------------
o = open(x, 'w')
y = str(y)
z = str(z)
p = str(p)
pagedef = ("http://www.localcallingguide.com/xmllocalprefix.php?npa=" +
y + "&nxx=" + z)
print "Querying", pagedef
#------Get info from NANPA.com ----------
urllib2.install_opener(opener)
page = urllib2.urlopen(pagedef)
soup = BeautifulSoup(page)
soup = str(soup)
#------Parse Gathered Data----------
for line in npaReg.findall(soup):
npalist.insert(count,line)
count = count + 1
for line2 in nxxReg.findall(soup):
nxxlist.insert(count2,line2)
count2 = count2 + 1
#-----Sort, remove duplicates, concatenate the last digits for similiar
NPA/NXX ------
for makenewlist in range(0,count):
sortlist.append(npalist.pop(0) + nxxlist.pop(0))
sortlist.sort()
for sortednumber in sortlist:
if sortednumber not in sortedlist:
sortedlist.append(sortednumber)
catlist = combine(sortedlist,5)
catlist2 = combine(catlist,4)
#------Print the dial-peers to file----
for line in catlist2:
figureDpn = count3 + 1000
dpn = str(figureDpn)
label = "dial-peer voice " + dpn
o.write(label)
o.write('\n')
o.write("description *** local outbound dialpeer ***")
o.write('\n')
destpatt = "destination-pattern %s...." % line.rstrip()
o.write(destpatt)
o.write('\n')
port = "port " + p
o.write(port)
o.write('\n')
if line[0:3] == y and q == "y":
o.write("forward-digits 7")
o.write('\n')
o.write('\n')
count3 = count3 + 1
o.close()
#-------------------------------------------
Thanks!
Edward Ellerbee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110714/79dc3c27/attachment.html>
More information about the Python-list
mailing list