0

以下代码比较了一对 python 代码的两个 ast 结构(以 XML 格式)。树之间的唯一区别是如果条件变为-1-中的-0-。

问题是 diff 函数返回多个编辑操作,而不仅仅是一个。

主要代码;

from xmldiff import main

diff = main.diff_files('before.txt', 'after.txt', diff_options={'ratio_mode': 'accurate'})

for action in diff:
    print(action)

阿斯特;

<?xml version="1.0" ?>
<file_input>
    <funcdef>
        <name label="tri_recursion"/>
        <parameters>
            <param>
                <name label="k"/>
            </param>
        </parameters>
        <suite>
            <if_stmt>
                <atom>
                    <comparison>
                        <name label="k"/>
                        <operator label="&gt;"/>
                        <number label="0"/>
                    </comparison>
                </atom>
                <suite>
                    <simple_stmt>
                        <expr_stmt>
                            <name label="result"/>
                            <operator label="="/>
                            <arith_expr>
                                <name label="k"/>
                                <operator label="+"/>
                                <atom_expr>
                                    <name label="tri_recursion"/>
                                    <trailer>
                                        <arith_expr>
                                            <name label="k"/>
                                            <operator label="-"/>
                                            <number label="1"/>
                                        </arith_expr>
                                    </trailer>
                                </atom_expr>
                            </arith_expr>
                        </expr_stmt>
                    </simple_stmt>
                    <simple_stmt>
                        <atom_expr>
                            <name label="print"/>
                            <trailer>
                                <name label="result"/>
                            </trailer>
                        </atom_expr>
                    </simple_stmt>
                </suite>
                <suite>
                    <simple_stmt>
                        <expr_stmt>
                            <name label="result"/>
                            <operator label="="/>
                            <number label="0"/>
                        </expr_stmt>
                    </simple_stmt>
                </suite>
            </if_stmt>
            <simple_stmt>
                <return_stmt>
                    <name label="result"/>
                </return_stmt>
            </simple_stmt>
        </suite>
    </funcdef>
</file_input>

正如我之前写的,除了 0 变成 1 之外,两棵树完全相同。所以输出应该是;

UpdateAttrib(node='/file_input/funcdef/suite/if_stmt/suite[1]/simple_stmt[1]/expr_stmt/arith_expr/atom_expr/trailer/arith_expr/number[1]', name='label', value='1')

这是我得到的输出;

MoveNode(node='/file_input/funcdef/suite/if_stmt/suite[1]/simple_stmt[1]/expr_stmt/arith_expr/atom_expr/trailer/arith_expr/number[1]', target='/file_input/funcdef/suite/if_stmt/atom/comparison[1]', position=2)
MoveNode(node='/file_input/funcdef/suite/if_stmt/atom/comparison/number[2]', target='/file_input/funcdef/suite/if_stmt/suite[2]/simple_stmt/expr_stmt[1]', position=2)
MoveNode(node='/file_input/funcdef/suite/if_stmt/suite[2]/simple_stmt/expr_stmt/number[2]', target='/file_input/funcdef/suite/if_stmt/suite[1]/simple_stmt[1]/expr_stmt/arith_expr/atom_expr/trailer/arith_expr[1]', position=2)
UpdateAttrib(node='/file_input/funcdef/suite/if_stmt/suite[1]/simple_stmt[1]/expr_stmt/arith_expr/atom_expr/trailer/arith_expr/number[1]', name='label', value='1')

我已经浏览了 xmldiff 文档,但无法解决问题。

有什么方法可以防止不必要的被退回或以某种方式优化它?

如果您能提供帮助,我将不胜感激。

4

0 回答 0