编程语言
首页 > 编程语言> > python小练习:涉及print,json,numpy

python小练习:涉及print,json,numpy

作者:互联网

枚举参考文件夹中的文件,并与待比较文件件中的同名文件比较是否一致。

#! /usr/bin/python3.6
# -*- coding:utf-8 -*-

import os
import sys
import json
import numpy as np
from sqlalchemy import false


def cmp_file(ref_file: str, dst_file: str) -> bool:
    ref_base_name = os.path.basename(ref_file)
    dst_base_name = os.path.basename(dst_file)
    assert os.path.exists(ref_file), f"ref file not exist: {ref_base_name}"
    if not os.path.exists(dst_file):
        print(f'dst file not exist: {dst_base_name}')
        return false

    ref_data = np.fromfile(ref_file, dtype=np.ubyte, count=-1)
    dst_data = np.fromfile(dst_file, dtype=np.ubyte, count=-1)
    is_equal = np.array_equal(ref_data, dst_data)
    print(is_equal, ": ", ref_base_name)
    return is_equal


def cmp_dir(ref_dir: str, dst_dir: str) -> None:
    print(f'\n==========>>> Start compare {ref_dir} and {dst_dir}')
    ref_names = os.listdir(ref_dir)
    for name in ref_names:
        ref_file = os.path.join(ref_dir, name)
        dst_file = os.path.join(dst_dir, name)
        cmp_file(ref_file, dst_file)


def main():
    if len(sys.argv) < 2:
        print('usage: dump_dir_cmp.py dir_config.json')
        return

    json_file = sys.argv[1]
    with open(json_file) as fp:
        js_data = json.load(fp)
        for dst_dir in js_data['dst_dirs']:
            cmp_dir(js_data['ref_dir'], dst_dir)


if (__name__ == '__main__'):
    main()

配置样例:

{
    "ref_dir": "./dump_data/NPU_DUMPF001_P0/tensorflow_squeezenet_task0_loop0",
    "dst_dirs": [
        "./dump_data/NPU_DUMPF002_P0/tensorflow_squeezenet_task0_loop0",
        "./dump_data/NPU_DUMPF002_P0/tensorflow_squeezenet_task1_loop0",
        "./dump_data/NPU_DUMPF002_P0/tensorflow_squeezenet_task2_loop0",
        "./dump_data/NPU_DUMPF002_P0/tensorflow_squeezenet_task3_loop0",
        "./dump_data/NPU_DUMPF002_P0/tensorflow_squeezenet_task4_loop0",
        "./dump_data/NPU_DUMPF002_P0/tensorflow_squeezenet_task5_loop0"
    ]
}

标签:name,python,data,dst,json,file,print,ref,dir
来源: https://www.cnblogs.com/vaughnhuang/p/16192008.html