列出来自外部脚本python的变量,而不包含内置变量和导入变量 [英] Listing variables from external script python without the built-in and imported variables

查看:92
本文介绍了列出来自外部脚本python的变量,而不包含内置变量和导入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个python脚本,该脚本从json文件获取参数,打开由其中一个参数指定的模板脚本(从一组选项中),并生成一个新的python脚本,将其中的某些模板脚本替换为json中给出的参数.

I'm writing a python script that gets parameters from a json file, opens a template script specified by one of the parameters (from a set of options), and generates a new python script replacing some of the template script with the parameters given in the json.

我目前正尝试从模板中列出所有变量,如下所示:

I'm currently trying to list all variables from the template as follows:

list = [item for item in dir(imported_script) if not item.startswith("__")]

因此,我可以使用列表对变量进行迭代,然后将其写入新脚本中.

So I can use the list to iterate over the variables and write them in the new script.

问题:此结果list还包含上述导入模板脚本中的导入模块.我也尝试过使用imported_script.__dict__vars(imported_script),但是这些解决方案的效果相同.还可以选择使用help(imported_script)返回脚本的元数据,但是直到现在我还没有找到正确的方法来获取与help(imported_script).DATA等效的方法.

Problem: this result list also contains the imported modules from the mentioned imported template script. I've also tried with imported_script.__dict__ and vars(imported_script) but these solutions yield to the same. There is also the option of using help(imported_script) which returns the metadata of the script, but until now I haven't found a correct way to get what would be the equivalent to help(imported_script).DATA.

问题:是否有一种更有效的方法来列出导入的脚本变量,这样我就可以明确地得到用户声明的变量?

Question: Is there a more efficient way of listing the imported script variables so I get specifically the user declared variables?

更多上下文::我正在使用Airflow生成工作流. Airflow读取一个python脚本,该脚本指定应如何构造工作流程(在气流中称为DAG).该脚本应位于特定的文件夹中,以便气流可以构成工作流程.在json文件中给出了如何构造工作流的规范,并且每次用户想要生成工作流时都会有所不同,因此我必须阅读此json文件,然后编写一个新脚本,以重复使用模板中的代码(至少有人告诉我这样做的方式).我可以从中使用几个脚本来重复使用代码,将来还会有更多(动态的)脚本.要选择的脚本取决于json中的参数之一.

More context: I'm using Airflow to generate workflows. Airflow reads a python script that specifies how the workflow (known as DAG in airflow) should be constructed. The script should be in a specific folder so airflow can make the workflow. The specifications of how the workflow should be constructed are given in a json file and vary every time the user wants to generate a workflow, so I must read this json file and then write a new script reusing the code from the template (at least that's the way I've been told to do it). There are several scripts from which I could take the code to reuse and in the future there could be even more (It's dynamic). The script to be chosen depends on one of the parameters in the json.

推荐答案

目前尚不清楚您要做什么,但是从您给出的描述来看,使用模板比通过完全生成代码更容易解​​决此问题.从头开始使用Python.您也许可以摆脱普通的旧python格式字符串,或者如果您需要更多功能,请 jinja2 可能有用.

It's kind of unclear what you're trying to do, but from this description you give, this problem might be more easily solved using templates than by generating code entirely from scratch in Python. You might be able to get away with plain old python format strings, or if you need more features jinja2 might be useful.

my_script.py.template

import something

{param1}_variable = 1
def {param2}_function():
    something.{something_func}

version_1.json

{
    'param1': 'foo',
    'param2': 'bar',
    'something_func': 'neato_function'
}

然后,您将生成类似以下内容的输出:

Then you generate the output with something like:

template = open('my_script.py.template').read()
with open('my_script_version_1.py', 'w') as f:
    f.write(template.format(**json.load(open('version_1.json'))))

所有这些...您是否超级确定要执行此操作?除非出于特殊原因需要生成包含代码的文件,否则最好使用更多标准方法(例如使用常规的Python导入)

All that being said...are you super sure that you want to do this? Unless there's a specific reason you need to generate a file containing the code, it might be better to use more standard methods (like using regular Python imports)

要真正回答您的问题:您可以尝试在vars()的元素上使用type函数,但这似乎很脆弱.模块的类型为<class 'module'>.但是然后,您还必须弄清楚如何过滤掉函数,类等.

To actually answer your question: you could try to use the type function on the elements of vars(), but this seems really fragile. Modules will have type <class 'module'>. But then you also have to figure out how to filter out functions, classes, etc.

这篇关于列出来自外部脚本python的变量,而不包含内置变量和导入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆