[issue11344] Add height argument to os.path.dirname()

2011-03-08 Thread Antoine Pitrou

Antoine Pitrou  added the comment:

> If I wrote the patches, tests and docs, what are the chances of it
> being accepted?

Rather high as far as I'm concerned. Be careful with semantics and 
implementation under Windows, though (you should probably take a look at 
existing functions in ntpath.py as a guideline).

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11344] Add height argument to os.path.dirname()

2011-03-08 Thread blokeley

blokeley  added the comment:

os.path.splitpath() as described by rhettinger would solve the problem.

If I wrote the patches, tests and docs, what are the chances of it being 
accepted?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11344] Add height argument to os.path.dirname()

2011-03-05 Thread R. David Murray

R. David Murray  added the comment:

Ah, yes, splitpath is a function I've occasionally wanted.  I also remember 
being surprised that os.path.split didn't return such a list.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11344] Add height argument to os.path.dirname()

2011-03-04 Thread Raymond Hettinger

Raymond Hettinger  added the comment:

> My point was that unix hasn't found it useful 
> to add a level option to the dirname API.

ISTM, that is a strong indication that this isn't needed in the form it has 
been proposed.

> I don't know that I personally have ever had 
> occasion to peel off more than one directory level 
> without also wanting to do something with the 
> intermediate results, so perhaps I am not a good 
> judge of how useful this would be.

I think this only arises when a known directory structure has been attached at 
some arbitrary point on a tree, so you might use a relative path like 
../../bin/command.py in the shell.  To serve that use case, it would be better 
to have a function that splits all the components of the path into a list 
that's easily manipulated:

>>> oldpath = os.path.splitpath('/ggparent/gparent/parent/')
>>> newpath = oldpath[:-2] + ['bin', 'command.py']
>>> os.path.join(*newpath)
'/ggparent/bin/command.py'

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11344] Add height argument to os.path.dirname()

2011-03-04 Thread Antoine Pitrou

Antoine Pitrou  added the comment:

As for use cases, I have used it quite commonly in test scripts in order to 
find out the base directory of the source tree (and/or other resources such as 
data files).

e.g.:

basepath = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11344] Add height argument to os.path.dirname()

2011-03-04 Thread Antoine Pitrou

Antoine Pitrou  added the comment:

Except that dirname() isn't a one-liner, so you are giving rather bad advice 
here.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11344] Add height argument to os.path.dirname()

2011-03-04 Thread Terry J. Reedy

Terry J. Reedy  added the comment:

I am inclined to -1 also.

a. The proposed behavior is anti-obvious to me: the higher the height, the 
shorter the result. Calling param 'drop' would be better.

b. Not every one-liner should be wrapped.

>>> path.rsplit('/',0)[0]
'/ggparent/gparent/parent/myfile.txt'
>>> path.rsplit('/',1)[0]
'/ggparent/gparent/parent'
>>> path.rsplit('/',2)[0]
'/ggparent/gparent'
>>> path.rsplit('/',3)[0]
'/ggparent'

Note: above gives '' for maxsplit out of range, easily converted to exception 
in function wrapper.

--
nosy: +terry.reedy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11344] Add height argument to os.path.dirname()

2011-02-27 Thread R. David Murray

R. David Murray  added the comment:

No, it isn't a design principle.  My point was that unix hasn't found it useful 
to add a level option to the dirname API.

I don't know that I personally have ever had occasion to peel off more than one 
directory level without also wanting to do something with the intermediate 
results, so perhaps I am not a good judge of how useful this would be.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11344] Add height argument to os.path.dirname()

2011-02-27 Thread Antoine Pitrou

Antoine Pitrou  added the comment:

Well, on the other hand, it *is* a common need.
(and I don't think mimicking the shell is a design principle for Python)

--
nosy: +pitrou

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11344] Add height argument to os.path.dirname()

2011-02-27 Thread R. David Murray

R. David Murray  added the comment:

I'm -1 on this feature request.  I think it is an unnecessary complication of 
the API, especially since dirname corresponds to the unix shell 'dirname' 
command, which doesn't have such a feature.  If you need this feature in a 
particular application, it is easy to write a function to provide it.

--
nosy: +r.david.murray

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11344] Add height argument to os.path.dirname()

2011-02-27 Thread blokeley

New submission from blokeley :

It is a common need to find the grandparent or great-grandparent (etc.) of a 
given directory, which results in this:

>>> from os.path import dirname
>>> mydir = dirname(dirname(dirname(path)))

Could a "height" parameter be added to os.path.dirname so it becomes:

>>> def dirname(path, height=1):

Then we could have usage like:

>>> path = '/ggparent/gparent/parent/myfile.txt'
>>> from os.path import dirname

>>> dirname(path)
/ggparent/gparent/parent

>>> dirname(path, 2)
/ggparent/gparent

>>> dirname(path, 3)
/ggparent

Perhaps we should throw ValueErrors for invalid height values:

>>> dirname(path, 10)
ValueError

>>> dirname(path, -1)
ValueError

Perhaps a height of 0 should do nothing:

>>> dirname(path, 0)
/ggparent/gparent/parent/myfile.txt

I can supply patches, unit tests and docs if you like.

--
components: Library (Lib)
messages: 129616
nosy: blokeley
priority: normal
severity: normal
status: open
title: Add height argument to os.path.dirname()
type: feature request
versions: Python 3.3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com