Python argument parser list of list or tuple of tuples

Question:

I’m trying to use argument parser to parse a 3D coordinate so I can use

--cord 1,2,3 2,4,6 3,6,9

and get

((1,2,3),(2,4,6),(3,6,9))

My attempt is

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--cord', help="Coordinate", dest="cord", type=tuple, nargs=3)
args = parser.parse_args(["--cord","1,2,3","2,4,6","3,6,9"])

vars(args) 
{'cord': [('1', ',', '2', ',', '3'),
  ('2', ',', '4', ',', '6'),
  ('3', ',', '6', ',', '9')]}

What would the replacement of the comma be?

Asked By: joedborg

||

Answers:

You can add your own type. This also allows for additional validations, for example:

def coords(s):
    try:
        x, y, z = map(int, s.split(','))
        return x, y, z
    except:
        raise argparse.ArgumentTypeError("Coordinates must be x,y,z")


parser.add_argument('--cord', help="Coordinate", dest="cord", type=coords, nargs=3)
Answered By: georg

For my problem, I had to have a more general approach, not linked to the number of inputs.

Starting from the great answer by georg, I solved my problem as follows

# additional type
def coords(s):
    seps = r'[ ;.]'
    try:
        situp = []
        for si in re.split(seps, s):
            situp.append(tuple(map(int, si.split(','))))
        return situp
    except:
        raise argparse.ArgumentTypeError("Coordinates must be given divided by commas and space, dot, or semicolon e.g.: 'x,y k,l,m'")

With this, an input like 1,2 3,4,5 will be turned in a list of tuples like [(1,2), (3,4,5)]

EDIT: It might be that the for loop is not optimal, but I wrote it to avoid the use of nargs

EDIT 2:

  1. to have a list of list, one should change

    the line situp.append(tuple(map(int, si.split(','))))

    with situp.append(list(map(int, si.split(','))))

  2. to have a tuple of uples one can just change the return with

    return tuple(situp)

Answered By: brodegon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.