使用 XML 和数组列出 Python 上的输入
我想通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这涉及到一些读心术,但我想我知道你在寻找什么......我构建了一个名为
inputs.xml
的文件,其中包含你上面引用的 xml,然后我读取它并存储名为inputs
的 Python 字典中的输入结果...
现在假设您从用户处获取输入并将其作为字符串存储在名为
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 calledinputs
The results...
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, useinputs["input"+input]
...这看起来更像是一个架构问题,而不是一个 Python 问题。
如果您有一堆不想接触的函数,则应该将每个函数与某处的输入消息关联(很可能位于该函数的相同源代码上,而不是在 xml 中)。
例如,您可以将所有函数放在一个文件中并将其作为模块导入。
然后,当您运行程序时,您将创建一个要呈现给用户的函数列表,以某种方式对它们进行数字排序,并显示每个函数的输入消息。
模块示例:
通过这样做,您的 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:
By doing so, you xml should contain only the keys of the
funcs
dictionary. Then, for you to print the message, you should usefuncs['reset']['message']
, for example, and to use the function, you should usefuncs['reset']['function'](args)
.It works, I have already used something very similar.