Run tests from code with arg - python

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")

Related

Syntax error using sys.argv as argument in function

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.

How to use Parameters in Python Luigi

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.

argparse: how to parse a single string argument OR a file listing many arguments?

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

Postion of argument will be fixed

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.

Parsing arbitrary number of arguments in Python OptParser

How can I define an option with an arbitrary number of arguments in Python's OptParser?
I'd like something like:
python my_program.py --my-option X,Y # one argument passed, "X,Y"
python my_prgoram.py --my-option X,Y Z,W # two arguments passed, "X,Y" and "Z,W"
the nargs= option of OptParser limits me to a defined number. How can I do something like this?
parser.add_option("--my-options", dest="my_options", action="append", nargs="*")
which will simply take whatever's after --my-option and put it into a list? E.g. for case 1, it should be ["X,Y"], for case 2 it should be ["X,Y", "Z,W"].
What's a way to do this with OptParser?
thanks.
The optarse module is deprecated in python 2.7 (which has just been released!). If you can upgrade, then you can use its replacement the argparse module. I think that has what you want. It supports a '*' value for nargs.
http://docs.python.org/library/argparse.html#nargs
Have you tried ommitting the n_args bit? The examples in the docs suggest you don't need it.

Categories