简体   繁体   English

如何将列表/数组作为参数传递给python fire?

[英]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 .我试图使用python-fire从命令行调用它。

I have tried several ways -我尝试了几种方法-


Attempt 1:尝试1:

fire_runner.py my_func [one, two]

Results in two separate arguments "[one" and "two]" .产生两个单独的参数"[one""two]"


Attempt 2:尝试2:

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

which has the same result.结果相同。


Attempt 3:尝试 3:

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

same result.同样的结果。

Your list will first be parsed by the command line shell.您的列表将首先由命令行 shell 解析。 This means you need to force it as a single argument.这意味着您需要将其强制为单个参数。 There are many ways to do this:有很多方法可以做到这一点:

  1. No space: [one,two]没有空格: [one,two]

  2. Escape the space: [one,\ two]逃离空间: [one,\ two]

  3. Quotes: "[one, two]" .引号: "[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.这里的名称args是约定俗成的,但它只是一个变量名,所以它可以是任何东西。 This will allow you to call your function as这将允许您将您的函数称为

fire_runner.py my_func one two

逗号对我有用后没有保留任何空格。

fire_runner.py my_func [one,two]

Adding this answer just for a special case.仅针对特殊情况添加此答案。

given that your function is like this:鉴于您的功能是这样的:

def my_func(dates: List[str] = []):
    ...

In case you need to send list of dates (or any combination of numbers and characters) from the CLI, the following WON'T work.如果您需要从 CLI 发送日期列表(或数字和字符的任意组合),以下不起作用。

fire_runner.py my_func --dates 2021-09-20,
fire_runner.py my_func --dates "[2021-09-20]"
fire_runner.py my_func --dates [2021-09-20]

In all the above cases, dates will be read as one string instead of list of strings.在上述所有情况下,日期将被读取为一个字符串而不是字符串列表。 For proper reading of list of strings you can use the following.为了正确阅读字符串列表,您可以使用以下内容。

fire_runner.py my_func --dates "[\"2021-09-20\"]"
fire_runner.py my_func --dates "[\"2021-09-20\", \"2021-09-76\"]"

Fire converts a comma-separated digit string to tuple as follows: Fire 将逗号分隔的数字字符串转换为元组,如下所示:

fire_runner.py my_func  1,2,3

and for a comma-separated string you can use:对于逗号分隔的字符串,您可以使用:

fire_runner.py my_func  \'2021-2\',\'20212-3\',\'2023-4\'

一个元素列表添加特殊的极端情况解决方案,因为上述语法似乎不起作用(列表被解析为以[开头的字符串):

fire_runner.py my_func "['one']"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中将数组作为参数传递 - Pass array as argument in python 如何在python中将字符串列表作为函数参数传递? - How to pass list of string as function argument in python? 在 python 中,如何将数组参数传递给 powershell 脚本 - In python, how to pass an array argument to powershell script Perl + Python - How to pass array from perl as an argument to a python function - Perl + Python - How to pass array from perl as an argument to a python function 如何在 Python 中将整个列表作为命令行参数传递? - How to pass an entire list as command line argument in Python? 如何将列表作为命令行参数传递给 python 脚本 - how to pass a list as a command line argument to a python script Python 如何在执行程序中传递参数对列表。map - Python how to pass list of argument pair in executor.map 通过odeint函数求解ODE时如何传递数组作为参数(在Python中) - How to pass an array as argument (in Python) when solving ODE by odeint function 如何将 meshgrid 作为参数传递给 function,它只允许 python 中的数组 - How to pass meshgrid as an argument to a function which only allows an array in python 如何将整个 numpy 数组作为 python 中的参数传递给用户定义的 function? - How to pass whole numpy array as argument in python to a user defined function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM