使用 XML 和数组列出 Python 上的输入

发布于 2024-12-09 19:03:01 字数 509 浏览 1 评论 0原文

我想通过 Python 和 XML 对某种函数进行排序,但我对此感到非常困惑。

我举个例子,

config.xml 文件中包含 3 个输入选项。

<inputs>
    <input1 value='1- Sending Email' />
    <input2 value='2- Extracting a Tar File' />
    <input3 value='3- Rebooting Server' />
</inputs>

-

所以,我已经对名为“funcs”的文件夹中的每个函数进行了 python 处理,并且对它们没有任何问题。

我想要的只是;

1- 列出输入并询问选择哪一项。

2-我需要通过数组来完成。因为我以后只想更新 XML 文件,所以我永远不会碰让 python 执行所有功能的主文件。

任何帮助都会让我感激不尽,谢谢。

I want to sort some kind of functions by Python and XML, but I'm really confused about it.

I'll give you an example,

There are 3 input options that contains in a config.xml file.

<inputs>
    <input1 value='1- Sending Email' />
    <input2 value='2- Extracting a Tar File' />
    <input3 value='3- Rebooting Server' />
</inputs>

--

So, I have already pythoned each function in a folder named "funcs" and I have no problems with them.

All I want is;

1- Listing the inputs and ask for which one to choose.

2- And I need to do it by array. Because I only want to update the XML file in the future, so I'll never touch the main file that lets python to do every functions.

Any help will make me grateful, thank you.

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

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

发布评论

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

评论(2

回忆凄美了谁 2024-12-16 19:03:01

这涉及到一些读心术,但我想我知道你在寻找什么......我构建了一个名为 inputs.xml 的文件,其中包含你上面引用的 xml,然后我读取它并存储名为 inputs 的 Python 字典中的输入

from xml.etree import ElementTree

inputs = dict()
tree = ElementTree.parse( 'inputs.xml' )
for row in tree.getiterator('inputs'):
    for elem in row.getchildren():
        inputs[elem.tag] = elem.get('value')

结果...

$ python
Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01) 
[GCC 4.3.4 20090804 (release) 1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from xml.etree import ElementTree
>>> 
>>> inputs = dict()
>>> tree = ElementTree.parse( 'inputs.xml' )
>>> for row in tree.getiterator('inputs'):
...     for elem in row.getchildren():
...         inputs[elem.tag] = elem.get('value')
... 
>>> print inputs
{'input2': '2- Extracting a Tar File', 'input3': '3- Rebooting Server', 'input1': '1- Sending Email'}
>>>

现在假设您从用户处获取输入并将其作为字符串存储在名为 input 的 Python 变量中...如果您想访问用户选择的选项,请使用输入[“输入”+输入]...

This involves a bit of mindreading, but I think I know what you're looking for... I built a file called inputs.xml that contains the xml you referenced above, then I read it and stored the inputs in a python dictionary called inputs

from xml.etree import ElementTree

inputs = dict()
tree = ElementTree.parse( 'inputs.xml' )
for row in tree.getiterator('inputs'):
    for elem in row.getchildren():
        inputs[elem.tag] = elem.get('value')

The results...

$ python
Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01) 
[GCC 4.3.4 20090804 (release) 1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from xml.etree import ElementTree
>>> 
>>> inputs = dict()
>>> tree = ElementTree.parse( 'inputs.xml' )
>>> for row in tree.getiterator('inputs'):
...     for elem in row.getchildren():
...         inputs[elem.tag] = elem.get('value')
... 
>>> print inputs
{'input2': '2- Extracting a Tar File', 'input3': '3- Rebooting Server', 'input1': '1- Sending Email'}
>>>

Now suppose you take input from a user and store it as a string in a python variable called input... if you want to access what choice the user selected, use inputs["input"+input]...

紧拥背影 2024-12-16 19:03:01

这看起来更像是一个架构问题,而不是一个 Python 问题。

如果您有一堆不想接触的函数,则应该将每个函数与某处的输入消息关联(很可能位于该函数的相同源代码上,而不是在 xml 中)。

例如,您可以将所有函数放在一个文件中并将其作为模块导入。

然后,当您运行程序时,您将创建一个要呈现给用户的函数列表,以某种方式对它们进行数字排序,并显示每个函数的输入消息。

模块示例:

def reset():
    print "reset all"

def sendmail():
    print "send mail"

funcs = {
    "reset":     {"function": reset,    "message": "Choose to reset"},
    "sendmail":  {"function": sendmail, "message": "Choose to email someone"}
    }

通过这样做,您的 xml 应该仅包含 funcs 字典的键。然后,为了打印消息,您应该使用 funcs['reset']['message'],例如,要使用该函数,您应该使用 funcs['reset ']['function'](args)。

它有效,我已经使用过非常相似的东西。

It seems more an architectural problem than a Python problem.

If you have a Bunch of Functions you never want to touch, you should have each function associated with an imput message somewhere (most probably on the same source code of the function, and not in a xml).

You could, for example, put all the functions in a single file and import it as a module.

Then, when you run your program, you create a list of the functions you want to present to the user, sort them numerically someway, and display the input message for each function.

Example of module:

def reset():
    print "reset all"

def sendmail():
    print "send mail"

funcs = {
    "reset":     {"function": reset,    "message": "Choose to reset"},
    "sendmail":  {"function": sendmail, "message": "Choose to email someone"}
    }

By doing so, you xml should contain only the keys of the funcs dictionary. Then, for you to print the message, you should use funcs['reset']['message'], for example, and to use the function, you should use funcs['reset']['function'](args).

It works, I have already used something very similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文