python - 优雅地处理多个参数的序列
到目前为止,我通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里使用命名元组怎么样:
使用
;
和,
来区分你的命令组件:现在实例化为:
它允许你调用:
其中最后两个元素可以然后只需使用
.split()
再次拆分what about using a namedtuple here:
use
;
and,
to differentiate your command components:now instantiate with:
which allows you to call:
of which the elements of last two can then be split again with just
.split()
我不确定这是否是您想要的,但在我看来,您可以使用 argparse 并使用 nargs 参数来指定应使用多少个参数。
您可能已经知道了,但这里有一个链接:http://docs.python。 org/dev/library/argparse.html#nargs
使用
nargs=+
或nargs=*
会将您的参数放在列表中。因此,根据您的示例,我认为您可以这样调用您的程序: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=+
ornargs=*
will put your arguments on a list. So with your example I think you could call your program like: