开发者

Python - how to avoid exec for batching?

I have an existing python application (limited deployment) that requires the ability to run batches/macros (ie do foo 3 times, change x, do y). Currently I have this implemented as exec running through a text file which contains simple python code to do all the required batching.

However exec is messy (ie security issues) and t开发者_运维知识库here are also some cases where it doesn't act exactly the same as actually having the same code in your file. How can I get around using exec? I don't want to write my own mini-macro language, and users need to use multiple different macros per session, so I can't setup it such that the macro is a python file that calls the software and then runs itself or something similar.

Is there a cleaner/better way to do this?

Pseudocode: In the software it has something like: -when a macro gets called

for line in macrofile:
   exec line

and the macrofiles are python, ie something like:

property_of_software_obj = "some str"
software_function(some args)

etc.


Have you considered using a serialized data format like JSON? It's lightweight, can easily translate to Python dictionaries, and all the cool kids are using it.

You could construct the data in a way that is meaningful, but doesn't require containing actual code. You could then read in that construct, grab the parts you want, and then pass it to a function or class.

Edit: Added a pass at a cheesy example of a possible JSON spec.

Your JSON:

{
    "macros": [
        {
            "function": "foo_func", 
            "args": {
                "x": "y", 
                "bar": null
            }, 
            "name": "foo", 
            "iterations": 3
        }, 
        {
            "function": "bar_func", 
            "args": {
                "x": "y", 
                "bar": null
            }, 
            "name": "bar", 
            "iterations": 1
        }
    ]
}

Then you parse it with Python's json lib:

import json

# Get JSON data from elsewhere and parse it
macros = json.loads(json_data)

# Do something with the macros
for macro in macros:
    run_macro(macro) # For example

And the resulting Python data is almost identical syntactically to JSON aside from some of the keywords like True, False, None (true, false, null in JSON).

{
    'macros': [
        {
            'args': 
            {
                'bar': None, 
                'x': 'y'
            },
             'function': 'foo_func',
             'iterations': 3,
             'name': 'foo'
        },
        {
            'args': 
            {
                'bar': None, 
                'x': 'y'
            },
             'function': 'bar_func',
             'iterations': 1,
             'name': 'bar'
        }
    ]
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜