Re: python file API

2012-09-30 Thread Ramchandra Apte
On Tuesday, 25 September 2012 03:05:16 UTC+5:30, zipher wrote: > For some time now, I've wanted to suggest a better abstraction for the > 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

Re: python file API

2012-09-25 Thread Thomas Rachel
Am 25.09.2012 09:28 schrieb Steven D'Aprano: 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

Re: python file API

2012-09-25 Thread Chris Angelico
On Wed, Sep 26, 2012 at 2:07 AM, Dennis Lee Bieber wrote: > f.pos += delta > > would be a "seek.set" and with a naive driver might trigger a rewind to > the start of the tape followed by a seek to the absolute position, > whereas the seek from current location would only move the tape by t

Re: python file API

2012-09-25 Thread Grant Edwards
On 2012-09-25, Dennis Lee Bieber wrote: > On Tue, 25 Sep 2012 08:22:05 +0200, Ulrich Eckhardt > declaimed the following in > gmane.comp.python.general: > >> Am 24.09.2012 23:49, schrieb Dave Angel: >> > And what approach would you use for positioning relative to >> > end-of-file? That's currently

Re: python file API

2012-09-25 Thread Thomas Rachel
Am 25.09.2012 10:13 schrieb Dennis Lee Bieber: Or some bit setting registers, like on ATxmega: OUT = 0x10 sets bit 7 and clears all others, OUTSET = 0x10 only sets bit 7, OUTTGL = 0x10 toggles it and OUTCLR = 0x10 clears it. Umpfzg. s/bit 7/bit 4/. I don't think I'd want to work with

Re: python file API

2012-09-25 Thread Oscar Benjamin
On 25 September 2012 11:51, Mark Lawrence wrote: > On 25/09/2012 11:38, Oscar Benjamin wrote: > >> On 25 September 2012 08:27, Mark Lawrence >> wrote: >> >> On 25/09/2012 03:32, Mark Adam wrote: >>> >>> On Mon, Sep 24, 2012 at 5:55 PM, Oscar Benjamin wrote: try: > f.

Re: python file API

2012-09-25 Thread Mark Lawrence
On 25/09/2012 11:38, Oscar Benjamin wrote: On 25 September 2012 08:27, Mark Lawrence wrote: On 25/09/2012 03:32, Mark Adam wrote: On Mon, Sep 24, 2012 at 5:55 PM, Oscar Benjamin wrote: try: f.pos = 256 except IOError: print('Unseekable file') Something along these lines ht

Re: python file API

2012-09-25 Thread Oscar Benjamin
On 25 September 2012 08:27, Mark Lawrence wrote: > On 25/09/2012 03:32, Mark Adam wrote: > >> On Mon, Sep 24, 2012 at 5:55 PM, Oscar Benjamin >> wrote: >> >>> try: >>> f.pos = 256 >>> except IOError: >>> print('Unseekable file') >> >> > Something along these lines http://docs.python.or

Re: python file API

2012-09-25 Thread Oscar Benjamin
On Sep 25, 2012 9:28 AM, "Dennis Lee Bieber" wrote: > > On Tue, 25 Sep 2012 08:22:05 +0200, Ulrich Eckhardt > declaimed the following in > gmane.comp.python.general: > > > Am 24.09.2012 23:49, schrieb Dave Angel: > > > And what approach would you use for positioning relative to > > > end-of-file?

Re: python file API

2012-09-25 Thread Ian Kelly
On Mon, Sep 24, 2012 at 11:32 PM, Thomas Rachel wrote: > Am 25.09.2012 00:37 schrieb Ian Kelly: >> Since ints are immutable, the language specifies that it should be the >> equivalent of "file.pos = file.pos - 100", so it should set the file >> pointer to 68 bytes before EOF. > > > But this is not

Re: python file API

2012-09-25 Thread Steven D'Aprano
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() >>

Re: python file API

2012-09-25 Thread Mark Lawrence
On 25/09/2012 03:32, Mark Adam wrote: On Mon, Sep 24, 2012 at 5:55 PM, Oscar Benjamin wrote: There are many situations where a little bit of attribute access magic is a good thing. However, operations that involve the underlying OS and that are prone to raising exceptions even in bug free code

Re: python file API

2012-09-25 Thread Ulrich Eckhardt
Am 24.09.2012 23:49, schrieb Dave Angel: 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. Negative indices. ;) Uli -- http://mail.python.org/mailman/listinfo/python-list

Re: python file API

2012-09-24 Thread Thomas Rachel
Am 25.09.2012 00:37 schrieb Ian Kelly: On Mon, Sep 24, 2012 at 4:14 PM, Chris Angelico wrote: file.pos = 42 # Okay, you're at position 42 file.pos -= 10 # That should put you at position 32 foo = file.pos # Presumably foo is the integer 32 file.pos -= 100 # What should this do? Since ints are

Re: python file API

2012-09-24 Thread Thomas Rachel
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

Re: python file API

2012-09-24 Thread Mark Adam
On Mon, Sep 24, 2012 at 5:55 PM, Oscar Benjamin wrote: > There are many situations where a little bit of attribute access magic is a > good thing. However, operations that involve the underlying OS and that are > prone to raising exceptions even in bug free code should not be performed > implicitl

Re: python file API

2012-09-24 Thread Steven D'Aprano
On Mon, 24 Sep 2012 15:36:20 -0700, zipher wrote: > You raise a valid point: that by abstracting the file pointer into a > position attribute you risk "de-coupling" the conceptual link between > the underlying file and your abstraction in the python interpreter I don't think this argument holds w

Re: python file API

2012-09-24 Thread Steven D'Aprano
On Tue, 25 Sep 2012 08:14:01 +1000, Chris Angelico wrote: > Presumably the same way you reference a list element relative to > end-of-list: negative numbers. However, this starts to feel like magic > rather than attribute assignment - it's like manipulating the DOM in > JavaScript, you set an attr

Re: python file API

2012-09-24 Thread Chris Kaynor
On Mon, Sep 24, 2012 at 3:37 PM, Ian Kelly wrote: > On Mon, Sep 24, 2012 at 4:14 PM, Chris Angelico wrote: >> file.pos = 42 # Okay, you're at position 42 >> file.pos -= 10 # That should put you at position 32 >> foo = file.pos # Presumably foo is the integer 32 >> file.pos -= 100 # What should th

Re: python file API

2012-09-24 Thread Mark Lawrence
On 24/09/2012 22:35, zipher wrote: For some time now, I've wanted to suggest a better abstraction for the 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 se

Re: python file API

2012-09-24 Thread Chris Angelico
On Tue, Sep 25, 2012 at 8:37 AM, Ian Kelly wrote: > On Mon, Sep 24, 2012 at 4:14 PM, Chris Angelico wrote: >> file.pos = 42 # Okay, you're at position 42 >> file.pos -= 10 # That should put you at position 32 >> foo = file.pos # Presumably foo is the integer 32 >> file.pos -= 100 # What should th

Re: python file API

2012-09-24 Thread Oscar Benjamin
On 24 September 2012 23:41, Mark Adam wrote: > > seek() and tell() can raise exceptions on some files. Exposing pos as an > > attribute and allowing it to be manipulated with attribute access gives > the > > impression that it is always meaningful to do so. > > It's a good point, python already

Re: python file API

2012-09-24 Thread Ian Kelly
On Mon, Sep 24, 2012 at 4:14 PM, Chris Angelico wrote: > file.pos = 42 # Okay, you're at position 42 > file.pos -= 10 # That should put you at position 32 > foo = file.pos # Presumably foo is the integer 32 > file.pos -= 100 # What should this do? Since ints are immutable, the language specifies

Re: python file API

2012-09-24 Thread Dave Angel
(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 wrote: >> On 09/24/2012 05:35 PM, zipher wrote: >>> For some time now, I've wanted to suggest a better abstraction for the >>> type in Python. It currently uses an antiquated C-style

Re: python file API

2012-09-24 Thread zipher
You raise a valid point: that by abstracting the file pointer into a position attribute you risk "de-coupling" the conceptual link between the underlying file and your abstraction in the python interpreter, but I think the programmer can take responsibility for maintaining the abstraction. Th

Re: python file API

2012-09-24 Thread Chris Angelico
On Tue, Sep 25, 2012 at 7:49 AM, Dave Angel wrote: > On 09/24/2012 05:35 PM, zipher wrote: >> 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. >> >

Re: python file API

2012-09-24 Thread Chris Kaynor
On Mon, Sep 24, 2012 at 2:49 PM, Dave Angel wrote: > > 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. > I'm not advocating for or against the idea, but that could be handled the same way index

Re: python file API

2012-09-24 Thread Oscar Benjamin
On 24 September 2012 22:35, zipher wrote: > For some time now, I've wanted to suggest a better abstraction for the > 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 P

Re: python file API

2012-09-24 Thread Dave Angel
On 09/24/2012 05:35 PM, zipher wrote: > For some time now, I've wanted to suggest a better abstraction for the > 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 Pyth