Problems trying to override __str__ on path class

M

Mike Krell

I'm running into problems trying to override __str__ on the path class
from Jason Orendorff's path module
(http://www.jorendorff.com/articles/python/path/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
 
P

Peter Otten

Mike said:
I'm running into problems trying to override __str__ on the path class
from Jason Orendorff's path module
(http://www.jorendorff.com/articles/python/path/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
 
M

Mike Krell

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
 
P

Peter Otten

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



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

Older versions

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/python/path/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
 
M

Mike Krell

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
 
P

Peter Otten

Mike said:
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
.... def __str__(self): return "yadda"
........ def __str__(self): return "yadda"
....(u'', 'yadda')

So Python itself doesn't honor an overridden __str__() method for the "%s"
format. Implementing __unicode__() doesn't help, either:
.... def __unicode__(self): return u"YADDA"
.... def __str__(self): return "yadda"
....(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
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top