Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion python file API

Received: by 10.180.107.38 with SMTP id gz6mr874138wib.0.1348558140219;
        Tue, 25 Sep 2012 00:29:00 -0700 (PDT)
Path: ed8ni50623678wib.0!nntp.google.com!proxad.net!feeder1-2.proxad.net!news.glorb.com!news.astraweb.com!border5.newsrouter.astraweb.com!not-for-mail
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Subject: Re: python file API
Newsgroups: comp.lang.python
References: <0ec1fe2e-890c-4e25-8047-4cb8bee0aa95@googlegroups.com>
	<5060D55C.3000407@davea.name>
	<mailman.1222.1348524844.27098.python-list@python.org>
	<mailman.1228.1348526190.27098.python-list@python.org>
	<506116d4$0$29981$c3e8da3$5496439d@news.astraweb.com>
	<k3rf8t$ppk$1@r03.glglgl.gl>
User-Agent: Pan/0.133 (House of Butterflies)
MIME-Version: 1.0
Date: 25 Sep 2012 07:28:59 GMT
Lines: 61
Message-ID: <50615d3b$0$29997$c3e8da3$5496439d@news.astraweb.com>
Organization: Unlimited download news at news.astraweb.com
NNTP-Posting-Host: cb3b509a.news.astraweb.com
X-Trace: DXC=Gg?P;l\omXlMm1IZKF?@MbL?0kYOcDh@jN7:H2`MmAUcHIPDifKVMen]G;2>V^?kWcbEW9A[5UK?e[[H7\8Y=QneGQJf;_A8]cl
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

On Tue, 25 Sep 2012 07:25:48 +0200, Thomas Rachel wrote:

> Am 25.09.2012 04:28 schrieb Steven D'Aprano:
> 
>> By the way, the implementation of this is probably trivial in Python
>> 2.x. Untested:
>>
>> class MyFile(file):
>>      @property
>>      def pos(self):
>>          return self.tell()
>>      @pos.setter
>>      def pos(self, p):
>>          if p<  0:
>>              self.seek(p, 2)
>>          else:
>>              self.seek(p)
>>
>> You could even use a magic sentinel to mean "see to EOF", say, None.
>>
>>          if p is None:
>>              self.seek(0, 2)
>>
>> although I don't know if I like that.
> 
> The whole concept is incomplete at one place: self.seek(10, 2) seeks
> beyond EOF, potentially creating a sparse file. This is a thing you
> cannot achieve.

On the contrary, since the pos attribute is just a wrapper around seek, 
you can seek beyond EOF easily:

f.pos = None
f.pos += 10

But for anything but the most trivial usage, I would recommend sticking 
to the seek method.

The problem with this idea is that the seek method takes up to three 
arguments (the file being operated on, the position, and the mode), and 
attribute syntax can only take two (the file, the position, e.g.: 
file.pos = position). So either there are cases that file.pos cannot 
handle (and so we need to keep tell/seek around, which leaves file.pos 
redundant), or we need multiple attributes, one for each mode), or we 
build a complicated, inconvenient API using special data types instead of 
plain integers.

So all up, I'm -1 on trying to replace the tell/seek API, and -0 on 
adding a second, redundant API.

Wait, there is another alternative: tuple arguments:

f.pos = (where, whence)

being the equivalent to seek(where, whence). At this point you just save 
two characters "f.pos=a,b" vs "f.seek(a,b)" so it simply isn't worth it 
for such a trivial benefit.


-- 
Steven