| Reply | « Previous Thread | Next Thread » |
|
Are there any ways of cleaning up my code?
Does anyone have any spam expertise to pass on? http://reed.cs.depaul.edu/lperkovic/...orks/Lab3.html import urllib,re,urlparse def links(url): hyper=urllib.urlopen(url).read() pattern='href="[\w]*[\w.@_+\./-:]+"' l = re.findall(pattern,hyper) box2 = [] for i in l: if 'mailto:' not in i: if url not in i: box2.append(urlparse.urljoin(url,i[6:-1])) else: box2.append(i[6:-1]) return box2 def mailto(url): html=urllib.urlopen(url).read() pattern = "[\.\w]+@[\.\w]+" return re.findall(pattern, html) spam_dict = {} def spam(url, n): global spam_dict listlinks = links(url) listmailto = mailto(url) for i in listlinks: for j in listmailto: spam_dict[j]=i if n>0: spam(i,n-1) def test(filename): infile = open(filename) text = infile.read() print "Exercise i: words that end with 'ing'" print re.findall('.*ing',text) print "Exercise ii: words that have 3 'e's" print re.findall('.*e.*e.*e.*',text) print "Exercise iii: words with two occurences of substring 'ab'" print re.findall('.*ab.*ab.*',text) print "Exercise v: words with two or more 'e' characters followed by a repetition of one or more 's' characters" print re.findall('.*e.*e.*s+.*',text) infile.close() def analyze(filename): infile = open(filename) text = infile.read() pattern = "[A-Z][a-z]*" words= re.findall(pattern, text) d={} for word in words: if word in d: d[word] += 1 else: d[word] = 1 for word in d: print word, 'appeared', d[word], 'times' def analyze2(filename): infile = open(filename,'r') text = infile.read() infile.close() pattern = "[A-Z]+[a-z]+" l = re.findall(pattern,text) dict = {} for i in l: if i not in dict: dict[i]=1 else: dict[i]+=1 d = dict.keys() d.sort() for i in d: if dict[i]==1: print i, "appears",dict[i],'Time' else: print i, "appears",dict[i],'Times' |
| snakecharmer |
| View Public Profile |
| Find all posts by snakecharmer |
|
Join Date: Feb 2008
Posts: 2,543
Location: Bhavnagar, Gujarat, India
gaba88
Online
Forum Nokia Champion
|
|
hello snakecharmer
welcome to forum nokia python dibo ![]() i will advice you to use the code tags(#) provided in your editor tool so that we can have a better look at the code. In the way you have posted we are getting nothing. Enjoy Pythoning Gaba88 Gargi Das- http://gargidas.blogsot.com Forum Nokia Python Wiki Learn Python at http://mobapps.org/PyS60 |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Rate This Thread | |