Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Subclassing file class

Reply
Thread Tools

Subclassing file class

 
 
David Vaughan
Guest
Posts: n/a
 
      08-04-2004

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: http://www.velocityreviews.com/forums/(E-Mail Removed)
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.
 
Reply With Quote
 
 
 
 
Michael Hudson
Guest
Posts: n/a
 
      08-04-2004
"David Vaughan" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      08-05-2004
On Wed, 4 Aug 2004 11:20:20 +0100, rumours say that "David Vaughan"
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Noob Q: subclassing or wrapping file class kj Python 4 09-17-2009 03:24 AM
Tricky subclassing problem: Parent class method uses static value from child class don.hosek@gmail.com Perl Misc 0 09-26-2006 08:45 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Thread safety when subclassing the Page class thechaosengine ASP .Net 2 12-10-2004 02:48 PM
Subclassing file and getting around the file.__init__ rigidity Jan Burgy Python 4 04-03-2004 05:50 PM



Advertisments