My first attempt at subclassing....Gahh!

R

Robin Siebler

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)
 
D

Dan Perl

Robin Siebler said:
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.
 
A

Alex Martelli

Robin Siebler said:
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
 
R

Robin Siebler

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.
 
J

John Lenton

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 ([email protected]) -- 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-----
 
J

Just

John Lenton said:
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
 
A

Alex Martelli

John Lenton said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top