开发者

Listing inputs on Python by using XML and array

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 grat开发者_Go百科eful, thank you.


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]...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜