Subclassing file class

Discussion in 'Python' started by David Vaughan, Aug 4, 2004.

  1. I'm using v2.3, and trying to write to text files, but with a maximum
    line length. So, if a line is getting too long, a suitable ' '
    character is replaced by a new line. I'm subclassing the file class,
    and, as long as I just use the write method, this works fine. But
    "print >>" doesn't behave as I want:

    class max_width_file(file):
    def write(self, txt):
    print "In max_width_file's write method."
    fout = max_width_file('foo.txt', 'w')
    print >> fout, 'bar'
    fout.close()

    bypasses my write method and prints 'bar' directly to the file. That
    isn't the behaviour I inferred from

    http://docs.python.org/ref/print.html

    How do I intercept "print >>"? Has anyone already implemented this?
    And should the documentation be changed?

    Any help very welcome.
    David



    David Vaughan
    Structural Analyst
    for Gifford

    GIFFORD AND PARTNERS LTD
    Carlton House, Ringwood Road, Woodlands,
    Southampton, SO40 7HT, UNITED KINGDOM

    Tel: +44 (0)23 8081 7500 Fax: +44 (0)23 8081 7600
    Email:
    Web: http://www.gifford.uk.com

    This e-mail and any files transmitted with it are intended solely for
    the use of the individual or entity to whom they are addressed and may
    be confidential. If you are not the intended recipient or the person
    responsible for delivering the e-mail to the intended recipient, be
    advised that you have received this e-mail in error and that any use,
    dissemination, forwarding, printing or copying of this e-mail is
    strictly prohibited.
     
    David Vaughan, Aug 4, 2004
    #1
    1. Advertising

  2. "David Vaughan" <> writes:

    > I'm using v2.3, and trying to write to text files, but with a maximum
    > line length. So, if a line is getting too long, a suitable ' '
    > character is replaced by a new line. I'm subclassing the file class,
    > and, as long as I just use the write method, this works fine. But
    > "print >>" doesn't behave as I want:


    You'll almost certainly be happier implementing a 'file-like object'
    that does what you want than subclassing file.

    Cheers,
    mwh

    --
    Every day I send overnight packages filled with rabid weasels to
    people who use frames for no good reason.
    -- The Usenet Oracle, Oracularity #1017-1
     
    Michael Hudson, Aug 4, 2004
    #2
    1. Advertising

  3. On Wed, 4 Aug 2004 11:20:20 +0100, rumours say that "David Vaughan"
    <> might have written:

    >I'm using v2.3, and trying to write to text files, but with a maximum
    >line length. [snip]


    Apart from any other advices you get, you might want to consider the
    textwrap module.
    --
    TZOTZIOY, I speak England very best,
    "Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
     
    Christos TZOTZIOY Georgiou, Aug 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thechaosengine

    Thread safety when subclassing the Page class

    thechaosengine, Dec 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    397
    Scott Allen
    Dec 10, 2004
  2. E11
    Replies:
    1
    Views:
    4,781
    Thomas Weidenfeller
    Oct 12, 2005
  3. Jan Burgy
    Replies:
    4
    Views:
    448
    Peter Otten
    Apr 3, 2004
  4. kj
    Replies:
    4
    Views:
    239
    Steven D'Aprano
    Sep 17, 2009
  5. Replies:
    0
    Views:
    138
Loading...

Share This Page