[issue39779] [argparse] Add parameter to sort help output arguments

2020-02-28 Thread brian.gallagher


brian.gallagher  added the comment:

That makes sense. For what it's worth, the use-case that inspired this was for 
commands with a lot of optional arguments in a company where a large amount of 
contributors (who may not be aware of an effort to order the arguments in the 
source code) were able to make changes to the command.

I understand that isn't a particularly compelling reason though, as it can be 
addressed by other means -- increasing diligence at the code review stage, 
commit hooks, testing, etc.

Thanks for taking a look Raymond.

--

___
Python tracker 

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



[issue39779] [argparse] Add parameter to sort help output arguments

2020-02-27 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Thanks for the suggestion, but I am going to decline.

Since the parser remembers the order the arguments were added, the programmer 
already has complete control over the ordering of arguments.

--
nosy: +rhettinger
resolution:  -> rejected
stage:  -> resolved
status: open -> closed

___
Python tracker 

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



[issue39779] [argparse] Add parameter to sort help output arguments

2020-02-27 Thread brian.gallagher


New submission from brian.gallagher :

1 import argparse   

  2 
  
  3 parser = argparse.ArgumentParser(description='Test')
  
  4 parser.add_argument('c', help='token c')
  
  5 parser.add_argument('b', help='token b')
  
  6 parser.add_argument('d', help='token d')
  
  7 parser.add_argument('-a', help='token a')   
  
  8 parser.add_argument('-z', help='token z')   
  
  9 parser.add_argument('-f', help='token f', required=True)


 10 parser.print_help() 

It would be nice if we could have the option to alphabetically sort the tokens 
in the optional and positional arguments sections of the help message in order 
to find an argument more quickly when reading long help descriptions.

Currently we output the following, when the above program is ran:

positional arguments:
  c   token c
  b   token b
  d   token d

optional arguments:
  -h, --help  show this help message and exit
  -a Atoken a
  -z Ztoken z
  -f Ftoken f

I'm proposing that we provide a mechanism to allow alphabetical ordering of 
both sections, like so:

positional arguments:
  b   token b
  c   token c
  d   token d

optional arguments:
  -h, --help  show this help message and exit
  -a Atoken a
  -f Ftoken f
  -z Ztoken z

I've chosen to leave -h as an exception, as it will always be there as an 
optional argument, but it could easily be treated no different.

We could provide an optional argument to print_help(sort=False) as a potential 
approach.

If this is something that the maintainer's would be willing to accept, I'd love 
to take it on and prepare a patch.

--
components: Library (Lib)
messages: 362849
nosy: brian.gallagher
priority: normal
severity: normal
status: open
title: [argparse] Add parameter to sort help output arguments
type: enhancement

___
Python tracker 
<https://bugs.python.org/issue39779>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: sort help

2015-09-23 Thread Larry Martell
On Tue, Sep 22, 2015 at 6:55 PM, Chris Angelico  wrote:
> On Wed, Sep 23, 2015 at 8:42 AM, Larry Martell  
> wrote:
>> I currently have 3 lists of lists and I sort them based on a common
>> field into a single list like this:
>>
>> def GetObjKey(a):
>> return a[2]
>>
>> sorted(a + b + c, key=GetObjKey)
>>
>> Which works just fine.
>>
>> But now, I need to have just the first list (a) also sub sorted by
>> another field and I can't quite figure out how to do this.
>
> Have you tried simply sorting a by the other field prior to doing your
> merge-and-sort? The Python list.sort() method is guaranteed to be
> stable. I can't find a comparable guarantee for sorted(), but worst
> case, you should be able to do your list merge, and then explicitly
> name it and sort it.

Thanks to everyone for the replied. I ended up just presorting he
first list, then merging and sorting all 3. Very simple. Not sure why
I didn't see that. Probably comes from working 75 hours/week.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: sort help

2015-09-23 Thread Peter Otten
Larry Martell wrote:

> I currently have 3 lists of lists and I sort them based on a common
> field into a single list like this:
> 
> def GetObjKey(a):
> return a[2]
> 
> sorted(a + b + c, key=GetObjKey)
> 
> Which works just fine.
> 
> But now, I need to have just the first list (a) also sub sorted by
> another field and I can't quite figure out how to do this.
> 
> So for example, if my initial data was this (I'll only show the fields
> involved with the sort - the first number is a[2] above and the second
> is the new additional sorting field, only present in a)
> 
> a[1, 4]
> a[1, 2]
> a[2, 3]
> a[2, 1]
> a[5, 6]
> a[5, 2]
> b[2]
> b[5]
> c[1]
> c[6]
> 
> Then I'd want my sorted list to be this:
> 
> a[1,2]
> a[1,4]
> c[1]
> a[2,1]
> a[2,3]
> b[2]
> a[5,2]
> a[5,6]
> b[5]
> c[6]
> 
> I hope that's clear.
> 
> So is there some pythonic way to sort this without resorting to a
> brute force old fashioned plow through the data?

Performing two sorts as suggested by Chris and Paul is cleaner, but it is 
possible to write the key function you were probably looking for.

def get_obj_key(a):
if has_extra_field(a):
return a[2], 0, a[-1] # assuming a[-1] is the extra field
else:
return a[2], 1

You have to write the has_extra_field() test yourself as I don't know what 
distinguishes the items in a from those in the other sequences. For example:

>>> a = [[1, 4], [1, 2], [2, 3], [2, 1], [5, 6], [5, 2]]
>>> b = [[2], [5]]
>>> c = [[1], [6]]
>>> sorted(a + b + c,
... key=lambda a: (a[0], 0, a[-1]) if len(a) > 1 else (a[0], 1))
[[1, 2], [1, 4], [1], [2, 1], [2, 3], [2], [5, 2], [5, 6], [5], [6]]


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: sort help

2015-09-22 Thread Paul Rubin
Larry Martell  writes:
> def GetObjKey(a):
> return a[2]

This function is called operator.itemgetter:

from operator import itemgetter
sorted(a + b + c, key=itemgetter(2))

> So for example, if my initial data was this (I'll only show the fields
> involved with the sort - the first number is a[2] above and the second
> is the new additional sorting field, only present in a)

  sorted(sorted(a, key=itemgetter(5)) + b + c, key = itemgetter(2))

or whatever the new field (instead of 5) is for a.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: sort help

2015-09-22 Thread Chris Angelico
On Wed, Sep 23, 2015 at 9:02 AM, Ian Kelly  wrote:
> On Tue, Sep 22, 2015 at 4:55 PM, Chris Angelico  wrote:
>> The Python list.sort() method is guaranteed to be
>> stable. I can't find a comparable guarantee for sorted()
>
> https://docs.python.org/3.5/library/functions.html#sorted

Right, sorry. Since all I looked at was its docstring, I should have
said so :) So, yep, sorted() is guaranteed stable too, and that would
be the easiest way to subsort something.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: sort help

2015-09-22 Thread Ian Kelly
On Tue, Sep 22, 2015 at 4:55 PM, Chris Angelico  wrote:
> The Python list.sort() method is guaranteed to be
> stable. I can't find a comparable guarantee for sorted()

https://docs.python.org/3.5/library/functions.html#sorted
-- 
https://mail.python.org/mailman/listinfo/python-list


sort help

2015-09-22 Thread Larry Martell
I currently have 3 lists of lists and I sort them based on a common
field into a single list like this:

def GetObjKey(a):
return a[2]

sorted(a + b + c, key=GetObjKey)

Which works just fine.

But now, I need to have just the first list (a) also sub sorted by
another field and I can't quite figure out how to do this.

So for example, if my initial data was this (I'll only show the fields
involved with the sort - the first number is a[2] above and the second
is the new additional sorting field, only present in a)

a[1, 4]
a[1, 2]
a[2, 3]
a[2, 1]
a[5, 6]
a[5, 2]
b[2]
b[5]
c[1]
c[6]

Then I'd want my sorted list to be this:

a[1,2]
a[1,4]
c[1]
a[2,1]
a[2,3]
b[2]
a[5,2]
a[5,6]
b[5]
c[6]

I hope that's clear.

So is there some pythonic way to sort this without resorting to a
brute force old fashioned plow through the data?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: sort help

2015-09-22 Thread Chris Angelico
On Wed, Sep 23, 2015 at 8:42 AM, Larry Martell  wrote:
> I currently have 3 lists of lists and I sort them based on a common
> field into a single list like this:
>
> def GetObjKey(a):
> return a[2]
>
> sorted(a + b + c, key=GetObjKey)
>
> Which works just fine.
>
> But now, I need to have just the first list (a) also sub sorted by
> another field and I can't quite figure out how to do this.

Have you tried simply sorting a by the other field prior to doing your
merge-and-sort? The Python list.sort() method is guaranteed to be
stable. I can't find a comparable guarantee for sorted(), but worst
case, you should be able to do your list merge, and then explicitly
name it and sort it.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list