How do I pass in parameters to Luigi? if I have a python file called FileFinder.py with a class named getFIles:
class getFiles(luigi.Task):
and I want to pass in a directory to this class such as:
C://Documents//fileName
and then use this parameter in my run method
def run(self):
how do I run this in command line and add the parameter for use in my code? I am accustomed to running this file in command line like this:
python FileFinder.py getFiles --local-scheduler
What do I add to my code to use a parameter, and how do I add that parameter to the command line argument?
Also, as an extension of this question, how would I use multiple arguments? or arguments of different data types such as strings or lists?
As you have already figured out, you can pass arguments to luigi via
--param-name param-value
in the command line. Inside your code, you have to declare these variables by instantiating the Parameter class or one of it's subclasses. The subclasses are used to tell luigi if the variable has a data-type that is not string. Here is an example which uses two command line arguments, one Int and one List:
import luigi
class testClass(luigi.Task):
int_var = luigi.IntParameter()
list_var = luigi.ListParameter()
def run(self):
print('Integer Param + 1 = %i' % (self.int_var + 1))
list_var = list(self.list_var)
list_var.append('new_elem')
print('List Param with added element: ' + str(list_var))
Note that ListParams actually get converted to tuples by luigi, so if you want to do list operations on them, you have to convert them back first (This is a known issue, but doesn't look like it will be fixed soon).
You can invoke the above module from the command line like this (i have saved the code as a file called "testmodule.py" and made the call from inside the same directory):
luigi --module testmodule testClass --int-var 3 --list-var '[1,2,3]' --local-scheduler
Note here that for variables containing a _, this has to be replaced by -.
The call yields (along with many status messages):
Integer Param + 1 = 4
List Param with added element: [1, 2, 3, 'new_elem']
So I think this works, in the code I added:
fileName = luigi.Parameter()
if i run this in the command line:
python FileFinder.py getFiles --local-scheduler --getFiles-fileName C://Documents//fileName
but if anyone has any advice on parameters of different types and how to use them, especially numbers and lists, please let me know.
Adding to Toterich's answer.
While passing a list of string arguments as a ListParameter():
python file_name.py --local-scheduler TaskName --arg '["a","b"]'
The string arguments must be enclosed in double-quotes and not single quotes otherwise it'll give a JSONParsing error.
Related
Got syntax error caused by this line of code
def load(sys.argv):
with open(sys.argv[1]) as file:
reader = json.load(file)
return reader['json']
Message from stderr:
def load(sys.argv):
^
SyntaxError: invalid syntax
Of course, I have imported sys.
Actually, parameters inside the function, that use sys.argv, work properly. Does anyone know a solution to this?
Functions pass in parameters and variable names, you don't declare them in there. You code should look like the following
def load(arguments):
# Your Code
firstArg = arguments[0]
load(sys.argv)
You can't use sys.argv as a function argument.
Just rename the argument and it will work:
import sys
def load(args):
# your logic
print(load(sys.argv))
If you are going to specify you want sys.argv[1] inside the function, you wouldn't need any sort of parameter. You could make it a function that needs no arguments.
import sys
def load():
with open(sys.argv[1]) as file:
# code to execute...
# code to execute...
load()
My suggestion is only applicable if you only want to open one file using one command line parameter (at index 1). If you wanted to open others using other command line parameters, you would need a parameter in your function like 'args' (like David Teather said) to make the code more flexible.
Parameters are set as new variable names to be used within the scope of your function. In your code, it's like you are trying to set 'sys.argv' as a function name, which doesn't work because it is already a function.
In a shell script I have:
/usr/local/bin/pybot --variablefile variables.py:$var1:$var2 test_cases.tsv
inside variables.py how can I access var1 and var2 arguments?
I have tried:
import sys
var1 = sys.argv[1]
var1 = sys.argv[2]
it seems like this doesn't work.
For you to access the variables, your variable file must define the function get_variables, which will be given the arguments passed from the command line. This function needs to return a dictionary where the keys are the robot variable names.
For example:
def get_variables(arg1, arg2):
variables = {
"var1": arg1,
"var2": arg2
}
return variables
If your variable file is based on a class, the class needs to have the get_variables method.
For example:
# variables.py
class variables(object):
def get_variables(self, arg1, arg2):
variables = {
"var1": arg1,
"var2": arg2
}
return variables
When you do the above, your test will have two variables set: ${var1} and ${var2} which will have the values that were passed via the --variablefile argument.
Here is a test that can be used to verify the above:
# example.robot
*** Test cases ***
Example
should be equal ${var1} hello
should be equal ${var2} world
Here is how to run the test in order for it to pass:
$ var1=hello
$ var2=world
$ /usr/local/bin/pybot --variablefile variables.py:$var1:$var2 example.robot
Of course, var1 and var2 are completely arbitrary. You can pass raw strings, too:
$ /usr/local/bin/pybot --variablefile variables.py:hello:world example.robot
Passing arguments is described in the user guide section titled Getting variables from a special function
sys reads the arguments fron the command line, as they appears to it:
sys.argv[0] contains the script name
sys.argv[1], the first argument (whatever it is)
sys.argv[2], the second, and so on.
You should use argparse, it helps to build comprehensive CLIs. A nice tutorial exists on the Python website.
You seem to make assumptions about how the arguments are parsed which are not true. Here's how these arguments are passed from the shell to Python:
sys.argv[0] is /usr/local/bin/pybot
sys.argv[1] is --variablefile
sys.argv[2] is variables.py:$var1:$var2 where the values of the shell variables var1 and var2 are substituted.
sys.argv[n] is test_cases.tsv
The last one is [n] because without quotes around the argument, sys.argv[2] might actually be split into multiple values. For example, if var1 contains = foo * bar= then actually
sys.argv[2] is variables.py:=
sys.argv[3] is foo
sys.argv[4..n-2] is a list of files in the current directory, and
sys.argv[n-1] is =bar:$var2 where similar further processing for the value of var2 may take place.
There are Python argument parsing modules which assign further semantics e.g. to arguments which start with a dash (these will be interpreted as options) but by itself, Python does no such thing. If that's what you want, maybe look at argparse or one of its replacements; but you still need to understand how the basic mechanics work. A common arrangement is to avoid internal structure in arguments, and instead require the user to pass each value as a separate argument -- so perhaps
--variablefile variables.py --variablefile "$var1" --variablefile "$var2"
with quoting to prevent the shell from attempting to perform whitespace tokenization and wildcard expansion on the variable values, and then probably in your script an argparse definition which says to merge multiple option arguments into a list.
parser = argparse.ArgumentParser()
parser.add_argument('--variablefile', action='append')
I used to run tests with a command like
pytest.main('-s path_to_file --my_fixtures_arg1 arg_value')
At the moment, such a call is considered obsolete, and you need to call this command through a list of arguments. But I can not pass the necessary parameters in any way. Who knows the solution?
I'm currently using list of arguments since passing string was deprecated. In your case I think this one should work:
arguments = ['-s', '--my_fixtures_arg1=arg_value', 'path_to_file']
pytest.main(args=arguments)
Currently pytest uses shlex.split to split a string into a list of arguments.
You can do the same if your string comes from DB, config file or such:
import shlex, sys
args_list = shlex.split(args_string, posix=sys.platform != "win32")
I have a use case where I'd like the user to be able to provide, as an argument to argparse, EITHER a single string OR a filename where each line has a string.
Assume the user launches ./myscript.py -i foobar
The logical flow I'm looking for is something like this:
The script determines whether the string foobar is a readable file.
IF it is indeed a readable file, we call some function from the script, passing each line in foobar as an argument to that function. If foobar is not a readable file, we call the same function but just use the string foobar as the argument and return.
I have no ability to guarantee that a filename argument will have a specific extension (or even an extension at all).
Is there a more pythonic way to do this OTHER than just coding up the logic exactly as I've described above? I looked through the argparse tutorial and didn't see anything, but it also seems reasonable to think that there would be some specific hooks for filenames as arguments, so I figured I'd ask.
A way would be:
Let's say that you have created a parser like this:
parser.add_argument('-i',
help='...',
type=function)
Where type points to the function which will be an outer function that evaluates the input of the user and decides if it is a string or a filename
More information about type you can find in the documentation.
Here is a minimal example that demonstrates this use of type:
parser.add_argument('-d','--directory',
type=Val_dir,
help='...')
# ....
def Val_dir(dir):
if not os.path.isdir(dir):
raise argparse.ArgumentTypeError('The directory you specified does not seem to exist!')
else:
return dir
The above example shows that with type we can control the input at parsing time. Of course in your case the function would implement another logic - evaluate if the input is a string or a filename.
This doesn't look like an argparse problem, since all you want from it is a string. That string can be a filename or a function argument. To a parser these will look the same. Also argparse isn't normally used to run functions. It is used to parse the commandline. Your code determines what to do with that information.
So here's a script (untested) that I think does your task:
import argparse
def somefunction(*args):
print(args)
if __name__=='__main__':
parser=argparse.ArgumentParser()
parser.add_argument('-i','--input')
args = parser.parse_args()
try:
with open(args.input) as f:
lines = f.read()
somefunction(*lines)
# or
# for line in lines:
# somefuncion(line.strip())
except:
somefunction(arg.input)
argparse just provides the args.input string. It's the try/except block that determines how it is used.
================
Here's a prefix char approach:
parser=argparse.ArgumentParser(fromfile_prefix_chars='#',
description="use <prog -i #filename> to load values from file")
parser.add_argument('-i','--inputs')
args=parser.parse_args()
for arg in args.inputs:
somefunction(arg)
this is supposed to work with a file like:
one
two
three
https://docs.python.org/3/library/argparse.html#fromfile-prefix-chars
in command line if I run my program
python parse.py config=abc.txt factor_date=20151001 like this
I want the position of argument will be fixed. That means if I pass argument like below
python parse.py factor_date=20151001 config=abc.txt
it has to show error.
import sys
config_file=sys.argv[1]
factor_date = sys.argv[2]
argstring=""+config_file+" "+factor_date+""
arg_list = argstring.split(' ')
input={}
for arg in arg_list:
#x=arg.split("--")
key,val=arg.split("=")[0],arg.split("=")[1]
if key == "config":
input[key]=val
if key =="factor_date":
input[key]=val
print input
You can have a look at click. It let's you create command line interfaces pretty much effortlessly. It's bases on using decorators.
You should have a look at argparse. Your use case is for positional arguments. If you specify the name of the argument (optional arguments with argparse) then it does not make sense to force a specific order.
Still, when using positional arguments one could call the program with worng arguments, you will have to check by yourself the values provided by the user. However, you can force a type and it will automagically convert the strings, which in the case you describe would solve the problem.