<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=Advanced_Python_%28Fall_2017%29%2Flecture5</id>
	<title>Advanced Python (Fall 2017)/lecture5 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=Advanced_Python_%28Fall_2017%29%2Flecture5"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture5&amp;action=history"/>
	<updated>2026-06-21T22:57:08Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture5&amp;diff=124576&amp;oldid=prev</id>
		<title>Eroman: Created page with &quot;= lecture 5 =  == piping stdout to stdin ==  you can send the outputs (stdout) of commands in shell to other commands, or write the to files  http://www.tldp.org/LDP/abs/html/...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture5&amp;diff=124576&amp;oldid=prev"/>
		<updated>2017-10-10T09:12:54Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= lecture 5 =  == piping stdout to stdin ==  you can send the outputs (stdout) of commands in shell to other commands, or write the to files  http://www.tldp.org/LDP/abs/html/...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= lecture 5 =&lt;br /&gt;
&lt;br /&gt;
== piping stdout to stdin ==&lt;br /&gt;
&lt;br /&gt;
you can send the outputs (stdout) of commands in shell to other commands, or write the to files&lt;br /&gt;
&lt;br /&gt;
http://www.tldp.org/LDP/abs/html/io-redirection.html&lt;br /&gt;
&lt;br /&gt;
To receive the output of a pipe into your python script, use &amp;lt;code&amp;gt;sys.stdin&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;sys.stdin&amp;lt;/code&amp;gt; is an iterable.&lt;br /&gt;
&lt;br /&gt;
To write to stdout, you can use &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;sys.stdout.write&amp;lt;/code&amp;gt; -- which does not add the newline.&lt;br /&gt;
&lt;br /&gt;
To print all stdin to stdout in a python script, you could use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import sys&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main()&lt;br /&gt;
    for line in sys.stdin:&lt;br /&gt;
        sys.stdout.write(line)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
main()&amp;lt;/source&amp;gt;&lt;br /&gt;
and to print out a file: &amp;lt;code&amp;gt;cat ./file.txt | python3 my_script.py&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To make an stream of a file that waits for new lines to be added, use the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;tail -f ./file.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and you can pipe this output to your python script.&lt;br /&gt;
&lt;br /&gt;
== Task 1: Make a basic grep ==&lt;br /&gt;
&lt;br /&gt;
something that can function as such &amp;lt;code&amp;gt;tail -f ./file.txt | grep $SEARCH_TERM&amp;lt;/code&amp;gt; where lines with the search term are written to stdout&lt;br /&gt;
&lt;br /&gt;
=== A Solution ===&lt;br /&gt;
&lt;br /&gt;
Normal function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import argparse&lt;br /&gt;
import sys&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def grep(term, line):&lt;br /&gt;
    if term in line:&lt;br /&gt;
        sys.stdout.write(line)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
&lt;br /&gt;
    parser = argparse.ArgumentParser()&lt;br /&gt;
    parser.add_argument(&amp;#039;term&amp;#039;)&lt;br /&gt;
    args = parser.parse_args()&lt;br /&gt;
    term = args.term&lt;br /&gt;
    for line in sys.stdin:&lt;br /&gt;
        grep(term, line)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    main()&amp;lt;/source&amp;gt;&lt;br /&gt;
== Event driven: Using a generator ==&lt;br /&gt;
&lt;br /&gt;
the function &amp;lt;code&amp;gt;grep&amp;lt;/code&amp;gt; is a generator. When the file object &amp;lt;code&amp;gt;sys.stdin&amp;lt;/code&amp;gt; yields another line, then the generator &amp;lt;code&amp;gt;grep&amp;lt;/code&amp;gt; yields another line. When &amp;lt;code&amp;gt;grep&amp;lt;/code&amp;gt; yeilds another line, then the &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; loop prints another line.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import argparse&lt;br /&gt;
import sys&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def grep(term, lines):&lt;br /&gt;
    for line in lines:&lt;br /&gt;
        if term in line:&lt;br /&gt;
            print(&amp;#039;grep executed&amp;#039;)&lt;br /&gt;
            yield line&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
&lt;br /&gt;
    parser = argparse.ArgumentParser()&lt;br /&gt;
    parser.add_argument(&amp;#039;term&amp;#039;)&lt;br /&gt;
    args = parser.parse_args()&lt;br /&gt;
    term = args.term&lt;br /&gt;
    grep_lines = grep(term, sys.stdin)&lt;br /&gt;
    for line in grep_lines:&lt;br /&gt;
        print(&amp;#039;print executed&amp;#039;)&lt;br /&gt;
        sys.stdout.write(line)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
== Coroutine ==&lt;br /&gt;
&lt;br /&gt;
Please see this tutorial for a better and fuller explanation of coroutines. NOTE: this tutorial is written in Python 2 code. http://www.dabeaz.com/coroutines/&lt;br /&gt;
&lt;br /&gt;
Here is our grep written with a coroutine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import argparse&lt;br /&gt;
import sys&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def grep(pattern):&lt;br /&gt;
    while True:&lt;br /&gt;
        line = (yield)&lt;br /&gt;
        if pattern in line:&lt;br /&gt;
            sys.stdout.write(line)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
&lt;br /&gt;
    parser = argparse.ArgumentParser()&lt;br /&gt;
    parser.add_argument(&amp;#039;term&amp;#039;)&lt;br /&gt;
    args = parser.parse_args()&lt;br /&gt;
    term = args.term&lt;br /&gt;
    g = grep(term)&lt;br /&gt;
    next(g)&lt;br /&gt;
    for line in sys.stdin:&lt;br /&gt;
        g.send(line)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eroman</name></author>
	</entry>
</feed>