接手项目代码

Posted frank99

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接手项目代码相关的知识,希望对你有一定的参考价值。

原项目代码如下

def data_filter(src=r‘D:materialdata.json‘,des=r‘D:materialpro_data.json‘,db = r‘D:material	nbs.db‘):
    fw = open(des, ‘w‘, encoding=‘utf-8‘)
    with open(src, encoding=‘utf-8‘) as fr:
        lines = fr.readlines()

        legend_num = len(lines)

        line_1 = eval(lines[1].lstrip())
        conn = sqlite3.connect(db)
        cursor = conn.cursor()
        dwg_name = line_1[‘dwg_name‘]

        cursor.execute(‘delete from t_designRecStat‘)
        filtered_num = 0
        for index in range(len(lines)):
            line = eval(lines[index].lstrip())
            pole_info_tag = True
            if ‘wires_info‘ in line[‘pole_info‘].keys():
                for pole_info in line[‘pole_info‘][‘wires_info‘]:
                    if ‘materials_selection‘ not in pole_info.keys():
                        pole_info_tag = False
            else:
                pole_info_tag = False

            if ‘materials_selection‘ in line[‘pole_info‘].keys() and pole_info_tag:
                fw.write(re.sub(‘‘‘, ‘"‘, str(line)) + ‘
‘)
            else:
                filtered_num += 1
        rec_amount = legend_num - filtered_num
        t_designRecStat_sql = ‘insert into t_designRecStat(designName,legendAmount,recAmount,filteredNum) values(‘ + ‘‘‘ + dwg_name + ‘‘‘                               ‘,‘ + ‘‘‘ + str(legend_num) + ‘‘‘ + ‘,‘ + ‘‘‘ + str(rec_amount) + ‘‘‘ + ‘,‘ + ‘‘‘ + str(filtered_num) + ‘‘‘ + ‘)‘
        cursor.execute(t_designRecStat_sql)
        conn.commit()
        conn.close()
    fw.close()

修改后

def sqlite_dml(dml_sql, db_name=r‘D:material	nbs_2.db‘, seq_of_parameters=‘‘):
    conn = sqlite3.connect(db_name)
    cursor = conn.cursor()
    cursor.executemany(dml_sql, seq_of_parameters)
    conn.commit()
    conn.close()

@timeit
def data_filter(src=r‘D:materialdata.json‘,des=r‘D:materialpro_data_2.json‘,db = r‘D:material	nbs_2.db‘):
    t_designRecStat_sql_prefix = ‘insert into t_designRecStat(designName,legendAmount,recAmount,filteredNum) values‘
    t_designRecStat_sql_params = []
    with open(des, ‘w‘, encoding=‘utf-8‘) as fw:
        with open(src, encoding=‘utf-8‘) as fr:
            lines = fr.readlines()
            legend_num = len(lines)
            line_1 = json.loads(lines[1])
            dwg_name = line_1[‘dwg_name‘]
            filtered_num = 0
            for line in lines:
                line = json.loads(line)
                pole_info_tag = all((‘materials_selection‘ in line[‘pole_info‘].keys(), all([‘materials_selection‘ in pole_info.keys() for pole_info in line[‘pole_info‘][‘wires_info‘]]) if ‘wires_info‘ in line[‘pole_info‘].keys() else False))
                if pole_info_tag:
                    json.dump(line, fw, ensure_ascii=False)
                else:
                    filtered_num += 1
            rec_amount = legend_num - filtered_num
            params_list = [dwg_name, legend_num, rec_amount, filtered_num]
            params = ‘(‘+‘,‘.join(map(lambda s: "‘" + s + "‘" if isinstance(s, str) else str(s), params_list))+‘)‘
            t_designRecStat_sql_params.append(params)
        t_designRecStat_sql = t_designRecStat_sql_prefix + ‘,‘.join(t_designRecStat_sql_params)
        sqlite_dml(‘delete from t_designRecStat‘)
        sqlite_dml(t_designRecStat_sql)

以上是关于接手项目代码的主要内容,如果未能解决你的问题,请参考以下文章

接手了屎一样的代码 怎么办?

接手了屎一样的代码 怎么办?

微信小程序代码片段

全中了!接手同事项目时最崩溃的事 | 每日趣闻

《重构,改善既有代码的设计》读书笔记

程序员:如何接手垃圾代码?