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


Groups > comp.lang.python > #30021

Re: python file API

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!news2.arglkargh.de!news.visyn.net!visyn.net!not-for-mail
From Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Newsgroups comp.lang.python
Subject Re: python file API
Date Tue, 25 Sep 2012 07:25:48 +0200
Organization A newly installed InterNetNews server
Lines 96
Message-ID <k3rf8t$ppk$1@r03.glglgl.gl> (permalink)
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>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace hoshi.visyn.net WEvSDrMJH14koDaiM7ME2VBg/tRVcVLSqbnEKyS8wxvY5F7c8sXtulZ89aREVikrFeISnmYgjyt3qvX7kG3fwA==
X-Complaints-To abuse@open-news-network.org
NNTP-Posting-Date Tue, 25 Sep 2012 05:30:44 +0000 (UTC)
X-User-ID fZLTLpa3Jgnsbn2pegY7gzVy2wkPMBIpWzXZlNM2fPrRyIz6+A5AxY3aFlFzqvP92XgdFMf0421S3WygihTCDw==
In-Reply-To <506116d4$0$29981$c3e8da3$5496439d@news.astraweb.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120312 Thunderbird/11.0
Xref csiph.com comp.lang.python:30021

Show key headers only | View raw


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.

But the idea is great. I'd suggest to have another property:

       [...]
       @pos.setter
       def pos(self, p):
           self.seek(p)
       @property
       def eofpos(self): # to be consistent
           return self.tell()
       @eofpos.setter
       def eofpos(self, p):
           self.seek(p, 2)

Another option could be a special descriptor which can be used as well 
for relative seeking:

class FilePositionDesc(object):
     def __init__(self):
         pass
     def __get__(self, instance, owner):
	return FilePosition(self)
     def __set__(self, value):
         self.seek(value)

class FilePosition(object):
     def __init__(self, file):
         self.file = file
     def __iadd__(self, offset):
         self.file.seek(offset, 1)
     def __isub__(self, offset):
         self.file.seek(-offset, 1)

class MyFile(file):
     pos = FilePositionDesc()
     [...]

Stop.

This could be handled with a property as well.

Besides, this breaks some other expectations to the pos. So let's 
introduce a 3rd property named relpos:

class FilePosition(object):
     def __init__(self, file):
         self.file = file
	self.seekoffset = 0
     def __iadd__(self, offset):
         self.seekoffset += offset
     def __isub__(self, offset):
         self.seekoffset -= offset
     def __int__(self):
         return self.file.tell() + self.seekoffset

class MyFile(file):
       @property
       def relpos(self):
           return FilePosition(self) # from above
       @relpos.setter
       def relpos(self, ofs):
           try:
               o = ofs.seekoffset # is it a FilePosition?
           except AttributeError:
               self.seek(ofs, 1) # no, but ofs can be an int as well
           else:
               self.seek(o, 1) # yes, it is


Thomas

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


Thread

python file API zipher <dreamingforward@gmail.com> - 2012-09-24 14:35 -0700
  Re: python file API Dave Angel <d@davea.name> - 2012-09-24 17:49 -0400
    Re: python file API Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-09-25 08:22 +0200
      Re: python file API Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-25 04:19 -0400
        Re: python file API Grant Edwards <invalid@invalid.invalid> - 2012-09-25 14:07 +0000
      Re: python file API Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-25 12:07 -0400
      Re: python file API Chris Angelico <rosuav@gmail.com> - 2012-09-26 02:12 +1000
  Re: python file API Chris Kaynor <ckaynor@zindagigames.com> - 2012-09-24 14:58 -0700
  Re: python file API Chris Angelico <rosuav@gmail.com> - 2012-09-25 08:14 +1000
    Re: python file API zipher <dreamingforward@gmail.com> - 2012-09-24 15:36 -0700
      Re: python file API Mark Adam <dreamingforward@gmail.com> - 2012-09-24 21:32 -0500
      Re: python file API Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-25 08:27 +0100
      Re: python file API Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-25 11:51 +0100
    Re: python file API zipher <dreamingforward@gmail.com> - 2012-09-24 15:36 -0700
      Re: python file API Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-25 02:28 +0000
        Re: python file API Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 07:25 +0200
          Re: python file API Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-25 07:28 +0000
            Re: python file API Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 21:40 +0200
    Re: python file API Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-25 01:33 +0000
  Re: python file API Dave Angel <d@davea.name> - 2012-09-24 18:36 -0400
  Re: python file API Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-24 16:37 -0600
    Re: python file API Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 07:32 +0200
      Re: python file API Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-25 04:13 -0400
        Re: python file API Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 11:53 +0200
      Re: python file API Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-25 03:32 -0600
  Re: python file API Chris Angelico <rosuav@gmail.com> - 2012-09-25 08:57 +1000
  Re: python file API Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-25 00:12 +0100
  Re: python file API Chris Kaynor <ckaynor@zindagigames.com> - 2012-09-24 16:14 -0700
  Re: python file API Ramchandra Apte <maniandram01@gmail.com> - 2012-09-30 05:50 -0700

csiph-web