<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7655.11">
<TITLE>Please critique my script</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->
<P><FONT SIZE=2 FACE="Arial">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.</FONT></P>
<P><FONT SIZE=2 FACE="Arial">I'd love to hear comments on how I could improve this code, what would be good to turn into a function/class etc.</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial"># The function of this script is to gather user data from the user to</FONT>
<BR><FONT SIZE=2 FACE="Arial"># build appropriate local dial-peers in a cisco voice gateway.</FONT>
<BR><FONT SIZE=2 FACE="Arial"># Data gathered from the user:</FONT>
<BR><FONT SIZE=2 FACE="Arial"># name of file to write dial-peers to</FONT>
<BR><FONT SIZE=2 FACE="Arial"># NPA</FONT>
<BR><FONT SIZE=2 FACE="Arial"># NXX</FONT>
<BR><FONT SIZE=2 FACE="Arial"># outbound port to send calls</FONT>
<BR><FONT SIZE=2 FACE="Arial"># question if home npa local calls are 7 digit</FONT>
<BR><FONT SIZE=2 FACE="Arial"># script navigates to nanpa.com, gathers the local calling area</FONT>
<BR><FONT SIZE=2 FACE="Arial"># and returns the data</FONT>
<BR><FONT SIZE=2 FACE="Arial"># the data is converted to beautiful soup (I had a problem with the xml format)</FONT>
<BR><FONT SIZE=2 FACE="Arial"># the data is parsed for the proper NPA/NXXs</FONT>
<BR><FONT SIZE=2 FACE="Arial"># list is sorted, duplicates removed</FONT>
<BR><FONT SIZE=2 FACE="Arial"># data from list is formatted and printed to the file specified</FONT>
</P>
<BR>
<P><FONT SIZE=2 FACE="Arial">#--------Import dependencies-----------</FONT>
<BR><FONT SIZE=2 FACE="Arial">import urllib2</FONT>
<BR><FONT SIZE=2 FACE="Arial">from BeautifulSoup import BeautifulSoup</FONT>
<BR><FONT SIZE=2 FACE="Arial">import re</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">#--------Define variables------------</FONT>
<BR><FONT SIZE=2 FACE="Arial">count = 0</FONT>
<BR><FONT SIZE=2 FACE="Arial">count2 = 0</FONT>
<BR><FONT SIZE=2 FACE="Arial">count3 = 0</FONT>
<BR><FONT SIZE=2 FACE="Arial">npalist = []</FONT>
<BR><FONT SIZE=2 FACE="Arial">nxxlist = []</FONT>
<BR><FONT SIZE=2 FACE="Arial">sortlist = []</FONT>
<BR><FONT SIZE=2 FACE="Arial">sortedlist = []</FONT>
<BR><FONT SIZE=2 FACE="Arial">npaReg = re.compile('(?<=<npa>)(...)(?=</npa>)')</FONT>
<BR><FONT SIZE=2 FACE="Arial">nxxReg = re.compile('(?<=<nxx>)(...)(?=</nxx>)')</FONT>
<BR><FONT SIZE=2 FACE="Arial">proxy = urllib2.ProxyHandler({'http': 'proxy.com:8080'})</FONT>
<BR><FONT SIZE=2 FACE="Arial"># --actual proxy was removed for confidentiality--</FONT>
<BR><FONT SIZE=2 FACE="Arial">opener = urllib2.build_opener(proxy)</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">#----- function to concatenate numbers - thanks to Chris Angelico -----</FONT>
<BR><FONT SIZE=2 FACE="Arial">def combine(list_of_numbers, position):</FONT>
<BR><FONT SIZE=2 FACE="Arial"> lastprefix=tails=lastsuffix=None</FONT>
<BR><FONT SIZE=2 FACE="Arial"> result=[]</FONT>
<BR><FONT SIZE=2 FACE="Arial"> for cur in list_of_numbers:</FONT>
<BR><FONT SIZE=2 FACE="Arial"> prefix=cur[:position]; tail=cur[position]; suffix=cur[position+1:]</FONT>
<BR><FONT SIZE=2 FACE="Arial"> if prefix!=lastprefix or suffix!=lastsuffix:</FONT>
<BR><FONT SIZE=2 FACE="Arial"> if lastprefix!=None:</FONT>
<BR><FONT SIZE=2 FACE="Arial"> if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))</FONT>
<BR><FONT SIZE=2 FACE="Arial"> else: result.append(lastprefix+tails+lastsuffix)</FONT>
<BR><FONT SIZE=2 FACE="Arial"> lastprefix,tails,lastsuffix=prefix,"",suffix</FONT>
<BR><FONT SIZE=2 FACE="Arial"> tails+=tail</FONT>
<BR><FONT SIZE=2 FACE="Arial"> if lastprefix!=None:</FONT>
<BR><FONT SIZE=2 FACE="Arial"> if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))</FONT>
<BR><FONT SIZE=2 FACE="Arial"> else: result.append(lastprefix+tails+lastsuffix)</FONT>
<BR><FONT SIZE=2 FACE="Arial"> return result</FONT>
</P>
<BR>
<P><FONT SIZE=2 FACE="Arial">#-------Gather info from user--------</FONT>
<BR><FONT SIZE=2 FACE="Arial">x = raw_input("please enter a filename: ")</FONT>
<BR><FONT SIZE=2 FACE="Arial">y = raw_input("please enter the npa: ")</FONT>
<BR><FONT SIZE=2 FACE="Arial">z = raw_input("please enter the nxx: ")</FONT>
<BR><FONT SIZE=2 FACE="Arial">p = raw_input("please enter the port: ")</FONT>
<BR><FONT SIZE=2 FACE="Arial">q = raw_input("Is home npa local dialing 7 digit?(y/n) ")</FONT>
<BR><FONT SIZE=2 FACE="Arial">#print x</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">#-------Modify user data-------------</FONT>
<BR><FONT SIZE=2 FACE="Arial">o = open(x, 'w')</FONT>
<BR><FONT SIZE=2 FACE="Arial">y = str(y)</FONT>
<BR><FONT SIZE=2 FACE="Arial">z = str(z)</FONT>
<BR><FONT SIZE=2 FACE="Arial">p = str(p)</FONT>
<BR><FONT SIZE=2 FACE="Arial">pagedef = ("</FONT><A HREF="http://www.localcallingguide.com/xmllocalprefix.php?npa="><U><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">http://www.localcallingguide.com/xmllocalprefix.php?npa=</FONT></U></A><FONT SIZE=2 FACE="Arial">" + y + "&nxx=" + z)</FONT>
<BR><FONT SIZE=2 FACE="Arial">print "Querying", pagedef</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">#------Get info from NANPA.com ----------</FONT>
<BR><FONT SIZE=2 FACE="Arial">urllib2.install_opener(opener)</FONT>
<BR><FONT SIZE=2 FACE="Arial">page = urllib2.urlopen(pagedef)</FONT>
<BR><FONT SIZE=2 FACE="Arial">soup = BeautifulSoup(page)</FONT>
<BR><FONT SIZE=2 FACE="Arial">soup = str(soup)</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">#------Parse Gathered Data----------</FONT>
<BR><FONT SIZE=2 FACE="Arial">for line in npaReg.findall(soup):</FONT>
<BR> <FONT SIZE=2 FACE="Arial">npalist.insert(count,line)</FONT>
<BR> <FONT SIZE=2 FACE="Arial">count = count + 1</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">for line2 in nxxReg.findall(soup):</FONT>
<BR> <FONT SIZE=2 FACE="Arial">nxxlist.insert(count2,line2)</FONT>
<BR> <FONT SIZE=2 FACE="Arial">count2 = count2 + 1</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">#-----Sort, remove duplicates, concatenate the last digits for similiar NPA/NXX ------</FONT>
<BR><FONT SIZE=2 FACE="Arial">for makenewlist in range(0,count):</FONT>
<BR><FONT SIZE=2 FACE="Arial"> sortlist.append(npalist.pop(0) + nxxlist.pop(0))</FONT>
<BR><FONT SIZE=2 FACE="Arial"> </FONT>
<BR><FONT SIZE=2 FACE="Arial">sortlist.sort()</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">for sortednumber in sortlist:</FONT>
<BR> <FONT SIZE=2 FACE="Arial">if sortednumber not in sortedlist:</FONT>
<BR> <FONT SIZE=2 FACE="Arial">sortedlist.append(sortednumber)</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">catlist = combine(sortedlist,5)</FONT>
<BR><FONT SIZE=2 FACE="Arial">catlist2 = combine(catlist,4)</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">#------Print the dial-peers to file----</FONT>
<BR><FONT SIZE=2 FACE="Arial">for line in catlist2:</FONT>
<BR><FONT SIZE=2 FACE="Arial"> figureDpn = count3 + 1000</FONT>
<BR><FONT SIZE=2 FACE="Arial"> dpn = str(figureDpn)</FONT>
<BR><FONT SIZE=2 FACE="Arial"> label = "dial-peer voice " + dpn</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write(label)</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write('\n')</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write("description *** local outbound dialpeer ***")</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write('\n')</FONT>
<BR><FONT SIZE=2 FACE="Arial"> destpatt = "destination-pattern %s...." % line.rstrip()</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write(destpatt)</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write('\n')</FONT>
<BR><FONT SIZE=2 FACE="Arial"> port = "port " + p</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write(port)</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write('\n')</FONT>
<BR><FONT SIZE=2 FACE="Arial"> if line[0:3] == y and q == "y":</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write("forward-digits 7")</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write('\n')</FONT>
<BR><FONT SIZE=2 FACE="Arial"> o.write('\n')</FONT>
<BR><FONT SIZE=2 FACE="Arial"> count3 = count3 + 1</FONT>
<BR><FONT SIZE=2 FACE="Arial"> </FONT>
<BR><FONT SIZE=2 FACE="Arial">o.close()</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">#-------------------------------------------</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">Thanks!</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">Edward Ellerbee</FONT>
</P>
<BR>
<BR>
</BODY>
</HTML>