Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a list/array as argument to python fire?

I have function like this -

def my_func(my_list_arg=["one", "two"]):

Which I'm trying to call from command line using python-fire.

I have tried several ways -


Attempt 1:

fire_runner.py my_func [one, two]

Results in two separate arguments "[one" and "two]".


Attempt 2:

fire_runner.py my_func ["one", "two"]

which has the same result.


Attempt 3:

fire_runner.py my_func [\"one\", \"two\"]

same result.

like image 855
Duke79 Avatar asked Jun 30 '19 14:06

Duke79


People also ask

How do you pass a list as an argument in Python?

In Python, you can unpack list , tuple , dict (dictionary) and pass its elements to function as arguments by adding * to list or tuple and ** to dictionary when calling function.

Can we pass list as argument in Python function?

You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

How do I use the fire command in Python?

Fire() The easiest way to use Fire is to take any Python program, and then simply call fire. Fire() at the end of the program. This will expose the full contents of the program to the command line.


1 Answers

Your list will first be parsed by the command line shell. This means you need to force it as a single argument. There are many ways to do this:

  1. No space: [one,two]

  2. Escape the space: [one,\ two]

  3. Quotes: "[one, two]".

Note that you could modify your function to take a list of arguments using the following syntax:

def my_func(*args):

The name args here is convention, but it is just a variable name, so it can be anything. This will allow you to call your function as

fire_runner.py my_func one two
like image 172
Code-Apprentice Avatar answered Sep 28 '22 00:09

Code-Apprentice