[Python-checkins] python/nondist/sandbox/path test_path.py, 1.2, 1.3 path.py, 1.2, 1.3
birkenfeld@users.sourceforge.net
birkenfeld at users.sourceforge.net
Sun Jul 24 14:42:54 CEST 2005
Update of /cvsroot/python/python/nondist/sandbox/path
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14122
Modified Files:
test_path.py path.py
Log Message:
* subdirs() is now dirs().
* fixed compare behaviour for unicode base (unicode has no rich compare)
* __iter__() iterates over the parts().
* the following methods raise NotImplemented:
capitalize, expandtabs, join, splitlines, title, zfill
Index: test_path.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/path/test_path.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- test_path.py 23 Jul 2005 09:27:40 -0000 1.2
+++ test_path.py 24 Jul 2005 12:42:51 -0000 1.3
@@ -59,7 +59,7 @@
self.assertEqual(d.relpathto(boz), boz)
## this isn't going to work cause Paths won't compare
- ## with ordinary strings
+ ## to ordinary strings
'''
def testStringCompatibility(self):
""" Test compatibility with ordinary strings. """
@@ -79,6 +79,12 @@
self.assert_(items == ['A', 'B', 'E', 'c', 'd', 'fgh', 'fhj'])
'''
+ def testUnusableMethods(self):
+ p = Path()
+ for method in [p.capitalize, p.expandtabs, p.join, p.splitlines,
+ p.title, p.zfill]:
+ self.assertRaises(NotImplementedError, method)
+
def testProperties(self):
# Create sample Path object.
f = p(nt='C:\\Program Files\\Python\\Lib\\xyzzy.py',
@@ -118,6 +124,16 @@
self.assert_(p.uncshare == r'\\python1\share1')
self.assert_(p.splitunc() == os.path.splitunc(str(p)))
+ def testCompare(self):
+ p1 = Path('/')
+ lst = [p1, Path('/usr/'), Path('/usr/share')]
+ p2 = Path('/')
+ # a Path is not interned, so these are different objects
+ assert p1 is not p2
+ # but they must compare the same
+ self.assert_(p1 == p2)
+ self.assert_(p2 in lst)
+
class TempDirTestCase(unittest.TestCase):
def setUp(self):
# Create a temporary directory.
@@ -349,7 +365,7 @@
(e/name).touch()
self.assertList(d.children('*.tmp'), [d/'x.tmp', d/'xdir.tmp'])
self.assertList(d.files('*.tmp'), [d/'x.tmp'])
- self.assertList(d.subdirs('*.tmp'), [d/'xdir.tmp'])
+ self.assertList(d.dirs('*.tmp'), [d/'xdir.tmp'])
self.assertList(d.walk(), [e for e in dirs if e != d] + [e/n for e in dirs for n in names])
self.assertList(d.walk('*.tmp'),
[e/'x.tmp' for e in dirs] + [d/'xdir.tmp'])
Index: path.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/path/path.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- path.py 23 Jul 2005 09:27:40 -0000 1.2
+++ path.py 24 Jul 2005 12:42:51 -0000 1.3
@@ -63,6 +63,10 @@
return Path(os.curdir)
return _base.__new__(typ, *args)
+ # Iterating over a string yields its parts
+ def __iter__(self):
+ return iter(self.parts())
+
def __repr__(self):
return 'Path(%s)' % repr(_base(self))
@@ -95,30 +99,37 @@
return self.joinwith(other)
__truediv__ = __div__
-
- # Rich comparison
- def __eq__(self, other):
- return isinstance(other, Path) and _base.__eq__(self, other)
-
- def __ne__(self, other):
- return (not isinstance(other, Path)) or _base.__ne__(self, other)
- def __lt__(self, other):
- if isinstance(other, Path):
- return _base.__lt__(self, other)
- return NotImplemented
- def __le__(self, other):
- if isinstance(other, Path):
- return _base.__le__(self, other)
- return NotImplemented
- def __gt__(self, other):
- if isinstance(other, Path):
- return _base.__gt__(self, other)
- return NotImplemented
- def __ge__(self, other):
- if isinstance(other, Path):
- return _base.__ge__(self, other)
- return NotImplemented
+ if _base is str:
+ # Rich comparison for string
+ def __eq__(self, other):
+ return isinstance(other, Path) and _base.__eq__(self, other)
+
+ def __ne__(self, other):
+ return (not isinstance(other, Path)) or _base.__ne__(self, other)
+
+ def __lt__(self, other):
+ if isinstance(other, Path):
+ return _base.__lt__(self, other)
+ return NotImplemented
+ def __le__(self, other):
+ if isinstance(other, Path):
+ return _base.__le__(self, other)
+ return NotImplemented
+ def __gt__(self, other):
+ if isinstance(other, Path):
+ return _base.__gt__(self, other)
+ return NotImplemented
+ def __ge__(self, other):
+ if isinstance(other, Path):
+ return _base.__ge__(self, other)
+ return NotImplemented
+ else:
+ # Unicode has no rich compare methods
+ def __cmp__(self, other):
+ if isinstance(other, Path):
+ return _base.__cmp__(self, other)
+ return NotImplemented
# Alternative constructor.
@@ -350,7 +361,7 @@
""" D.children() -> List of items in this directory,
with this path prepended to them.
- Use D.files() or D.subdirs() instead if you want a listing
+ Use D.files() or D.dirs() instead if you want a listing
of just files or just subdirectories.
The elements of the list are path objects.
@@ -363,8 +374,8 @@
names = fnmatch.filter(names, pattern)
return [self / child for child in names]
- def subdirs(self, pattern=None):
- """ D.subdirs() -> List of this directory's subdirectories.
+ def dirs(self, pattern=None):
+ """ D.dirs() -> List of this directory's subdirectories.
The elements of the list are path objects.
This does not walk recursively into subdirectories
@@ -372,7 +383,7 @@
With the optional 'pattern' argument, this only lists
directories whose names match the given pattern. For
- example, d.subdirs('build-*').
+ example, d.dirs('build-*').
"""
return [p for p in self.children(pattern) if p.isdir()]
@@ -414,7 +425,7 @@
example, mydir.walkdirs('*test') yields only directories
with names ending in 'test'.
"""
- for child in self.subdirs():
+ for child in self.dirs():
if pattern is None or child.fnmatch(pattern):
yield child
for subsubdir in child.walkdirs(pattern):
@@ -864,3 +875,14 @@
def startfile(self):
os.startfile(self)
+ # --- Unusable string methods
+
+ def _not_implemented(self, *args, **kwargs):
+ raise NotImplementedError("not implemented for Path objects")
+
+ capitalize = _not_implemented
+ expandtabs = _not_implemented
+ join = _not_implemented
+ splitlines = _not_implemented
+ title = _not_implemented
+ zfill = _not_implemented
More information about the Python-checkins
mailing list