[Python-3000] Mini Path object

Antoine Pitrou solipsis at pitrou.net
Tue Nov 7 00:08:08 CET 2006


Le lundi 06 novembre 2006 à 14:37 -0800, Mike Orr a écrit :
>     def __init__(klass, *args):
>         if len(args) == 1 and isinstance(args[0], klass.path_class):
>             self.path = args[0]
>         else:
>             self.path = self.path_class(*args)

s/klass/self/, I suppose ?

> Subclassing unicode would be the simplest implementation.  The PEP 355
> implementation does a unicode-or-str dance in case
> os.path.supports_unicode_filenames is false.  Is it really necessary
> to support this nowadays?

I'm not sure what you mean, but on Mandriva Linux:

$ python
Python 2.4.3 (#2, Sep 18 2006, 21:07:35) 
[GCC 4.1.1 20060724 (prerelease) (4.1.1-3mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.supports_unicode_filenames
False

However:
>>> os.path.exists(u"/etc/passwd")
True
>>> os.path.exists("/etc/passwd")
True
>>> os.path.exists(u"éléphant")
True
>>> os.path.exists("éléphant")
True

(after having run "touch éléphant")


>     Path("a/b").components[0:1] => Path("a/b")
> 
> Is there a problem with .component returning a Path instead of a list
> of components?

Having a "list slice" returning a non-sequence result is a bit
surprising; the plural "components" is also misleading. Why not simply a
method, and why not name it "subpath" ?

	Path("a/b/c").subpath(0, 2) => Path("a/b")
	Path("a/b/c").subpath(0, -1) => Path("a/b")

By the way, is the absolute root a separate "component"?

	Path("/a/b/c").subpath(0, 1) => Path("/")
	Path("/a/b/c").subpath(1, 2) => Path("a")
	Path("/a/b/c").subpath(1) => Path("a/b/c")





More information about the Python-3000 mailing list