extending file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Cioffi

    #1

    extending file?

    Hello all,

    Are there any docs or examples of extending the file type? I work
    with EDI messages that are very like text files, just with a few
    quirks. ;-) and I was wondering if I could strech and twist the built
    in file type to make things a bit faster and more full featured.

    Specifically I would need to alter the iterator and ideally the line terminitor.

    Chris
    --
    <a href="http://spreadfirefox.c om/community/?q=affiliates&i d=0&t=1">Get
    Firefox!</a>
  • Diez B. Roggisch

    #2
    Re: extending file?

    Chris Cioffi wrote:
    [color=blue]
    > Are there any docs or examples of extending the file type? I work
    > with EDI messages that are very like text files, just with a few
    > quirks. ;-) and I was wondering if I could strech and twist the built
    > in file type to make things a bit faster and more full featured.[/color]

    I'm not sure what you want to accomplish here - but one of the nicer aspects
    of python is that you don't have to inherit from file when you want to pass
    a file-like object to some lib. See the cStringIO module. So if you create
    a edi-class that _behaves_ like a file, thats all you need - you then can
    pass it to any file-accepting pieco of code.

    Is that what you've been asking for?
    --
    Regards,

    Diez B. Roggisch

    Comment

    • Bengt Richter

      #3
      Re: extending file?

      On Tue, 02 Nov 2004 12:13:34 -0500, Jeremy Jones <zanesdad@bells outh.net> wrote:
      [color=blue]
      ><snip the whole thread>
      >
      >So, with everything being said that's been said, here's are two
      >questions for myself and for Chris Cioffi:
      >
      >1. How difficult would it be to modify file.readline() so it would read
      >until a specific character rather than the standard end of line
      >character(s) ?
      >2. What would be the best way to go about the above?
      >
      >If anyone has any ideas, I think we would both listen with great interest.
      >
      >[/color]
      Maybe this general idea could give you a way to install your own custom solution
      without an app-specific custom mod to file or open:

      I'd like to see a file/open hook in sys (or maybe os), so that you could intercept the open
      operation for specific file paths registered with the hook. E.g., if it were a
      simple directory

      sys.filehook['foo.txt'] = MyFileClass # or callable factory

      would cause file('foo.txt', mode, kwarg=something ) to look in sys.filehook before going to the normal
      file system, and return MyFileClass('fo o.txt', mode, kwarg) instead of the normal file object. (I.e.,
      file & open would be extended to accept *args, **kw to pass through to hooks if present)

      This would allow passing custom input sequences as file-like objects to programs whose interface
      is a file path string, and obviously you could use the file path arg passed through to open/file
      to open a real file that you wanted to filter in some way (assuming you temporarily at least
      removed the hook, or had an alias to the unhooked open function).

      I don't think this would be hard to implement, but I haven't thought it through.
      Gotta go vote. And other stuff...

      An extension of this idea would be to allow registering patterns like
      sys.filehook['*.cfg'] = MyConfigFilter
      where a mathing open would pass through the actual matched path.


      Regards,
      Bengt Richter

      Comment

      Working...