By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,173 Members | 796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,173 IT Pros & Developers. It's quick & easy.

Some source code

P: n/a
For those that like to read source code, or just wondered how a C programmer
would use Python I post some code written by a friend which I pestered to at
least try Python out. He did not seem too impressed after the exercise, but said
it was not that painfull :-).

It might be of interest to those wondering how python is being used and misused
:-). I guess teachers using Python to teach can confirm or deny the benefits of
certain language decisions daily by looking at the source code of their
students.

Anyway here we go:

#
# Reading File and loading it

#Hashing a string with a specified separator
# and storing results in an Array
#
def HashString(str,sep):
lim = len(str)
array=[]
indexes=[]
cptr =0
for i in range(lim):
if str[i]== sep and i < lim:
indexes.append(i)
cptr+=1

lim = len(indexes)
cptr =j=0
for i in range(lim-1):
if(str[j] == sep and j ==0) :
j+=1
cptr+=1
array.append(str[j:indexes[cptr]])
j=indexes[cptr]+1
cptr+=1

return array;
#
# End of HashString Function
#

File= open('input.txt','r')
content=[]
content = File.readlines()
sum =0
Result = []
OFile = open('output.txt','w')
for i in range(len(content)):
result = HashString(content[i],'\"')
cptr=sum = 0
for j in range(len(result)):
sum += int(result[cptr])
if(j>0):
OFile.write(",")
OFile.write("\""+result[cptr]+"\"")
cptr+=2
if cptr >= len(result) : break;
OFile.write(",\""+sum.__str__()+"\"\n")
result.append(sum)
print result
#
# Creating Ouput file
#
OFile.close();
File.close();
print "\n....\tdone view output in \'output.txt\'.......\n"
#######################################

Roughly does this:

file('output.txt','w').writelines(['%s,"%s"\n' % (line[:-1],
sum(map(int,line[1:-2].split('","')))) for line in
file('input.txt')])
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I like the clever use of 'cptr' -- that would be silly in any language.
Jul 18 '05 #2

P: n/a

nnes <pr*******@latinmail.com> wrote:
file('output.txt','w').writelines(['%s,"%s"\n' % (line[:-1],
sum(map(int,line[1:-2].split('","')))) for line in
file('input.txt')])

file('output.txt','w').writelines(['%s,"%s"\n' %
(line[:-1],sum(eval(line[1:-2]))) for line in file('input.txt')])
Regards
Adam Przybyla
Jul 18 '05 #3

P: n/a
Adam Przybyla <ad**@gliwice.pl> pisze:
file('output.txt','w').writelines(['%s,"%s"\n' % (line[:-1],
sum(map(int,line[1:-2].split('","')))) for line in
file('input.txt')])

file('output.txt','w').writelines(['%s,"%s"\n' %
(line[:-1],sum(eval(line[1:-2]))) for line in file('input.txt')])


You still call this code "pythonic"?

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #4

P: n/a
Adam Przybyla <ad**@gliwice.pl> wrote in message news:<c1**********@atlantis.news.tpi.pl>...
nnes <pr*******@latinmail.com> wrote:
file('output.txt','w').writelines(['%s,"%s"\n' % (line[:-1],
sum(map(int,line[1:-2].split('","')))) for line in
file('input.txt')])

file('output.txt','w').writelines(['%s,"%s"\n' %
(line[:-1],sum(eval(line[1:-2]))) for line in file('input.txt')])
Regards
Adam Przybyla

You mean:

file('output.txt','w').writelines(['%s,"%s"\n' % (line[:-1],
sum(eval(line[1:-2].replace('"','')))) for line in file('input.txt')])

input.txt in the form of:
"1","2","3"
"4","5","6"

And both versions are exactly 132 bytes long. It is a draw :-P
Jul 18 '05 #5

P: n/a
> You still call this code "pythonic"?

May as well do it in Perl, where it will be shorter and even more
unreadable!
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.