On Thu, Nov 11, 2010 at 6:10 AM, Peter Otten <__pete...@web.de> wrote:
> James Mills wrote: > > > On Thu, Nov 11, 2010 at 11:01 AM, alex23 <wuwe...@gmail.com> wrote: > >> +1 on this approach. Clear and obvious and not reliant on any library > >> modules other than sys. > >> > >> itertools, what WAS I thinking? :) > > > > maybe: > > > > import sys > > from itertools import islice > > > > print [v for v in islice((line for line in sys.stdin), 0, 5)] > > (line for line in sys.stdin) > > and sys.stdin are equivalent as are > > [v for v in items] > > and list(items) > > So > > print list(islice(sys.stdin, 5)) > > Peter > -- > http://mail.python.org/mailman/listinfo/python-list > Hello Folks, You are great! Thanks for the suggestions! 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. 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. Check below: My test file: TestFile.txt ===================================================== 1 2 3 4 5 6 7 8 9 10 ===================================================== My test code: simpleReadLines.py ===================================================== import sys import cProfile def simpleTestLimitRLS(tLimit): sysStdinReadLines = sys.stdin.readlines allValues = [ int(v) for v in sysStdinReadLines() ] print allValues[:tLimit] def simpleTestLimitRL(tLimit): sysStdinReadLine = sys.stdin.readline print [ sysStdinReadLine() for x in xrange(0,5) ] if __name__ == '__main__': cProfile.run("simpleTestLimitRL(5)") cProfile.run("simpleTestLimitRLS(5)") ===================================================== Test result: ===================================================== >type TestFile.txt | python simpleReadLines.py ['1\n', '2\n', '3\n', '4\n', '5\n'] 8 function calls in 0.004 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.004 0.004 <string>:1(<module>) 1 0.004 0.004 0.004 0.004 simpleReadLines.py:13(simpleTestLimitRL) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 5 0.000 0.000 0.000 0.000 {method 'readline' of 'file' objects} [6, 7, 8, 9, 10] 4 function calls in 0.003 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.003 0.003 <string>:1(<module>) 1 0.003 0.003 0.003 0.003 simpleReadLines.py:8(simpleTestLimitRLS) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 1 0.000 0.000 0.000 0.000 {method 'readlines' of 'file' objects} ===================================================== If I change the execution order to: 1. simpleTestLimitRLS(5) 2. simpleTestLimitRL(5) Of course, python returns an error because all the input has been consumed by readlines(). Again, thanks for your attention and suggestions. Regards, Felipe.
-- http://mail.python.org/mailman/listinfo/python-list