python file API
Dave Angel
d at davea.name
Mon Sep 24 18:36:00 EDT 2012
(forwarding to the list)
On 09/24/2012 06:23 PM, Mark Adam wrote:
> On Mon, Sep 24, 2012 at 4:49 PM, Dave Angel <d at davea.name> wrote:
>> On 09/24/2012 05:35 PM, zipher wrote:
>>> For some time now, I've wanted to suggest a better abstraction for the <file> type in Python. It currently uses an antiquated C-style interface for moving around in a file, with methods like tell() and seek(). But after attributes were introduced to Python, it seems it should be re-addressed.
>>>
>>> Let file-type have an attribute .pos for position. Now you can get rid of the seek() and tell() methods and manipulate the file pointer more easily with standard arithmetic operations.
>>>
>>>>>> file.pos = x0ae1 #move file pointer to an absolute address
>>>>>> file.pos +=1 #increment the file pointer one byte
>>>>>> curr_pos = file.pos #read current file pointer
>
>> And what approach would you use for positioning relative to
>> end-of-file? That's currently done with an optional second parameter to
>> seek() method.
>
> As size is an oft-useful construct, let it (like .name) be part of the
> descriptor. Then
>
>>>> file.pos = file.size - 80 #80 chars from end-of-file
>
> (Or, one could make slices part of the API...)
>
> mark
>
Well, if one of the goals was to reduce the number of attributes, we're
now back to the original number of them.
--
DaveA
More information about the Python-list
mailing list