[Python-Dev] Alternative path suggestion
Nick Coghlan
ncoghlan at gmail.com
Sat May 6 09:21:49 CEST 2006
Mike Orr wrote:
> I think you meant to say Perl in that sentence. In Python there
> should be one way to do it, and beautiful is better than ugly. The
> os.path functions are bad enough, but shutil.copy2 is just plain evil.
I agree a decent pathlib may make many of the low-level filesystem
manipulation modules obsolete - just not necessarily all of them. Until we get
an actual module to experiment with, we won't really know.
> Is it that much of a step to translate:
>
> Y="$(dirname $(dirname $X))/lib"
>
> as
>
> y = Path(x).parent.parent + "lib"
> y = Path(x).parent.parent.join("lib")
> or whatever the syntax do jour is
I'd suggest something like:
y = Path(x).parent(2) + "lib/"
Where the parent function works like (based on the three-part internal data
approach I posted recently):
def parent(self, generation=1):
return type(self).from_parts(
self.basepath, self.dirparts[:-generation], ())
And path addition is equivalent to:
def __add__(self, other):
if isinstance(other, basestring):
other = Path(other)
if self.nameparts:
raise ValueError("Cannot add to path with non-empty name")
if other.basepath:
raise ValueError("Cannot append non-relative path")
dirparts = self.dirparts + other.dirparts
return type(self).from_parts(
self.basepath, dirparts, other.nameparts)
Anyway, I'll try to find some time to look at this on the Wiki. It'll be
easier to bat code back and forth over there.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-Dev
mailing list