<br><br><div class="gmail_quote">On Wed, Oct 5, 2011 at 1:42 PM, Dave Angel <span dir="ltr">&lt;<a href="mailto:d@davea.name">d@davea.name</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On 10/04/2011 11:13 PM, lina wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
On Wed, Oct 5, 2011 at 10:45 AM, Dave Angel&lt;<a href="mailto:d@davea.name" target="_blank">d@davea.name</a>&gt;  wrote:<br>
<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
On 10/04/2011 10:22 PM, lina wrote:<br>
<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Wed, Oct 5, 2011 at 1:30 AM, Prasad, Ramit&lt;ramit.prasad@jpmorgan.**<u></u>com&lt;<a href="mailto:ramit.prasad@jpmorgan.com" target="_blank">ramit.prasad@jpmorgan.com</a>&gt;<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
w<br>
</blockquote></blockquote>
&lt;SNIP&gt;<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
SyntaxError: invalid syntax<br>
<br>
for fileName in os.listdir(&quot;.&quot;):<br></div>
     if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=<u></u>**<div><div></div><div class="h5"><br>
=&quot;.xpm&quot;:<br>
         filedata = open(fileName)<br>
         text=filedata.readlines()<br>
         cols = len(text[0])<br>
         except IndexError:<br>
             print (&quot;Index Error.&quot;)<br>
         result=[]<br>
         for idx in xrange(cols):<br>
             results.append(0)<br>
         for line in text:<br>
             for col_idx, field in enumerate(line):<br>
                 if token in field:<br>
                     results[col_idx]+=1<br>
             for index in col_idx:<br>
                 print results[index]<br>
<br>
it showed up:<br>
<br>
     print results[]<br>
                 ^<br>
SyntaxError: invalid syntax<br>
<br>
Sorry, I am still lack deep understanding about something basic. Thanks<br>
for<br>
your patience.<br>
<br>
<br>
  Simplest answer here is you might have accidentally run this under Python<br>
</div></div></blockquote><div><div></div><div class="h5">
3.x.  That would explain the syntax error on the print function.   Pick a<br>
single version and stick to it.  In fact, you might even put a version test<br>
at the beginning of the code to give an immediate error.<br>
<br>
</div></div></blockquote>
choose python3.<br>
<br>
</blockquote>
Then change that last print to use parentheses.  print() is a function call in Python 3.x, while it was a statement in earlier Python versions.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
&lt;SNIP&gt;<div class="im"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This example illustrates one reason why it&#39;s a mistake to write all the<br>
code at top level.  This code should probably be at least 4 functions, with<br>
each one handling one abstraction.<br>
<br>
</blockquote>
It&#39;s frustrating. Seriously. (I think I need to read some good (relevant)<br>
codes first.<br>
<br>
</div></blockquote>
Is Python your first programming language?  It was approximately my 30th.<br></blockquote><div>Not exactly. Ha ... I don&#39;t know there are so many languages there. <br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<br>
I learned &quot;programming&quot; from a Fortran book in 1967.  I had no access to a computer, though there was at least one in the state, at the Yale campus.  I saw it in a field trip by the (advanced) students that were taking programming.  They weren&#39;t allowed to take it till finishing 2nd year calculus, which I didn&#39;t do till I got to college.  However, when I went to college the following year, I ran across another student who knew how to access the mainframe (via punch-cards), and could tell me how to do it.  (Security was very light).  For a few months, I hacked daily, and learned a lot.  Then the following year, I actually took an electrical engineering class that introduced the concepts of programming, and I spent my time doing experiments that barely resembled the assignments.  I ended up with an incomplete in the course, which I made up by writing a linear circuit analysis program.  Punched card input, graphical output to a line printer using rows of asterisks.<br>
</blockquote><div>How to start, I learned C 10 years ago, but for whole semester, I never wrote a serious program, but indeed attended every lecture. <br>At that time, I was addicted literature staff. But later realized that lots of writers (especially the ones I like)  ended up with committing suicide, something to heavy to handle, so I changed to something like physics, I noted lots of people doing physics living really long and happy (long living the physicist), then four years as (applied) physics, three years as (theoretical) physics, then (bio-) physics in the following years. (It&#39;s a joke). <br>
During those years used maple, matlab and some basic awk, bash. but all is very basic. shame...did not do something seriously.<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<br>
Point is, it takes a lot of time, and usually a one-on-one mentor to get the concepts nailed down.  Seldom did anyone tell me &quot;write these lines down, and it&#39;ll solve the problem.&quot;  instead they told me where my problem was, and where in those manuals (chained to tables in the lab) to find more information.<br>

<br>
It wasn&#39;t till my fourth language that I found out about local variables, and how a function should encapsulate one concept.  The first three didn&#39;t have such things.<div><div></div><div class="h5"><br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Further, while you&#39;re developing, you should probably put the test data<br>
into a literal (probably a multiline literal using triplequotes), so you can<br>
experiment easily with changes to the data, and see how it results.<br>
<br>
</blockquote>
<br>
  #!/bin/python<br>
<br>
import os.path<br>
<br>
tokens=[&#39;B&#39;,&#39;E&#39;]<br>
<br>
for fileName in os.listdir(&quot;.&quot;):<br>
     if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=<u></u>=&quot;.xpm&quot;:<br>
         filedata = open(fileName)<br>
         text=filedata.readlines()<br>
         results={}<br>
         numcolumns=len(text.strip())<br>
         for ch in tokens:<br>
             results[ch]=[0]*numcolumns<br>
         for line in text:<br>
             for col, ch in enumerate(line):<br>
                 if ch in tokens:<br>
                     results[ch][col]+=1<br>
         for item in results:<br>
                 print item<br>
<br>
$ python3 counter-vertically.py<br>
   File &quot;counter-vertically.py&quot;, line 20<br>
     print item<br>
              ^<br>
SyntaxError: invalid syntax<br>
<br>
</blockquote></div></div>
As I said above, Python 3 needs parentheses around print&#39;s argument list.<br>
<br>
As for splitting into functions, consider:<br>
<br></blockquote><div> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
#these two are capitalized because they&#39;re intended to be constant<br>
TOKENS = &quot;BE&quot;<br>
LINESTOSKIP = 43<br>
INFILEEXT = &quot;.xpm&quot;<br>
OUTFILEEXT = &quot;.txt&quot;<br>
<br>
def dofiles(topdirectory):<br>
    for filename in os.listdr(topdirectory):<br>
        processfile(filename)<br>
<br>
def processfile(infilename):<br>
    base, ext =os.path.splitext(fileName)<br>
    if ext == INFILEEXT:<br>
        text = fetchonefiledata(infilename)<br>
        numcolumns = len(text[0])<br>
        results = {}<br>
        for ch in TOKENS:<div class="im"><br>
            results[ch] = [0] * numcolumns<br></div>
        for line in text:<br>
            line = line.strip()<div class="im"><br>
            for col, ch in enumerate(line):<br>
                if ch in tokens:<br>
                    results[ch][col] += 1<br></div>
        writeonefiledata(base+<u></u>OUTFILEEXT, results)<br>
<br>
def fetchonefiledata(inname):<br>
    infile = open(inname)<br>
    text = infile.readlines()<br>
    return text[LINESTOSKIP:]<br>
<br>
def writeonefiledata(outname):<br>
    outfile = open(outname, &quot;w&quot;)<br>
    ...process the results as appropriate...<br>
    ....(since you didn&#39;t tell us how multiple tokens were to be displayed)<br>
<br>
if __name__ == &quot;__main__&quot;:<br>
    dofiles(&quot;.&quot;)     #or get the top directory from the sys.argv variable, which is set from command line.<br>
<br>
<br></blockquote><div>You dissect the former one you suggested before into 4 functions. <br>a little question, why choose .ext? why the splitext is also ext here?<br><br> <br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

Now this is totally untested.  I just typed it without even trying any of it.  </blockquote><div><br><br>import os.path<br><br><br>TOKENS=&quot;E&quot;<br>LINESTOSKIP=0<br>INFILEEXT=&quot;.xpm&quot;<br>OUTFILEEXT=&quot;.txt&quot;<br>
<br>def dofiles(topdirectory):<br>    for filename in os.listdir(topdirectory):<br>        processfile(filename)<br>        <br>def processfile(infilename):<br>    base, ext =os.path.splitext(infilename)<br>    if ext == INFILEEXT:<br>
        text = fetchonefiledata(infilename)<br>        numcolumns=len(text[0])<br>        results={}<br>        for ch in TOKENS:<br>        <br>            results[ch] = [0]*numcolumns<br>        for line in text:<br>            line = line.strip()<br>
            <br>            for col, ch in enumerate(line):<br>                if ch in TOKENS:<br>                    results[ch][col]+=1<br>        writeonefiledata(base+OUTFILEEXT,results)<br>        <br>def fetchonefiledata(inname):<br>
    infile = open(inname)<br>    text = infile.readlines()<br>    return text[LINESTOSKIP:]<br>    <br>def writeonefiledata(outname,results):<br>    outfile = open(outname,&quot;w&quot;)<br>    for item in results:<br>        return outfile.write(item)<br>
    <br>    <br>if __name__==&quot;__main__&quot;:<br>    dofiles(&quot;.&quot;)<br><br>just the results is a bit unexpected.<br><br> $ more try.txt <br>E<br><br>I might make a mistake in the writeonefiledata your left part.<br>
<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">But it gives you a simple refactoring that splits the logic so each can be visualized (and tested) independently.  i&#39;d also split up processfile(), once I realized how big it was.<br>

<br>
There are many shortcuts that can be applied. Some of them probably use language features you&#39;re not comfortable with, like perhaps generators.  And if  efficiency is important, there are optimizations to do, like using islice directly on the infile object.  That one would eliminate having to have the whole file stored in memory at one time.<br>

<br>
Likewise there are further things that could be done to decouple the functions even more.<br>
<br>
But there&#39;s nothing in the above code which uses very advanced topics, so you should be able to understand it and fix whatever typos I&#39;ve undoubtedly got.<br>
<br>
What are you using for debugging aids?  Besides this group, I mean.  print statements?  An IDE ?  which one?<br></blockquote><div>debugging aids?<br>I just run python3 script.py<br>it will pop up some hints, <br>in the middle, probably try print.<br>
<br>Thanks for your time,<br> <br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
 --<br>
<br>
DaveA<br>
<br>
</blockquote></div><br><br clear="all"><br>-- <br>Best Regards,<br><br>lina<br><br><br>