user3341276
user3341276

Reputation: 15

file as an optional parameter

I'm new in python and I'm developing a script that processes a file to make some calculations. The script (preprocess.py) in command-line is called like this:

python preprocess.py if.txt of.txt 13 13 41 41 52 24 

if.txt is the input file, of.txt is the output file, (13 13 41 41 52 24) are just size of groups and subgroups of data.

In one part of the script a calculation is made with two variables (mean=0 and std=1).

I have to modify the script so that, the script accepts an optional parameter, a file which contains 2 values (mean and std), so that, if the script is called like above, the calculation is made with mean=0 and std=1, but if the script is called like this:

python preprocess.py if.txt of.txt 13 13 41 41 52 24 xxxx.txt 

the script take the two values in the xxxx.txt file and saves each value in its respective variable (mean and std).

The script right now gets the parameters with the sys.argv[] command. I have been looking for ways to solve this and found that the argparse module can solve it but I can't understand how to get the values ​​from the file and save them as expected.

  1. How to use the argparse module to make the xxxx.txt file an optional parameter and how to save the two values in that file in the two variables (mean and std)?

  2. If I use the argparse module, should be used argparse to get all the other parameters?. If so, how to do it?

PD: I'm learning english to, so if I made a mistake please correct me.

Upvotes: 1

Views: 1372

Answers (2)

user12699757
user12699757

Reputation:

Another way to do this is by setting a default value for a parameter, with a named parameter.

def method(one, two, three=3):
  print(one)
  print(two)
  if three != 3:  # don't have to use it like this but it is a default value
    print(three)

This set a default value if the parameter is not filled and, if it is filled, it will overrule the default value and use that.

method(11, 22)  # prints first two parameters
method(23, 54, 89)  # prints all three

Not sure if this is what your looking for but it is another way.

Upvotes: 0

hpaulj
hpaulj

Reputation: 231355

If the number of int values is fixed, then this script will work

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file1')
parser.add_argument('file2')
parser.add_argument('vars', type=int, nargs=3)
parser.add_argument('file3',nargs='?')
print(parser.parse_args())

The '?' means that the last argument is optional.

A variable number of ints can be taken with

parser.add_argument('vars', type=int, nargs='+')

However, the 2 variable nargs don't work together. '+' will get everything, leaving none for '?'.

I would suggest making that optional file an 'optional/flagged' argument. That's how optional ones are usually handled, and gets around this ambiguity in handling positionals.

parser.add_argument('--file3')

python preprocess.py if.txt of.txt 13 13 41 41 52 24 --file3 xxxx.txt

A non argparse approach is to check the last sys.argv value. If it does not look like an int, then assume it is the file name, and proceed with parsing the rest as before.

Upvotes: 1

Related Questions