[path-PEP] Path inherits from basestring again
Reinhold Birkenfeld
reinhold-birkenfeld-nospam at wolke7.net
Sun Jul 31 03:48:45 EDT 2005
phil hunt wrote:
> On Sat, 30 Jul 2005 19:01:49 +0200, Reinhold Birkenfeld <reinhold-birkenfeld-nospam at wolke7.net> wrote:
>>phil hunt wrote:
>>
>>> def normalizePath(p, *pathParts):
>>> """ Normalize a file path, by expanding the user name and getting
>>> the absolute path..
>>> @param p [string] = a path to a file or directory
>>> @param pathParts [list of string] = optional path parts
>>> @return [string] = the same path, normalized
>>> """
>>> p1 = os.path.abspath(os.path.expanduser(p))
>>> if len(pathParts)>0:
>>> allPathParts = [ p1 ]
>>> allPathParts.extend(pathParts)
>>> p1 = os.path.join(*allPathParts)
>>> p2 = os.path.abspath(p1)
>>> return p2
>>> normalisePath=normalizePath # alternate spelling
>>> join=normalizePath # it works like os.path.join, but better
>>>
>>>
>>> To be honest I don't see the point of having a Path class. That's
>>> the way Java does it, and I find path handling in Java to be a lot
>>> more of a hassle than in Python. (Actually, most things are more of
>>> a hassle in Java, but that's another story).
>>
>>You see, with the Path class the above function could be written as
>>
>>def normalizePath(p, *pathParts):
>> """ Normalize a file path, by expanding the user name and getting
>> the absolute path..
>> @param p [Path] = a path to a file or directory
>> @param pathParts [list of string/Path] = optional path parts
>> @return [Path] = the same path, normalized
>> """
>> tp = p.expanduser().abspath()
>> return tp.joinwith(*pathParts).abspath()
>>
>>That's clearly an improvement, isn't it?
>
> An improvement to what? To how the class is implemented, or to how
> it is used?
No, the second function is cleaner and more readable than the first,
IMHO.
> If you mean the former, yes is it, due to the os.path module not
> providing a function that does this.
>
> If you mean the latter, I disagree, because I would then have to
> call it with something like:
>
> pn = normalizePath(Path(p), q)
That's easily helped by s/tp = p/tp = Path(p)/.
> and then I would have the problem that (pn) isn't a string so
> calling a function to write some data into the file at that filename
> would no longer work, i.e. this:
>
> writeFile(pn, someData)
>
> would become this:
>
> writeFile(pn.getString(), someData)
Why? A Path is a string.
Reinhold
More information about the Python-list
mailing list