By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,916 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,916 IT Pros & Developers. It's quick & easy.

Problems trying to override __str__ on path class

P: n/a
I'm running into problems trying to override __str__ on the path class
from Jason Orendorff's path module
(http://www.jorendorff.com/articles/p...h/src/path.py).

My first attempt to do this was as follows:

'''
class NormPath(path):
def __str__(self):
return 'overridden __str__: ' + path.__str__(self.normpath())
'''

The problem is that the override is not invoked unless str() is called
explictly, as indicated by the test program and its output below:

'''
from normpath import NormPath
np = NormPath('c:/mbk/test')
print 'np: "%s"' % np
print 'str(np): "%s"' % str(np)
print np / 'appendtest'
np: "c:/mbk/test"
str(np): "overridden __str__: c:\mbk\test"
c:/mbk/test\appendtest
'''

I suspect that the problem has to do with the base class of the path
class being unicode because it works when I create dummy classes derived
off of object.

My next attempt was to try delegation as follows:

'''
class NormPath(object):
def __init__(self, *a, **k):
self._path = path(*a, **k)

def __str__(self):
return 'overridden __str__: ' + str(self._path.normpath())

def __getattr__(self, attr):
print 'delegating %s...' % attr
return getattr(self._path, attr)
'''

In this case the test program blows up with a TypeError when trying to
invoke the / operator:

'''
np: "overridden __str__: c:\mbk\test"
str(np): "overridden __str__: c:\mbk\test"
-------------------------------------------------------------------------
exceptions.TypeError Traceback (most
recent call last)

e:\projects\Python\vc\nptest.py
1 from normpath import NormPath
2 np=NormPath('c:/mbk/test')
3 print 'np: "%s"' % np
4 print 'str(np): "%s"' % str(np)
----5 print np / 'appendtest'

TypeError: unsupported operand type(s) for /: 'NormPath' and 'str'
WARNING: Failure executing file: <nptest.py>
'''

Can someone explain these failures to me? Also, assuming I don't want to
modify path.py itself, is there any way to do what I'm trying to
accomplish? BTW, I'm running 2.4.2 under Windows.

Thanks in advance,
Mike
Oct 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Mike Krell wrote:
I'm running into problems trying to override __str__ on the path class
from Jason Orendorff's path module
(http://www.jorendorff.com/articles/p...h/src/path.py).

My first attempt to do this was as follows:

'''
class NormPath(path):
def*__str__(self):
return*'overridden*__str__:*'*+*path.__str__(self. normpath())
'''

The problem is that the override is not invoked unless str() is called
explictly, as indicated by the test program and its output below:

'''
from normpath import NormPath
np = NormPath('c:/mbk/test')
print 'np: "%s"' % np
print 'str(np): "%s"' % str(np)
print np / 'appendtest'
np: "c:/mbk/test"
str(np): "overridden __str__: c:\mbk\test"
c:/mbk/test\appendtest
'''
With

from path import path

class NormPath(path):
def __str__(self):
return 'overridden __str__: ' + path.__str__(self.normpath())

np = NormPath('c:/mbk/test')
print 'np: "%s"' % np
print 'str(np): "%s"' % str(np)
print np / 'appendtest'

I get

np: "overridden __str__: c:/mbk/test"
str(np): "overridden __str__: c:/mbk/test"
overridden __str__: overridden __str__: c:/mbk/test/appendtest

Are you using the latest version of the path module? Older versions implied
a Path() call in the __div__() operator which would explain at least the
output you get for

print np / 'appendtest'

Peter

Oct 22 '06 #2

P: n/a
Peter Otten <__*******@web.dewrote in
news:eh*************@news.t-online.com:
I get

np: "overridden __str__: c:/mbk/test"
str(np): "overridden __str__: c:/mbk/test"
overridden __str__: overridden __str__: c:/mbk/test/appendtest
Hmmm. I guess you're not running under windows, since normpath()
converts / to \ on windows. I wonder if that's part of the problem.

Are you using the latest version of the path module?
My path.py says it's 2.1, which is the latest according to the site.

Older versions
implied a Path() call in the __div__() operator which would explain at
least the output you get for

print np / 'appendtest'
You've lost me here. What do you mean about "implied a Path() call"?
Here is the definition of __div__ from path.py:

'''
# The / operator joins paths.
def __div__(self, rel):
""" fp.__div__(rel) == fp / rel == fp.joinpath(rel)

Join two path components, adding a separator character if
needed.
"""
return self.__class__(os.path.join(self, rel))
'''

I still don't understand the TypeError in the delegation case.

Mike
Oct 23 '06 #3

P: n/a
Mike Krell wrote:
Peter Otten <__*******@web.dewrote in
news:eh*************@news.t-online.com:
>I get

np: "overridden __str__: c:/mbk/test"
str(np): "overridden __str__: c:/mbk/test"
overridden __str__: overridden __str__: c:/mbk/test/appendtest

Hmmm. I guess you're not running under windows, since normpath()
converts / to \ on windows. I wonder if that's part of the problem.

>Are you using the latest version of the path module?

My path.py says it's 2.1, which is the latest according to the site.

Older versions
>implied a Path() call in the __div__() operator which would explain at
least the output you get for

print np / 'appendtest'

You've lost me here. What do you mean about "implied a Path() call"?
Here is the definition of __div__ from path.py:

'''
# The / operator joins paths.
def __div__(self, rel):
""" fp.__div__(rel) == fp / rel == fp.joinpath(rel)

Join two path components, adding a separator character if
needed.
"""
return self.__class__(os.path.join(self, rel))
'''
From http://www.jorendorff.com/articles/p.../changes.html:

"""
This page describes recent changes to the Python path module.

2.1

[...]

Better support for subclassing path. All methods that returned path objects
now respect subclassing.

Before: type(PathSubclass(s).parent) is path

Now: type(PathSubclass(s).parent) is PathSubclass
"""

So my assumption was that you are using a pre-2.1 version of path.
I suggest that you double-check that by inserting a

print path.__version__

into the code showing the odd behaviour before you start looking for more
exotic causes.
I still don't understand the TypeError in the delegation case.
For newstyle classes

a = A()
a / 42 # or any other operation implemented via __xxx__()

(unlike a.__div__) will only look into the class for a __div__() special
method and ignore methods defined in the instance or via the __getattr__()
mechanism.

For delegation to work you need (untested)

class NormPath(object):
def __div__(self, other):
return self.__class__(self._path / other)
# ...

Peter
Oct 23 '06 #4

P: n/a
Peter Otten <__*******@web.dewrote in
news:eh*************@news.t-online.com:
>
So my assumption was that you are using a pre-2.1 version of path.
I suggest that you double-check that by inserting a

print path.__version__

into the code showing the odd behaviour before you start looking for
more exotic causes.
Alas, the print statement says "2.1". So there's a definite platform /
environment difference here, but that isn't it.
For delegation to work you need (untested)

class NormPath(object):
def __div__(self, other):
return self.__class__(self._path / other)
# ...
I see. The base class implementation is throwing up its hands at the
unknown type NormPath. One needs to substitute the parameter in every
case where the base class implementation is called. That kind of kills
the attractiveness of the technique for new style classes, unless a way
could be done to do it programatically instead of explicitly for each
method. Of course, if I could get the simple override to work, the need
to do this would disappear.

Mike
Oct 23 '06 #5

P: n/a
Mike Krell wrote:
Alas, the print statement says "2.1".**So*there's*a*definite*platform*/
environment difference here, but that isn't it.
It turns out the observed difference is only indirectly triggered by the
differing platforms. On my machine the path baseclass is str. If I change
it to unicode (by patching path.py), I get the same output that you had.
The problem can be reduced to
>>class A(str):
.... def __str__(self): return "yadda"
....
>>"%s" % A(), str(A())
('yadda', 'yadda')
>>class B(unicode):
.... def __str__(self): return "yadda"
....
>>"%s" % B(), str(B())
(u'', 'yadda')

So Python itself doesn't honor an overridden __str__() method for the "%s"
format. Implementing __unicode__() doesn't help, either:
>>class C(unicode):
.... def __unicode__(self): return u"YADDA"
.... def __str__(self): return "yadda"
....
>>"%s" % C(), unicode(C())
(u'', u'')

Somewhere there is an isinstance() test where there should be a test for the
exact class. Seems like a bug to me.

Peter

Oct 23 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.