python - 优雅地处理多个参数的序列

发布于 2024-12-01 09:37:27 字数 1489 浏览 2 评论 0原文

到目前为止,我通过 Optparse 将多个参数作为字符串处理, 例如:

--update_entities="host_group hostname entity_type entities2monitor"

entities2monitor 有可变参数,通过(注意 [3:])在回调函数中获取它们,

host = value.split()
(host_group, hostname, entity_type, entities2monitor) = (host[0], host[1], host[2], host[3:])

但是当我需要将以下形式的参数提供给回调时我应该如何处理它? (我可以控制将生成 Optparse 输入字符串的 SQL)

  • action_name: 空格分隔的字符串。 (例如:'TEST ACTION'

  • 主机组:一个字符串

  • actions_holder:由以下部分组成的列表:

    • condition_type(字符串)
    • 条件运算符(字符串)
    • condition_filter(空格分隔的字符串)

  • operations_holder:由以下部分组成的列表:
    • 操作类型:(字符串)
    • 操作发送到:(字符串)

示例:

--create_action='''TEST ACTION | client_service_platform | "CONDITION_TYPE_TRIGGER_NAME CONDITION_OPERATOR_LIKE Weighted Successful" "CONDITION_TYPE_HOST CONDITION_OPERATOR_EQUAL host01" | "OPERATION_TYPE_MESSAGE userid1" "OPERATION_TYPE_EMAIL userid1" "OPERATION_TYPE_EMAIL userid2"'''

这是我到目前为止所拥有的,

actions_splits = actions_parameters.split(" | ")
action_name = actions_splits[0]
hostgroup = actions_splits[1]
actions_holder = actions_splits[2].strip('"').split('" "')
operations_holder = actions_splits[3].strip('"').split('" "')

哪种有效,但是有没有更无缝的方法来获取这些参数?

Thus far I was handling multiple arguments via Optparse as a string,
Eg:

--update_entities="host_group hostname entity_type entities2monitor"

where entities2monitor has variable arguments, grabbing them inside the callback function via (notice the [3:]),

host = value.split()
(host_group, hostname, entity_type, entities2monitor) = (host[0], host[1], host[2], host[3:])

But how should I approach it when I need to feed parameters of the following form into a callback?
(I have control over the SQL which will generate the Optparse input string)

  • action_name: a space delimited string. (Eg: 'TEST ACTION')

  • hostgroup: a string

  • actions_holder: a list comprised by:

    • condition_type (string)
    • condition_operator (string)
    • condition_filter (space delimited string)

and

  • operations_holder: a list comprised by:
    • operation_type: (string)
    • operation_sendto: (string)

Example:

--create_action='''TEST ACTION | client_service_platform | "CONDITION_TYPE_TRIGGER_NAME CONDITION_OPERATOR_LIKE Weighted Successful" "CONDITION_TYPE_HOST CONDITION_OPERATOR_EQUAL host01" | "OPERATION_TYPE_MESSAGE userid1" "OPERATION_TYPE_EMAIL userid1" "OPERATION_TYPE_EMAIL userid2"'''

This is what I have so far,

actions_splits = actions_parameters.split(" | ")
action_name = actions_splits[0]
hostgroup = actions_splits[1]
actions_holder = actions_splits[2].strip('"').split('" "')
operations_holder = actions_splits[3].strip('"').split('" "')

which kind of works but is there a more seamless way to get these parameters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角迷惘 2024-12-08 09:37:27

在这里使用命名元组怎么样:

import collections
Action = collections.namedtuple('Action', 'name hostgroup actions operations')

使用 ;, 来区分你的命令组件:

command= "TEST ACTION;client_service_platform;CONDITION_TYPE_TRIGGER_NAME CONDITION_OPERATOR_LIKE Weighted Successful,CONDITION_TYPE_HOST CONDITION_OPERATOR_EQUAL tt1scp1; OPERATION_TYPE_MESSAGE userid1,OPERATION_TYPE_EMAIL userid1,OPERATION_TYPE_EMAIL userid2"

现在实例化为:

a = Action(*command.split(';'))

它允许你调用:

a.name
a.hostgroup
a.actions.split(',')
a.operations.split(',')

其中最后两个元素可以然后只需使用 .split() 再次拆分

what about using a namedtuple here:

import collections
Action = collections.namedtuple('Action', 'name hostgroup actions operations')

use ; and , to differentiate your command components:

command= "TEST ACTION;client_service_platform;CONDITION_TYPE_TRIGGER_NAME CONDITION_OPERATOR_LIKE Weighted Successful,CONDITION_TYPE_HOST CONDITION_OPERATOR_EQUAL tt1scp1; OPERATION_TYPE_MESSAGE userid1,OPERATION_TYPE_EMAIL userid1,OPERATION_TYPE_EMAIL userid2"

now instantiate with:

a = Action(*command.split(';'))

which allows you to call:

a.name
a.hostgroup
a.actions.split(',')
a.operations.split(',')

of which the elements of last two can then be split again with just .split()

烏雲後面有陽光 2024-12-08 09:37:27

我不确定这是否是您想要的,但在我看来,您可以使用 argparse 并使用 nargs 参数来指定应使用多少个参数。

您可能已经知道了,但这里有一个链接:http://docs.python。 org/dev/library/argparse.html#nargs

使用 nargs=+nargs=* 会将您的参数放在列表中。因此,根据您的示例,我认为您可以这样调用您的程序:

--create_action "TEST ACTION" client_service_platform \
  '"CONDITION_TYPE_TRIGGER_NAME CONDITION_OPERATOR_LIKE Weighted Successful"\
  "CONDITION_TYPE_HOST CONDITION_OPERATOR_EQUAL tt1scp1"'\
  '"OPERATION_TYPE_MESSAGE userid1" "OPERATION_TYPE_EMAIL userid1"\
  "OPERATION_TYPE_EMAIL userid2"'

I'm not sure if this is what you want, but it seems to me that you could use argparse and use the nargs argument to specify how many arguments should be consumed.

You probably know it already, but here's a link: http://docs.python.org/dev/library/argparse.html#nargs

using nargs=+ or nargs=* will put your arguments on a list. So with your example I think you could call your program like:

--create_action "TEST ACTION" client_service_platform \
  '"CONDITION_TYPE_TRIGGER_NAME CONDITION_OPERATOR_LIKE Weighted Successful"\
  "CONDITION_TYPE_HOST CONDITION_OPERATOR_EQUAL tt1scp1"'\
  '"OPERATION_TYPE_MESSAGE userid1" "OPERATION_TYPE_EMAIL userid1"\
  "OPERATION_TYPE_EMAIL userid2"'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文