473,383 Members | 1,843 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,383 software developers and data experts.

My first attempt at subclassing....Gahh!

I want to use filecmp.dircmp, but it only prints to the screen, there
is no way to capture the output. So I thought that I should simply be
able to subclass it to return a list (Danger! Danger, Will! Simple
and Subclass should never be used in the same sentence!). However, my
first attempt (pathetic as it may be), results in an error that I
don't understand. Obviously, I'm doing something wrong, but what?
I'm using Python 2.2.3 and the error I get is 'TypeError: cannot
create 'instance method' instances'.

import filecmp

class dircmp(filecmp.dircmp):
def my_report_partial_closure(self):
output= []
self.report()
for x in self.subdirs.keys():
output.append(self.subdirs[x].report())
return output

class report(filecmp.dircmp.report):
def my_report(self): # Print a report on the differences
between a and b
# Output format is purposely lousy
output = []
output.append('diff', self.left, self.right)
if self.left_only:
self.left_only.sort()
output.append('Only in', self.left, ':',
self.left_only)
if self.right_only:
self.right_only.sort()
output.append('Only in', self.right, ':',
self.right_only)
if self.same_files:
self.same_files.sort()
output.append('Identical files :', self.same_files)
if self.diff_files:
self.diff_files.sort()
output.append('Differing files :', self.diff_files)
if self.funny_files:
self.funny_files.sort()
output.append('Trouble with common files :',
self.funny_files)
if self.common_dirs:
self.common_dirs.sort()
output.append('Common subdirectories :',
self.common_dirs)
if self.common_funny:
self.common_funny.sort()
output.append('Common funny cases :',
self.common_funny)
Jul 18 '05 #1
7 1532
Robin Siebler wrote:
class report(filecmp.dircmp.report):


Here you are attempting to subclass an instance method instead of another
class.

Jeffrey
Jul 18 '05 #2

"Robin Siebler" <ro***********@palmsource.com> wrote in message
news:95**************************@posting.google.c om...
I'm using Python 2.2.3 and the error I get is 'TypeError: cannot
create 'instance method' instances'.

import filecmp

class dircmp(filecmp.dircmp):
def my_report_partial_closure(self):
output= []
self.report()
for x in self.subdirs.keys():
output.append(self.subdirs[x].report())
return output

class report(filecmp.dircmp.report):


filecmp.dircmp.report is a method in the filecmp.dircmp class. You have to
subclass from another class and you cannot subclass from a method. What you
probably want is define a subclass of filecmp.dircmp (like your own dircmp)
and define a method report(self) inside it. This means that you are
overriding the superclass's report( ) method. Inside your report( ) method,
you can still use the filecmp.dircmp.report method like this:
filecmp.dircmp.report(self).

Hope this helps.
Jul 18 '05 #3
Robin Siebler <ro***********@palmsource.com> wrote:
I want to use filecmp.dircmp, but it only prints to the screen, there
is no way to capture the output. So I thought that I should simply be


Il prints to sys.stdout, which may be the screen or elsewhere, therefore
there IS of course a way to capture the output:

import sys, filecmp, cStringIO

save_stdout = sys.stdout
capture = cStringIO.StringIO()
sys.stdout = capture
filecmp.dircmp('/tmp', '/tmp').report()
sys.stdout = save_stdout

capture.seek(0)
for i, line in enumerate(capture):
print i, repr(line)
capture.close()

But why would you want that? The output format is purposefully lousy.
You have all the information as attributes of the object that
filecmp.dircmp returns -- why not format said info, or subsets thereof,
however best you please...? Say:

x = filecmp.dircmp('/tmp', '/tmp')
print 'Out of %d files on one side, and %d on the other,' % (
len(x.left_list), len(x.right_list))
print '%d are in common (%d files, %d directories, %d funny ones)' % (
len(x.common), len(x.common_files), len(x.common_dirs),
len(x.common_funny))

or whatever it is that you do wish.
Alex
Jul 18 '05 #4
> But why would you want that?

Because I don't know any better? Seriously. I like Python, but I'm
not a programmer. I'm more of a power user. I use Python (or batch
files or VB), to perform a specific task and that's it. I might use
whatever language, or tool for a few weeks and then not go back to it
until I need it again, which could be six months or 2 years, at which
point I have to learn everything that I have forgotten all over again.
Such is the lot of a tester. I long for the cyperpunk future where I
can download all of my Python knowledge to a chip and upload it when I
need it (or better yet someone else's knowledge) with the knowledge as
fresh as if I had just learned it.
Jul 18 '05 #5
On Tue, Sep 14, 2004 at 10:08:25AM +0200, Alex Martelli wrote:
Robin Siebler <ro***********@palmsource.com> wrote:
I want to use filecmp.dircmp, but it only prints to the screen, there
is no way to capture the output. So I thought that I should simply be


Il prints to sys.stdout, which may be the screen or elsewhere, therefore
there IS of course a way to capture the output:

import sys, filecmp, cStringIO

save_stdout = sys.stdout


I believe sys.__stdout__ is there for the purpose of not having to
'save' sys.stdout in this way.

--
John Lenton (jo**@grulic.org.ar) -- Random fortune:
Q: "What is the burning question on the mind of every dyslexic
existentialist?"
A: "Is there a dog?"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBRzYRgPqu395ykGsRAn/MAKCe0u0Gps4sK4FjaIcyKeA8Nq6ifwCfdmRI
8vwtCzqdAV1MEiPQ8U/P+G4=
=wYvU
-----END PGP SIGNATURE-----

Jul 18 '05 #6
In article <ma**************************************@python.o rg>,
John Lenton <jo**@grulic.org.ar> wrote:
On Tue, Sep 14, 2004 at 10:08:25AM +0200, Alex Martelli wrote:
Robin Siebler <ro***********@palmsource.com> wrote:
I want to use filecmp.dircmp, but it only prints to the screen, there
is no way to capture the output. So I thought that I should simply be


Il prints to sys.stdout, which may be the screen or elsewhere, therefore
there IS of course a way to capture the output:

import sys, filecmp, cStringIO

save stdout = sys.stdout


I believe sys.__stdout__ is there for the purpose of not having to
'save' sys.stdout in this way.


Nope, it's there so you can get at the original stdout, which is not
necessarily the same thing...

Just
Jul 18 '05 #7
John Lenton <jo**@grulic.org.ar> wrote:
...
save_stdout = sys.stdout


I believe sys.__stdout__ is there for the purpose of not having to
'save' sys.stdout in this way.


Hmmm, almost, but not quite. If you somehow "know" that your sys.stdout
has not been set by some other module, typically a GUI framework, then,
sure, you don't need to copy sys.stdout yourself. But if you get into
that habit, eventually you WILL get bitten, when you end up interfering
with exactly that kind of "other framework". Personally, I far prefer
to cultivate the habit of a full-fledged idiom such as:

redirected_stdout = cStringIO.StringIO()
save_stdout = sys.stdout
sys.stdout = redirected_stdout
try:
...something that does prints...
finally:
sys.stdout = save_stdout
...use redirected_stdout.value...
redirectred_stdout.close()
Alex
Jul 18 '05 #8

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

Similar topics

6
by: WhiteRavenEye | last post by:
Why can't I subclass any window except mine in VB? Do I have to write dll for this? I've tried to subclass it with SetWindowLong but without success... Does anyone know how to subclass window...
4
by: GrelEns | last post by:
hello, i wonder if this possible to subclass a list or a tuple and add more attributes ? also does someone have a link to how well define is own iterable object ? what i was expecting was...
2
by: David Vaughan | last post by:
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...
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: alotcode | last post by:
Hello: What is 'interprocess subclassing'? To give more context, I am writing in reference to the following remark: With the advent of the Microsoft Win32 API, interprocess subclassing was...
4
by: pietlinden | last post by:
Hi, I have the sort of standard Donors/Donations DB. The twist I have is that donations can be time, money (which are easy, because they're additive), but then a donor can make an in-kind pledge,...
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...
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...
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.