在 Python 中,如何在不更改命名空间的情况下在另一个文件中包含(不导入)一个文件,宏样式?

2022-01-11 00:00:00 python macros include

问题描述

首先我知道导入".当我尝试导入"时,它不起作用.我想要做的是将一个模块分成两部分,其中一个可以由一个组编辑,另一个不能.我希望团队编写定义明确的检索函数",而不会受到诱惑或无法编辑运行它们的后端代码(即使是偶然的).导入"上命名空间的变化妨碍了我.我正在寻找在 File_B 中包含 File_A 文本的宏样式,以便像它是 File_B 的一部分一样内联运行.

First I do know about 'import'. When I try 'import' it doesn't work. What I'm trying to do is split a single module into two parts, one of which is editable by a group and the other of which is not. I want the group to write well-defined 'retrieval functions' without the temptation or ability to edit the backend code that runs them (even accidentally). The changes in namespace on an 'import' are getting in my way. I'm looking for a macro-style inclusion of File_A's text within File_B, to be run inline as if it were part of File_B.

这就是我正在做的:

我有一些通用代码,旨在依次调用信息检索函数列表,并以统一的方式存储信息.为此,我将其文本名称添加到列表中:

I have some generalized code that is designed to call a list of information retrieval functions in turn, and store the info in a unified way. To do this I add its text name to a list:

DataTypes = ['TypeA','TypeB','TypeC']

...然后定义一个知道如何获取每种类型的函数,并返回一个填充的对象类:

... and then define a function that knows how to get each type, and returns a populated object class:

def Get_TypeA:
    # do some stuff to retrieve info
    InfoObj Data
    # Populate Data with the info I got
    return Data

def Get_TypeB:
    # etc. etc.
    return Data
def Get_TypeC:
    # etc. etc.
    return Data

# Backend code below this line, hopefully nobody touches it?
# (But really it would be best if this was in a different file
#  that is harder to mess with and has locked-down permissions.)
class InfoObj:
    # stuff stuff definitions methods etc.

这些函数将由只有 Python 基本知识但通常有不良编码习惯的人编辑,但他们需要能够经常自定义收集和显示的内容.我已经有一个后端来检查列表并调用函数,但我只想将这些定义与后端代码的其余部分移动到一个单独的文件中,但就像它们内联一样工作.(即将希望没有人碰它"变成人们必须不遗余力地碰它")

These functions will be edited by people with only a basic knowledge of Python but often bad coding habits, but who need to be able to frequently customize what is gathered and displayed. I already have a backend that checks the list and calls the functions, but I just want to move those definitions into a separate file from the rest of the backend code, but work as if they were right there inline. (i.e. turn the "hopefully nobody touches it" into a "people have to go out of their way to touch it")

Python 有这个吗?

Does Python have this?

当我尝试使用 Import 时,检索函数与 InfoObj 的定义失去联系.无论我使用import otherFile"还是from otherFile import *"都会发生这种情况.

When I try using Import, the retrieval functions lose contact with the definition of InfoObj. This happens whether I use 'import otherFile' or 'from otherFile import *'.

这是我用来以标准方式检索各种类型的方法:

This is what I'm using to retrieve the various types in a standard way:

THISMODULE = sys.modules[__name__]

for type in DataTypes:
    RetrievalFn = getattr( THISMODULE, 'Get_'+type )
    Data = RetrievalFn()
    StoreInDB(Data)

(虽然这归结为基础,但有 try/except 子句、验证步骤以确保正确填充数据并且不包含不正确的类型或坏东西,以及在出现问题时通知团队以及在哪里出现问题的代码但仍会处理其余项目.目标是进行编辑的人不会破坏其余检查,即使他们的编辑破坏了其中一项检查.)

(though this is boiled down to basics, there are try/except clauses, validation steps to make sure Data was populated correctly and doesn't contain incorrect types or bad stuff, and code to notify the team if and where something breaks but still process the rest of the items. The goal is that someone making edits does not break the rest of the checks, even if their edits broke one of the checks.)


解决方案

这可以通过 execfile(),但您应该考虑为此使用其他机制,例如多态性或插件.

This can be done with execfile(), but you should consider using some other mechanism for this instead such as polymorphism or plugins.

相关文章