Tim, Thank you for your suggestions that you made. I will modify my
class to what you said. I will also remove find_and_replace. seeing as
I won't use it anywhere else. I think I put it there for some test and
forgot to delete it. I was actually deleting the header outside of the
class. This works much better for me. Any other suggestions are
appreciated. Thank you. -A<br><br><div class="gmail_quote">On Sun, Dec 28, 2008 at 5:46 AM, Tim Roberts <span dir="ltr"><<a href="mailto:timr@probo.com">timr@probo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">"alex goretoy" <<a href="mailto:aleksandr.goretoy@gmail.com">aleksandr.goretoy@gmail.com</a>> wrote:<br>
><br>
</div><div class="Ih2E3d">>class parsercsvy(object):<br>
> """Return a line from a csv file or total amount of lines"""<br>
</div><div class="Ih2E3d">> def __init__(self,file_name=""):<br>
> self.func_me_color="white_on_black"<br>
> self.soc=stdout_colours.stdout_colors()<br>
> self.soc.me_him(['ENTER:',__name__],self.func_me_color)<br>
> self.filename = file_name<br>
> self.buffer = []<br>
> self.bufferp= []<br>
> if string.find(self.filename,"http") != -1:<br>
> resp=urllib2.urlopen(self.filename)<br>
> file=resp.read()<br>
> lfi=len(string.split(self.filename,"/"))<br>
> filename = "/tmp/"+string.split(self.filename,"/")[lfi-1]<br>
<br>
</div>Style issue: unless you are running Python 1.x, you virtually never need<br>
to import the "string" module. Also, you can always refer to the last<br>
element of a list or tuple by using [-1]:<br>
<br>
parts = self.filename.split( "/" )<br>
filename = "/tmp/" + parts[-1]<br>
<div class="Ih2E3d"><br>
<br>
> def parse(self,filename,ret=0):<br>
> self.soc.me_him(['ENTER:',__name__],self.func_me_color)<br>
> i = 0<br>
> try:<br>
</div><div class="Ih2E3d">> reader = csv.reader(file(filename, "rb"))<br>
> try:<br>
> for row in reader:<br>
> self.buffer.append(row)<br>
> s,a=[],{}<br>
><br>
</div><div class="Ih2E3d">> for j in range(len(self.buffer[0])):<br>
> a[self.buffer[0][j]]=row[j]<br>
> self.bufferp.append(a)<br>
> i+=1<br>
> self.total = i-1<br>
<br>
</div>You might consider keeping the header line separate.<br>
<br>
reader = csv.reader(open(filename, "rb"))<br>
header = reader.next()<br>
self.buffer = list(reader)<br>
self.bufferp = [ dict( zip( header, line ) ) for line in reader ]<br>
self.header = header<br>
<br>
Also, you don't really need a separate "total" variable, since it's equal<br>
to len(self.buffer).<br>
<div class="Ih2E3d"><br>
> def total(self):<br>
> """return total number of lines in csv file"""<br>
> self.soc.me_him(['ENTER:',__name__],self.func_me_color)<br>
> self.soc.me_him(['RETURN:',self.total,__name__],self.func_me_color)<br>
> return self.total<br>
<br>
</div>There's a problem here, as this was originally written. "self.total"<br>
starts out being a function (this one here). But after self.parse runs,<br>
"self.total" will be an integer, and this function is lost. You need to<br>
decide whether you want users to just access the self.total integer, or<br>
force them to use the function. In the latter case, you can change the<br>
counter to self._total.<br>
<br>
On the other hand, the self.total counter is unnecessary:<br>
def total(self):<br>
return len(self.buffer)<br>
<div class="Ih2E3d"><br>
> def find_and_replace(self,li,fi,re):<br>
> """<br>
> find and replace a string inside a string, return list<br>
> find_and_replace(list,find,replace)<br>
> """<br>
> this=[]<br>
> for l in li:<br>
># found_index=string.find(l,fi)<br>
> this.append(l.replace(fi,re))<br>
> return this<br>
<br>
def find_and_replace(self,li,fi,re):<br>
</div> return [l.replace(fi,re) for l in li]<br>
<br>
I'm not sure why this is a member of the class; it doesn't use any of the<br>
members.<br>
<font color="#888888">--<br>
Tim Roberts, <a href="mailto:timr@probo.com">timr@probo.com</a><br>
Providenza & Boekelheide, Inc.<br>
</font><div><div></div><div class="Wj3C7c">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я<br>а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я<br>