Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Answer</h2> <p>Let's break it down into pieces. Especially the pieces you got wrong. :)</p> <hr> <h3>Assignment</h3> <blockquote> <pre><code>outfile=ReadsAgain.txt </code></pre> </blockquote> <p>It should come to little surprise that you need to put quotes around strings. On the other hand, you have the luxury of putting spaces around the <code>=</code> for readability.</p> <pre><code>outfilename = "ReadsAgain.txt" </code></pre> <hr> <h3>Variable expansion → <a href="http://docs.python.org/library/stdtypes.html#str.format" rel="nofollow noreferrer"><code>str.format</code></a> (or, the <a href="http://docs.python.org/library/stdtypes.html#string-formatting-operations" rel="nofollow noreferrer"><code>%</code></a> operation)</h3> <blockquote> <pre><code>python reads.py &lt;snip/&gt; -q$queries &lt;snip/&gt; </code></pre> </blockquote> <p>So you know how to do the redirection already, but how do you do the variable expansion? You can use <a href="http://docs.python.org/library/stdtypes.html#str.format" rel="nofollow noreferrer">the <code>format</code> method</a> (v2.6+):</p> <pre><code>command = "python reads.py -r1 -pquery1.sql -q{0} -shotelspec -k6 -a5".format(queries) </code></pre> <p>You can alternatively use <a href="http://docs.python.org/library/stdtypes.html#string-formatting-operations" rel="nofollow noreferrer">the <code>%</code> operator</a>:</p> <pre><code>#since queries is a number, use %d as a placeholder command = "python reads.py -r1 -pquery1.sql -q%d -shotelspec -k6 -a5" % queries </code></pre> <hr> <h3>C-style loop → <a href="http://docs.python.org/tutorial/controlflow.html#for-statements" rel="nofollow noreferrer">Object-oriented-style loop</a></h3> <blockquote> <pre><code>for ((r = 1; r &lt; ($runs + 1); r++)) do done </code></pre> </blockquote> <p>Looping in Python is different from C-style iteration. What happens in Python is you iterate over an iterable object, like for example a list. Here, you are trying to do something <code>runs</code> times, so you would do this:</p> <pre><code>for r in range(runs): #loop body here </code></pre> <p><code>range(runs)</code> is equivalent to <code>[0,1,...,runs-1]</code>, a list of <code>runs = 5</code> integer elements. So you'll be repeating the body <code>runs</code> times. At every cicle, <code>r</code> is assigned the next item of the list. This is thus completely equivalent to what you are doing in Bash.</p> <p>If you're feeling daring, use <a href="http://docs.python.org/library/functions.html#xrange" rel="nofollow noreferrer"><code>xrange</code></a> instead. It's completely equivalent but uses more advanced language features (so it is harder to explain in layman's terms) but consumes less resources.</p> <hr> <h3>Output redirection → <a href="http://docs.python.org/library/subprocess.html?highlight=subprocess#using-the-subprocess-module" rel="nofollow noreferrer">the <code>subprocess</code> module</a></h3> <p>The "tougher" part, if you will: executing a program and getting its output. <a href="http://www.google.com/search?q=how+to+get+the+output+of+a+program+python" rel="nofollow noreferrer">Google to the rescue!</a> Obviously, the top hit is a stackoverflow question: <a href="https://stackoverflow.com/questions/748028/how-to-get-output-of-exe-in-python-script">this one</a>. You can hide all the complexity behind it with a simple function:</p> <pre><code>import subprocess, shlex def get_output_of(command): args = shlex.split(command) return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0] # this only returns stdout </code></pre> <p>So:</p> <blockquote> <pre><code>python reads.py -r1 -pquery1.sql -q$queries -shotelspec -k6 -a5 &gt;&gt; $outfile </code></pre> </blockquote> <p>becomes:</p> <pre><code>command = "python reads.py -r1 -pquery1.sql -q%s -shotelspec -k6 -a5" % queries read_result = get_output_of(command) </code></pre> <hr> <h3>Don't over-<code>subprocess</code>, batteries are included</h3> <p>Optionally, consider that you can get pretty much the same output of <code>date</code> with the following:</p> <pre><code>import time time_now = time.strftime("%c", time.localtime()) # Sat May 15 15:42:47 2010 </code></pre> <p>(Note the absence of the time zone information. This should be the subject of <a href="https://stackoverflow.com/questions/ask">another question</a>, if it is important to you.)</p> <hr> <h3>How your program should look like</h3> <p>The final result should then look like this:</p> <pre><code>import subprocess, shlex, time def get_output_of(command): #... body of get_output_of #... more functions ... if __name__ = "__main__": #only execute the following if you are calling this .py file directly, #and not, say, importing it #... initialization ... with file("outputfile.txt", "a") as output_file: #alternative way to open files, v2.5+ #... write date and other stuff ... for r in range(runs): #... loop body here ... </code></pre> <hr> <h2>Post scriptum</h2> <p>That must look pretty horrible when compared to the relatively simple and short Bash script, right? Python is not a specialized language: it aims to do everything reasonably well, but isn't built directly for running programs and getting the output of those.</p> <p>Still, you wouldn't <em>normally</em> write a database engine in Bash, right? It's different tools for different jobs. Here, unless you're planning to make some changes that would be non-trivial to write with that language, [Ba]sh was definitely the right choice.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload