[Python-3000] iostack and sock2

Nick Coghlan ncoghlan at gmail.com
Tue Jun 6 11:47:28 CEST 2006

Greg Ewing wrote:
> tomer filiba wrote:
>> yes, but +=/-= can be overriden to provide "efficient seeking". and, just
>> thought about it: just like negative indexes of sequences, negative positions
>> should be relative to the end of the stream. for example:
>> f.position = 4     # absolute -- seek(4, "start")
>> f.position += 6   # relative to current -- seek(6, "curr")
>> f.position = -7    # relative to end of stream -- seek(-7, "end")
> How would you seek to exactly the end of the file,
> without introducing signed integer zeroes to Python?-)

Since it doesn't mean anything else, you could define a position of "None" as 
meaning 'just past the last valid byte in the file' (i.e., right at the end).

Then "f.position = None" would seek to the end. This actually matches the way 
None behaves when it is used as the endpoint of a slice: range(3)[0:None] 
returns [0, 1, 2, 3].

If that's not intuitive enough for your tastes, then a class attribute would 
also work:

f.position = f.END

(f.END would be serving as 'signed zero', since f.position -=1 and f.position 
= -1 would do the same thing)

FWIW, I also realised my objection to properties raising IOError doesn't apply 
for IO streams - unlike path objects, an IO stream *only* makes sense if you 
have access to the underlying IO layer. So documenting certain properties as 
potentially raising IOError seems legitimate in this case.

I'm glad I thought of that, since I like this API a lot better than mucking 
around with passing strings to seek() ;)


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-3000 mailing list