Convert one list to set, but if empty use a default one

18,980

Solution 1

only = set(args.onlyTheseServers or availableServers)

Solution 2

Looking at your previous question, I'd say that what you're really looking for is a way to assign a default value to a missing parameter using argparse. In that case you should just use default as follows:

parser.add_argument('-o', '--only', default=default_servers, ...)

This way, when the -o/--only option isn't passed, the namespace will have the default value correctly set.

Solution 3

args.onlyTheseServers seems a variable coming from argparse.

If that's your case you should check the default argument and the set_default() method.

Here's an example:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', nargs='*', default=['1', '2', '3'])
>>> args = parser.parse_args()
>>> args.foo
['1', '2', '3']
>>> args = parser.parse_args(['--foo', 'a', 'b'])
>>> args.foo
['a', 'b']

Solution 4

Not really much better, but at least a bit shorter:

only = set(availableServers if args.onlyTheseServers is None 
           else args.onlyTheseServers)

You can also do

only = set(args.onlyTheseServers or availableServers) 

It works slightly different as it does not test for None, but only if the argument is true-is - in this case it should work though.

Share:
18,980
Deleted
Author by

Deleted

This account is deleted.

Updated on July 16, 2022

Comments

  • Deleted
    Deleted almost 2 years

    I'm looking for a nicer way to assign a set with the conent of a list if such list is not empty, otherwise another list should be used.

    If it is possible I'd like a nicer way to write this (or an argument to why this is the nicest way):

    if args.onlyTheseServers:
        only = set(args.onlyTheseServers)
    else:
        only = set(availableServers)
    
  • Duncan
    Duncan about 12 years
    That has slightly different semantics than the original. It may or may not matter, but if args.onlyTheseServers is an empty list then the original produces an empty set whereas your version would use the default.
  • Chris Morgan
    Chris Morgan about 12 years
    True indeed. The semantics are different from his sample code, but actually match the question title ("... with the contents of a list if the list isn't empty...").
  • Chris Morgan
    Chris Morgan about 12 years
    Well noticed. I think that that is indeed what he wants. I'm now annoyed that I didn't think of it myself... without having connected it with the previous question I had connected it with argparse due to the variable name args!
  • Deleted
    Deleted about 12 years
    I had a bug in my code sample. So you interpreted it as I wanted it, despite my example being buggy. Thanks!
  • Deleted
    Deleted about 12 years
    Nice deduction! I´ve solved it the way you suggested. However, to let my question here "stand on its own" (and to possibly help someone else with it) I´m marking the other one as my preferred answer. Although it feels a bit bad, if only I had more up-votes to give. :-)
  • Deleted
    Deleted about 12 years
    @Chris Morgan: Well, I am a confusing questioner. Sorry about that!
  • Deleted
    Deleted about 12 years
    Nice deduction! I´ve solved it the way you suggested. However, to let my question here "stand on its own" (and to possibly help someone else with it) I´m marking the other one as my preferred answer. Although it feels a bit bad, if only I had more up-votes to give. :-)
  • Deleted
    Deleted about 12 years
    I´ll actually have to call you crazy on that one. ;-) But thanks for answering! Have a zero-vote (voting down a correct answer would be cruel). :-)
  • Rik Poggi
    Rik Poggi about 12 years
    @Kent: Don't worry about it :) (in future if you have doubts on which answer to accept, here there's a reference)
  • Johan Lundberg
    Johan Lundberg about 12 years
    @Kent, hehe. It's cause I completely buy the 'one case is the common one and should be first' explanation given in the documentation of pep 308. docs.python.org/release/2.5/whatsnew/pep-308.html So you read it only = set(args.onlyTheseServers) ... bla bla some special case treated here...