You Are Here:

Community: Developer Discussion Boards

#1 Old pyton simplification - 2009-01-26, 05:27

Join Date: Jan 2009
Posts: 1
snakecharmer
Offline
Registered User
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'
Reply With Quote

#2 Old Re: pyton simplification - 2009-01-26, 05:34

Join Date: Feb 2008
Posts: 2,543
Location: Bhavnagar, Gujarat, India
Send a message via Yahoo to gaba88 Send a message via Skype™ to gaba88
gaba88's Avatar
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 With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia