473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Subclassing file class


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: Da***********@gifford.uk.com
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.
Jul 18 '05 #1
2 1483
"David Vaughan" <Da***********@Gifford.UK.com> 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
Jul 18 '05 #2
On Wed, 4 Aug 2004 11:20:20 +0100, rumours say that "David Vaughan"
<Da***********@Gifford.UK.com> 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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
3
by: Peter Olsen | last post by:
I want to define a class "point" as a subclass of complex. When I create an instance sample = point(<arglist>) I want "sample" to "be" a complex number, but with its real and imaginary...
6
by: Steven D'Aprano | last post by:
I've been working with the Borg design pattern from here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 and I'm having problems subclassing it. I'm a newbie, so I've probably...
11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
1
by: paul.hester | last post by:
Hi all, I'm planning to migrate a website from ASP to ASP .NET 2.0. The old page has a common include file that performs a series of tasks when every page loads. I'm new to ASP .NET 2.0 and am...
4
by: Erik Johnson | last post by:
I ran into a problem I didn't understand at first. I got part of it figured out. Let me first demonstrate the original problem: class Super(object): def __init__(self): self._class = 'Super'...
16
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do...
3
by: Larry Bates | last post by:
I'm trying to learn about subclassing new style classes and the first project I went to do needs to subclass zipfile to add some methods. Why does this: import zipfile class...
5
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.