PDA

View Full Version : [Python]Ottimizzazione


Luc@s
06-05-2004, 09:03
Sto scrivendomi un news reader.
Ora sono arrivato a skarikare i body dei msg e gli heade+subject(poi li fondero assieme).
Pero volevo sapere se il mio code si puo ottimizzare un pochino.
Ecco il code:

import os
import sys
import socket
import nntplib
import string

__author__ = "Kleidemos (francesca8810@ti.it)"
__version__ = "$Revision: 0.2 $"
__date__ = "$Date: 2004/05/6 14:00:00 $"
__copyright__ = "Copyright (c) 2004 Luca Francesca"
__license__ = "Python"

kb = 8 * 1024 # a kb unit

#
# This class elaborate the NewsServerGestion downloaded files
#
class ArticleGestion:
nws = ''
article = []
def Close(self):
nws.quit()
# Load into a GROUP_article.txt file the body of the article specified
# into GROUP.txx file
# Because this requied many time you should use a modal dialog or any
# other mode (threads for example) to not block the application GUI
def ParseGroup(self, server, gname):
self.nws = nntplib.NNTP(server)
self.nws.group(gname)
self.fin = file(gname+"/"+gname+".txt", "r")
self.fout = file(gname+"/"+gname+"_article.txt", "w")
while 1:
self.line = self.fin.readline()
if not self.line:
self.fin.close()
self.fout.close()
break
self.str_tmp = string.split(self.line, "\t")
self.id = self.str_tmp[0]
for i in self.nws.body(self.id)[3]:
try:
self.fout.write(i+"\n")
except:
print 'Error in write for file',
gname+"/"+gname+"_article.txt", '\n'
#self.fout.write("\n")

self.fin.close()
self.fout.close()
# Merge the sebject-header file to the body file and build a group tree
# to show
# Because this requied many time you should use a modal dialog or any
# other mode (threads for example) to not block the application GUI
# TODO : 100%
def BuildGroupTree(self, group, farticle, fsubject):
pass


#
# This class download message from group and save it into a files
#
class NewsServerGestion:
#
__i = ''
#
__news = ''
__msg = []
#
__group_state = []
__beg = ''
__end = ''
#
__stats = ''
#single article
__single_article = []
__response = ''
__number = ''
__list = ''
#article range
__articles_header = []
articles_list = []
#article id
__article_id = []
def __init__(self, host):
# Open a server connection
self.news = nntplib.NNTP(host)
self.news.getwelcome()
# Send a article
def SendArticle(self, file):
f = open(file)
s.post(f)
# Download article id & header
# Because this requied many time you should use a modal dialog or any
# other mode (threads for example) to not block the application GUI
def DownloadArticle(self):
self.fname = self.group_state[4]+"/"+self.group_state[4]+".txt"
self.articles_list = self.news.xover('subject', self.beg + '-' + self.end, self.fname)
# Save group info
def PrintGroupInfo(self, gname):
if not os.path.exists(gname):
os.mkdir(gname)
self.group_state = self.news.group(gname)
self.stats = self.group_state[0] # set group info
self.beg = self.group_state[2] # start message
self.end = self.group_state[3] # end message
f = open(gname+"/"+gname+"_stat.txt", "w")
try:
f.write(self.stats)
except:
print 'Error in write stat files for', gname, 'group\n'
f.close()
def Close(self):
self.news.quit()


if __name__ == "__main__":

print "Test NewsServerGestione on ", sys.getwindowsversion()

n = NewsServerGestion("news.tin.it")

n.PrintGroupInfo("it.comp.lang.python")

n.DownloadArticle()

n.Close()

msg = ArticleGestion()

msg.ParseGroup("news.tin.it", "it.comp.lang.python")

msg.Close()



Tnk 1000000000000000000