Skip to main content
 首页 » 编程设计

python之动态创建python函数

2025年05月04日10dyllove98

我正在尝试用 Python 解析一个 10k 行的 XML 文件。该文件(摘录如下)描述了一长串元素的一些物理特性。每个元素在 XML 文件中由“ble”元素描述,我已经为我将遇到的每个“ble”类型编写了 Python 类。

<header> 
    <!--SlotModels--> 
    <slotModels id="SpokeOptimusSlotModels" xml:base="spoke.xml" xmlns:xi="http://www.w3.org/2001/XInclude"> 
        <slotModel id="spokeLEDP"> 
            <var id="g1" type="double"/> 
            <ble id="DR0005" type="drift"> 
                <d id="l" type="double" unit="mm">240</d> 
                <d id="r" type="double" unit="mm">20</d> 
                <d id="ry" type="double" unit="mm">0</d> 
            </ble> 
            <ble id="QD0020" model="Quad310" type="quad"> 
                <d id="l" type="double" unit="mm">310</d> 
                <d id="g" type="double" unit="T/m">g1</d> 
                <d id="r" type="double" unit="mm">30</d> 
            </ble> 
        </slotModel> 
        <slotModel id="spokeLwu"> 
            <var id="g1" type="double"/> 
            <ble id="DR0010" type="drift"> 
                <d id="l" type="double" unit="mm">160</d> 
                <d id="r" type="double" unit="mm">30</d> 
                <d id="ry" type="double" unit="mm">0</d> 
            </ble> 
            <ble id="QD0020" model="Quad310" type="quad"> 
                <d id="l" type="double" unit="mm">310</d> 
                <d id="g" type="double" unit="T/m">g1</d> 
                <d id="r" type="double" unit="mm">30</d> 
            </ble> 
        </slotModel> 
        <slotModel id="spokeCryomodule"> 
            <var id="xelmax1" type="double"/> 
            <var id="rfpdeg1" type="double"/> 
            <ble id="DR0010" type="drift"> 
                <d id="l" type="double" unit="mm">368.5</d> 
                <d id="r" type="double" unit="mm">28</d> 
                <d id="ry" type="double" unit="mm">0</d> 
            </ble> 
            <ble id="FM0020" model="spokeCavity" type="fieldMap"> 
                <d id="rfpdeg" type="double" unit="deg">rfpdeg1</d> 
                <d id="xelmax" type="double" unit="unit">xelmax1</d> 
                <d id="radiusmm" type="double" unit="mm">28</d> 
                <d id="lengthmm" type="double" unit="mm">994</d> 
                <d id="file" type="string" unit="unit">Spoke_F2F</d> 
                <d id="scaleFactor" type="double" unit="unit">1.0</d> 
            </ble> 
        </slotModel> 
    </slotModels> 
    <slotModels id="medBetaSlotModels" xml:base="medBeta.xml" xmlns:xi="http://www.w3.org/2001/XInclude"> 
        <slotModel id="medBetaLwu"> 
            <var id="g1" type="double"/> 
            <ble id="DR0010" type="drift"> 
                <d id="l" type="double" unit="mm">256.2</d> 
                <d id="r" type="double" unit="mm">50</d> 
                <d id="ry" type="double" unit="mm">0</d> 
            </ble> 
            <ble id="QD0020" model="Quad410" type="quad"> 
                <d id="l" type="double" unit="mm">410</d> 
                <d id="g" type="double" unit="T/m">g1</d> 
                <d id="r" type="double" unit="mm">50</d> 
            </ble> 
        </slotModel> 
        <slotModel id="medBetaCryomodule"> 
            <var id="xelmax1" type="double"/> 
            <var id="rfpdeg1" type="double"/> 
            <ble id="DR0010" type="drift"> 
                <d id="l" type="double" unit="mm">414.4</d> 
                <d id="r" type="double" unit="mm">46.87</d> 
                <d id="ry" type="double" unit="mm">0</d> 
            </ble> 
            <ble id="FM0020" model="medBetaCavity" type="fieldMap"> 
                <d id="rfpdeg" type="double" unit="deg">rfpdeg1</d> 
                <d id="xelmax" type="double" unit="unit">xelmax1</d> 
                <d id="radiusmm" type="double" unit="mm">46.87</d> 
                <d id="lengthmm" type="double" unit="mm">1258.8</d> 
                <d id="file" type="string" unit="unit">MB_F2F</d> 
                <d id="scaleFactor" type="double" unit="unit">1.0</d> 
            </ble> 
        </slotModel> 
    </slotModels> 
    <cellModels id="SpokeOptimusCellModels" xml:base="spoke.xml" xmlns:xi="http://www.w3.org/2001/XInclude"> 
        <cellModel id="spokeLEDPCell"> 
            <var id="g1" type="double"/> 
            <var id="xelmax1" type="double"/> 
            <var id="rfpdeg1" type="double"/> 
            <slot id="slot010" model="spokeLEDP"> 
                <d id="g1" type="double">g1</d> 
            </slot> 
            <slot id="slot020" model="spokeCryomodule"> 
                <d id="xelmax1" type="double">xelmax1</d> 
                <d id="rfpdeg1" type="double">rfpdeg1</d> 
            </slot> 
        </cellModel> 
        <cellModel id="spokeCell"> 
            <var id="g1" type="double"/> 
            <var id="xelmax1" type="double"/> 
            <var id="rfpdeg1" type="double"/> 
            <slot id="slot010" model="spokeLwu"> 
                <d id="g1" type="double">g1</d> 
            </slot> 
            <slot id="slot020" model="spokeCryomodule"> 
                <d id="xelmax1" type="double">xelmax1</d> 
                <d id="rfpdeg1" type="double">rfpdeg1</d> 
            </slot> 
        </cellModel> 
    </cellModels> 
    <cellModels id="medBetaCellModels" xml:base="medBeta.xml" xmlns:xi="http://www.w3.org/2001/XInclude"> 
        <cellModel id="medBetaCell"> 
            <var id="g1" type="double"/> 
            <var id="xelmax1" type="double"/> 
            <var id="rfpdeg1" type="double"/> 
            <slot id="slot010" model="medBetaLwu"> 
                <d id="g1" type="double">g1</d> 
            </slot> 
            <slot id="slot020" model="medBetaCryomodule"> 
                <d id="xelmax1" type="double">xelmax1</d> 
                <d id="rfpdeg1" type="double">rfpdeg1</d> 
            </slot> 
        </cellModel> 
    </cellModels> 
</header> 
<linac> 
    <section id="SPOK" rfHarmonic="1" xml:base="spoke.xml" xmlns:xi="http://www.w3.org/2001/XInclude"> 
        <cell id="cell010" model="spokeLEDPCell"> 
            <d id="g1" type="double">5.23025</d> 
            <d id="g2" type="double">-4.68975</d> 
            <d id="xelmax1" type="double">0.868945</d> 
            <d id="xelmax2" type="double">0.865525</d> 
            <d id="rfpdeg1" type="double">-6.65943</d> 
            <d id="rfpdeg2" type="double">4.03247</d> 
        </cell> 
        <cell id="cell020" model="spokeCell"> 
            <d id="g1" type="double">4.85226</d> 
            <d id="g2" type="double">-4.77927</d> 
            <d id="xelmax1" type="double">0.890626</d> 
            <d id="xelmax2" type="double">0.891124</d> 
            <d id="rfpdeg1" type="double">22.0298</d> 
            <d id="rfpdeg2" type="double">31.6618</d> 
        </cell> 
        <cell id="cell030" model="spokeCell"> 
            <d id="g1" type="double">4.46164</d> 
            <d id="g2" type="double">-4.45154</d> 
            <d id="xelmax1" type="double">1</d> 
            <d id="xelmax2" type="double">1</d> 
            <d id="rfpdeg1" type="double">37.712</d> 
            <d id="rfpdeg2" type="double">47.397</d> 
        </cell> 
    <!--SPOKE--> 
    <section id="MBL" rfHarmonic="2" xml:base="medBeta.xml" xmlns:xi="http://www.w3.org/2001/XInclude"> 
        <cell id="cell010" model="medBetaCell"> 
            <d id="g1" type="double">3.39121</d> 
            <d id="g2" type="double">-3.31534</d> 
            <d id="xelmax1" type="double">0.44729</d> 
            <d id="xelmax2" type="double">0.4477</d> 
            <d id="xelmax3" type="double">0.453125</d> 
            <d id="xelmax4" type="double">0.453307</d> 
            <d id="rfpdeg1" type="double">55.8358</d> 
            <d id="rfpdeg2" type="double">61.7858</d> 
            <d id="rfpdeg3" type="double">66.1437</d> 
            <d id="rfpdeg4" type="double">72.2867</d> 
        </cell> 
        <cell id="cell020" model="medBetaCell"> 
            <d id="g1" type="double">3.60124</d> 
            <d id="g2" type="double">-3.64339</d> 
            <d id="xelmax1" type="double">0.512886</d> 
            <d id="xelmax2" type="double">0.512886</d> 
            <d id="xelmax3" type="double">0.512886</d> 
            <d id="xelmax4" type="double">0.512886</d> 
            <d id="rfpdeg1" type="double">77.201</d> 
            <d id="rfpdeg2" type="double">84.17</d> 
            <d id="rfpdeg3" type="double">91.207</d> 
            <d id="rfpdeg4" type="double">98.296</d> 
        </cell> 
    </section> 
</linac> 

请注意,XML 文件的结构是“slots”和“cells”,它们基本上是具有可识别模式的 ble 列表,在标题中定义,而数据保存在“linac”中。

我想做什么

我想扫描“直线加速器”,并将每个单元展开为我编写的类的完全实例化对象列表,以表示每个 ble。

为此,我希望能够以这样一种方式解析 header ,即返回可为每个 slotModel 或 cellModel 调用的函数。换句话说,我想自动生成如下函数:

def spokeLEDP(g1): 
    bleList = [] 
    bleList.append(drift(l=240, r=20, ry=0) 
    bleList.append(quad(l=310, g=g1, r=30) 
    return bleList 
 
def spokeLEDPcell(g1, xelmax1, rfpdeg1): 
    myList = [] 
    myList.append(spokeLEDP(g1)) 
    myList.append(spokeCryomodule(xelmax1, rfpdeg1)) 
    return myList 

我知道我需要适本地展平列表,但我希望你明白这一点。

我的计划

我目前看到的从 header 动态生成函数的唯一方法是一个两步过程。首先,使用 Python 创建一个包含必要函数的文本文件。然后将其导入工作代码。

这看起来很笨拙。

我的问题

有没有一种方法可以一步完成我想做的事情,而无需对 XML header 中的“cell”和“slot”元素进行大量重复解析?

非常感谢您阅读到这里,并感谢您提供的任何帮助。

(注意:我无法控制 XML 文件的结构。)

请您参考如下方法:

您可以使用 DOM 解析器路由,为每个节点类型设置处理程序 - 基本上只是访问每个节点,运行您的自定义代码......等等。这是一个非常常见的模式。

有几个 XML 到 python 的对象解决方案,没有一个是“很棒的”,某种工作,其他的并没有真正工作但是是出于好意(或非常有限)。

一个相当晦涩但有趣的包是 https://github.com/scieloorg/porteira 一个雄心勃勃的..有点作品包是http://pyxb.sourceforge.net/ ,您可以在其中根据模式进行“数据绑定(bind)”。这是正确的想法,但像大多数 python XML 包一样有一些粗糙的边缘。我遇到了 namespace 问题(这是理所当然的,对于大多数 python 项目来说,这可能是一个极端情况。如果您喜欢使用 XSD 和所有爵士乐的 super 硬核 XML,那么情况就更少了。

另一种解决方案是学习 XPath 并使用它来查询返回具有属性或字典的对象的结构。剥猫皮的方法有很多种 - 希望我已经为您提供了一些可能的解决方案。