Javascript

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"
    ]
}
这篇关于python小练习:涉及print,json,numpy的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!