Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89615

Re: seek operation in python

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Re: seek operation in python
Organization Decebal Computing
References (1 earlier) <mailman.107.1430354001.3680.python-list@python.org> <87vbge9lm6.fsf@Equus.decebal.nl> <mailman.118.1430379207.3680.python-list@python.org> <87y4lavy4q.fsf@Equus.decebal.nl> <mailman.120.1430382699.3680.python-list@python.org>
Date 2015-04-30 11:06 +0200
Message-ID <87twvyvvct.fsf@Equus.decebal.nl> (permalink)

Show all headers | View raw


Op Thursday 30 Apr 2015 10:31 CEST schreef Dave Angel:

> On 04/30/2015 04:06 AM, Cecil Westerhof wrote:
>> Op Thursday 30 Apr 2015 09:33 CEST schreef Chris Angelico:
>>
>>> On Thu, Apr 30, 2015 at 4:27 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>>>>> with open("input.cpp") as f:
>>>>> lines = f.readlines()
>>>>> print(lines[7])
>>>>
>>>> Is the following not better:
>>>> print(open('input.cpp', 'r').readlines()[7])
>>>>
>>>> Time is the same (about 25 seconds for 100.000 calls), but I find
>>>> this more clear.
>>>
>>> The significant difference is that the 'with' block guarantees to
>>> close the file promptly. With CPython it probably won't make a lot
>>> of difference, and in a tiny script it won't do much either, but
>>> if you do this on Jython or IronPython or MicroPython or some
>>> other implementation, it may well make a gigantic difference -
>>> your loop might actually fail because the file's still open.
>>
>> I thought that in this case the file was also closed. But if that
>> is not the case I should think about this when I switch to another
>> version as CPython.
>>
>> I wrote a module where I have:
>> def get_indexed_message(message_filename, index):
>> """
>> Get index message from a file, where 0 gets the first message
>> """
>>
>> return open(expanduser(message_filename),
>> 'r').readlines()[index].rstrip()
>>
>> But this can be used by others also and they could be using Jython
>> or another implementation. So should I rewrite this and other
>> functions? Or would it be OK because the open is in a function?
>>
>
> No, it's not going to close the file just because the open is in a
> function. The "with" construct was designed to help solve exactly
> this problem. Please use it.

I already done it. I thought it not to much work. And it even makes
some code shorter:
-    marshal_file    = open(expanduser(marshal_filename), 'r')
-    not_list        = load(marshal_file)
-    marshal_file.close()
-    return not_list
+    with open(expanduser(marshal_filename), 'r') as f:
+        return load(f)

But here I did the close myself already, so that is not completely
honest of me. ;-)

I should spend some time to make my code more consistent.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

seek operation in python siva sankari R <buddingrose11@gmail.com> - 2015-04-29 11:08 -0700
  Re: seek operation in python Joel Goldstick <joel.goldstick@gmail.com> - 2015-04-29 14:17 -0400
    Re: seek operation in python siva sankari R <buddingrose11@gmail.com> - 2015-04-29 11:26 -0700
      Re: seek operation in python Billy Earney <billy.earney@gmail.com> - 2015-04-29 13:42 -0500
      Re: seek operation in python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-29 21:53 +0100
        Re: seek operation in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-30 10:19 +1000
  Re: seek operation in python MRAB <python@mrabarnett.plus.com> - 2015-04-29 19:50 +0100
  Re: seek operation in python John Gordon <gordon@panix.com> - 2015-04-29 18:53 +0000
  Re: seek operation in python Chris Angelico <rosuav@gmail.com> - 2015-04-30 10:33 +1000
    Re: seek operation in python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 08:27 +0200
      Re: seek operation in python Chris Angelico <rosuav@gmail.com> - 2015-04-30 17:33 +1000
        Re: seek operation in python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 10:06 +0200
          Re: seek operation in python Dave Angel <davea@davea.name> - 2015-04-30 04:31 -0400
            Re: seek operation in python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 11:06 +0200
              Re: seek operation in python Chris Angelico <rosuav@gmail.com> - 2015-04-30 19:39 +1000
          Re: seek operation in python Larry Hudson <orgnut@yahoo.com> - 2015-04-30 12:38 -0700
            Re: seek operation in python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 22:50 +0200
              Re: seek operation in python Larry Hudson <orgnut@yahoo.com> - 2015-04-30 19:42 -0700
  Re: seek operation in python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 14:31 +0200

csiph-web