<br><br><div class="gmail_quote">On Thu, Nov 11, 2010 at 6:10 AM, Peter Otten <span dir="ltr"><__<a href="mailto:peter__@web.de">peter__@web.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">James Mills wrote:<br>
<br>
> On Thu, Nov 11, 2010 at 11:01 AM, alex23 <<a href="mailto:wuwei23@gmail.com">wuwei23@gmail.com</a>> wrote:<br>
</div><div class="im">>> +1 on this approach. Clear and obvious and not reliant on any library<br>
>> modules other than sys.<br>
>><br>
>> itertools, what WAS I thinking? :)<br>
><br>
</div><div class="im">> maybe:<br>
><br>
> import sys<br>
> from itertools import islice<br>
><br>
> print [v for v in islice((line for line in sys.stdin), 0, 5)]<br>
<br>
(line for line in sys.stdin)<br>
<br>
</div>and sys.stdin are equivalent as are<br>
<br>
[v for v in items]<br>
<br>
and list(items)<br>
<br>
So<br>
<br>
print list(islice(sys.stdin, 5))<br>
<font color="#888888"><br>
Peter<br>
</font><div><div></div><div class="h5">--<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><div>Hello Folks,</div><div><br></div><div>You are great! Thanks for the suggestions!</div><div><br></div><div>With the values between # #, I meant I would like to limit the values sys.stdin.readlines() read (but I think there is no way to do it); instead of reading the entire input, limit it to read only 5 entries.</div>

<div>I knew about readline(), but when checking it with cProfile each readline() needs a function call and with readlines() it is only one. I think that depending on input, readline() calls may have a higher cost.</div><div>

<br></div><div>Check below:</div><div><br></div><div>My test file: TestFile.txt</div><div>=====================================================</div><div><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div>

<div>7</div><div>8</div><div>9</div><div>10</div><div>=====================================================</div></div><div><br></div><div>My test code: simpleReadLines.py</div><div>=====================================================</div>

<div><div>import sys</div><div>import cProfile</div><div><br></div><div>def simpleTestLimitRLS(tLimit):</div><div>    sysStdinReadLines = sys.stdin.readlines</div><div>    allValues = [ int(v) for v in sysStdinReadLines() ]</div>

<div>    print allValues[:tLimit]</div><div><br></div><div>def simpleTestLimitRL(tLimit):</div><div>    sysStdinReadLine = sys.stdin.readline</div><div>    print [ sysStdinReadLine() for x in xrange(0,5) ]</div><div><br>
</div>
<div>if __name__ == '__main__':</div><div>    cProfile.run("simpleTestLimitRL(5)")</div><div>    cProfile.run("simpleTestLimitRLS(5)")</div></div><div>=====================================================</div>

<div><br></div><div>Test result:</div><div>=====================================================</div><div><div>>type TestFile.txt | python simpleReadLines.py</div><div>['1\n', '2\n', '3\n', '4\n', '5\n']</div>

<div>         8 function calls in 0.004 CPU seconds</div><div><br></div><div>   Ordered by: standard name</div><div><br></div><div>   ncalls  tottime  percall  cumtime  percall filename:lineno(function)</div><div>        1    0.000    0.000    0.004    0.004 <string>:1(<module>)</div>

<div>        1    0.004    0.004    0.004    0.004 simpleReadLines.py:13(simpleTestLimitRL)</div><div>        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}</div><div>

        5    0.000    0.000    0.000    0.000 {method 'readline' of 'file' objects}</div><div><br></div><div>[6, 7, 8, 9, 10]</div><div>         4 function calls in 0.003 CPU seconds</div><div><br></div><div>

   Ordered by: standard name</div><div><br></div><div>   ncalls  tottime  percall  cumtime  percall filename:lineno(function)</div><div>        1    0.000    0.000    0.003    0.003 <string>:1(<module>)</div>
<div>
        1    0.003    0.003    0.003    0.003 simpleReadLines.py:8(simpleTestLimitRLS)</div><div>        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}</div><div>        1    0.000    0.000    0.000    0.000 {method 'readlines' of 'file' objects}</div>

</div><div>=====================================================</div><div><br></div><div>If I change the execution order to:</div><div>    1. simpleTestLimitRLS(5)</div><div>    2. simpleTestLimitRL(5)</div><div><div><br>

</div><div>Of course, python returns an error because all the input has been consumed by readlines().</div><div><br></div><div>Again, thanks for your attention and suggestions.</div><div><br></div><div>Regards,</div><div>

Felipe.</div></div>