当自己定义函数过多时,只有一个python文件时代码会很长,不易理清代码框架,比如下面这段代码,如何隐藏具体函数细节呢?也就是把def函数放到另外一个python文件里步骤如下:
一个python文件代码篇幅过长
import os
import gzip
import numpy as np
def load_mnist_gz(image_file_path, label_file_path, flatten=True, normalize=False):
# 定义内部函数来加载标签
def load_labels(file_path):
with gzip.open(file_path, 'rb') as f:
f.read(8) # 跳过文件头
labels = np.frombuffer(f.read(), dtype=np.uint8, offset=0)
return labels
# 定义内部函数来加载图像
def load_images(file_path):
with gzip.open(file_path, 'rb') as f:
f.read(16) # 跳过文件头
images = np.frombuffer(f.read(), dtype=np.uint8, offset=0).reshape(-1, 784)
if normalize:
images = images / 255.0
return images
# 使用内部函数加载数据
t_labels = load_labels(label_file_path)
x_images = load_images(image_file_path)
return x_images, t_labels
# 设置数据集文件路径
train_image_file_path = r'D:\myproject\PyTorch-MNIST-master\DataSets\MNIST\raw\train-images-idx3-ubyte.gz'
train_label_file_path = r'D:\myproject\PyTorch-MNIST-master\DataSets\MNIST\raw\train-labels-idx1-ubyte.gz'
test_image_file_path = r'D:\myproject\PyTorch-MNIST-master\DataSets\MNIST\raw\t10k-images-idx3-ubyte.gz'
test_label_file_path = r'D:\myproject\PyTorch-MNIST-master\DataSets\MNIST\raw\t10k-labels-idx1-ubyte.gz'
# 使用定义的函数加载数据集
x_train, t_train = load_mnist_gz(train_image_file_path, train_label_file_path, flatten=True, normalize=False)
x_test, t_test = load_mnist_gz(test_image_file_path, test_label_file_path, flatten=True, normalize=False)
# 打印数据集的形状
print(x_train.shape) # (60000, 784)
print(t_train.shape) # (60000,)
print(x_test.shape) # (10000, 784)
print(t_test.shape) # (10000,)
调整后易于理解框架:
1.首先要新建文件夹,因为有两个python文件,必须要有文件夹
2.定义两个python文件,需要注意的时封装的函数也要导入相关的库比如
import os
import gzip
import numpy as np
def load_mnist_gz(image_file_path, label_file_path, flatten=True, normalize=False):
def load_labels(file_path):
with gzip.open(file_path, 'rb') as f:
f.read(8) # 跳过文件头
labels = np.frombuffer(f.read(), dtype=np.uint8, offset=0)
return labels
def load_images(file_path):
with gzip.open(file_path, 'rb') as f:
f.read(16) # 跳过文件头
images = np.frombuffer(f.read(), dtype=np.uint8, offset=0).reshape(-1, 784)
if normalize:
images = images / 255.0
return images
t_labels = load_labels(label_file_path)
x_images = load_images(image_file_path)
return x_images, t_labels
如果没有上面的三个库也会报错,原因是这个函数需要用到上面的那三个库,而不是简单的把def函数内容复制过来
3.在主函数里面加入要导入的自定义函数,这个例子是导入
from dataset.minst import load_mnist_gz
自定义函数在dataset文件夹下的minst.py中,然后导入自定义的函数名比如load_minst_gz即可,当相对路径导入报错时候就导入绝对路径即可