python 文件比对

本文深入探讨了一种用于对比两个代码列表的方法,通过比较类名、返回类型、参数数量及参数类型的匹配度,来评估代码的一致性。文章详细介绍了比较过程中的逻辑判断,包括如何处理参数类型不匹配、参数名称不存在、参数数量错误以及类名不存在等问题。
摘要由CSDN通过智能技术生成
# -*- coding: utf-8 -*-


class Data:
    def __init__(self, a="", b=""):
        self.a = a
        self.b = b

    def return_data(self):
        return self.a, self.b


class CreateObject:
    def __init__(self, class_name="null", class_type=-1, class_length=0, class_param=[]):
        self.class_name = class_name
        self.classType = class_type
        self.classLength = class_length
        self.classParam = class_param


class Interface:
    def __init__(self, result_name=0, error_mag=0):
        self.result_name = result_name
        self.error_mag = error_mag


def compare_files(p, c):
    # 从比较文件中获取一个方法
    msg_list = []
    for i in range(len(c)):
        itf = Interface()
        for j in range(len(p)):
            # 如果方法名一致
            if p[j].className == c[i].className:
                # 如果 返回类型一致
                if p[j].classType == c[i].classType:
                    # 参数长度
                    if p[j].classLength == c[i].classLength:
                        # 遍历参数列表
                        for m in range(len(c[i].classParam)):
                            for n in range(len(p[j].classParam)):
                                # 找出参数依次比较
                                if c[i].classParam[m].paramName == p[j].classParam[n].paramName:
                                    if c[i].classParam[m].paramType == p[j].classParam[n].paramType:
                                        itf.result_name = c[i].className
                                        itf.error_mag = "Success"
                                    else:
                                        print("classLength  i = ", i, "j = ", j, "m =", m, "n = ", n)
                                        if m == len(c[i].classParam) and n == len(p[j].classParam):
                                            itf.result_name = c.className
                                            itf.error_mag = "Wrong param type"
                                else:
                                    print("classLength  i = ", i, "j = ", j, "m =", m, "n = ", n)
                                    if m == len(c[i].classParam) and n == len(p[j].classParam):
                                        itf.result_name = c.className
                                        itf.error_mag = "paramName not exist"
                    else:
                        print("classLength  i = ", i, "j = ", j)
                        # 如果是最后一条数据
                        if j == len(p):
                            itf.result_name = c.className
                            itf.error_mag = "Wrong param number"
                else:
                    print("classType  i = ", i, "j = ", j)
                    # 如果是最后一条数据
                    if j == len(p):
                        itf.result_name = c.className
                        itf.error_mag = "Wrong return type"
            else:
                print ("className  i = ", i, "j = ", j)
                # 如果是最后一条数据
                if j == len(p):
                    itf.result_name = c.className
                    itf.error_mag = "className not exist"
        return msg_list


if __name__ == "__main__":

    lis1 = [
        {
            "className": "cls",
            "classType": "int",
            "classLength": 2,
            "classParam": [
                {
                    "paramName": "p",
                    "paramType": "int"
                }, {
                    "paramName": "p1",
                    "paramType": "int1"
                }]
        },
        {
            "className": "cls1",
            "classType": "int1",
            "classLength": 2,
            "classParam": [
                {
                    "paramName": "p1",
                    "paramType": "int1"
                }, {
                    "paramName": "p11",
                    "paramType": "int11"
                }]
        },
        {
            "className": "cls2",
            "classType": "int2",
            "classLength": 3,
            "classParam": [
                {
                    "paramName": "p2",
                    "paramType": "int2"
                }, {
                    "paramName": "p22",
                    "paramType": "int22"
                }, {
                    "paramName": "p222",
                    "paramType": "int222"
                }]
        },
        {
            "className": "cls2",
            "classType": "int2",
            "classLength": 3,
            "classParam": [
                {
                    "paramName": "p3",
                    "paramType": "int3"
                }, {
                    "paramName": "p33",
                    "paramType": "int33"
                }, {
                    "paramName": "p333",
                    "paramType": "int333"
                }]
        }
    ]

    lis2 = [
        {
            "className": "cls",
            "classType": "int",
            "classLength": 2,
            "classParam": [
                {
                    "paramName": "p",
                    "paramType": "int"
                }, {
                    "paramName": "p1",
                    "paramType": "int1"
                }]
        },
        {
            "className": "cls1",
            "classType": "int1",
            "classLength": 2,
            "classParam": [
                {
                    "paramName": "p1",
                    "paramType": "int1"
                }, {
                    "paramName": "p11",
                    "paramType": "int11"
                }]
        },
        {
            "className": "cls2",
            "classType": "str",
            "classLength": 3,
            "classParam": [
                {
                    "paramName": "p2",
                    "paramType": "int2"
                }, {
                    "paramName": "p22",
                    "paramType": "int22"
                }, {
                    "paramName": "p222",
                    "paramType": "int222"
                }]
        },
        {
            "className": "cls2",
            "classType": "int2",
            "classLength": 3,
            "classParam": [
                {
                    "paramName": "p3",
                    "paramType": "int3"
                }, {
                    "paramName": "p33",
                    "paramType": "int33"
                }, {
                    "paramName": "p333",
                    "paramType": "int333not"
                }]
        }
    ]

    data_obj = CreateObject()



    cf = compare_files(lis1, lis2)
    for inner in cf:
        if inner.result_name is not 'Success':
            print inner.result_name + ":" + inner.error_mag
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值