text: import os
  import requests
  import gzip
  
  
  class MNISTFileLoader():
      __base_url = "http://yann.lecun.com/exdb/mnist"
      __filenames = {
          "train": {
              "image": {
                  "file": "train-images.idx3-ubyte",
                  "gz": "train-images-idx3-ubyte.gz",
              },
              "label": {
                  "file": "train-labels.idx1-ubyte",
                  "gz": "train-labels-idx1-ubyte.gz",
              },
          },
          "test": {
              "image": {
                  "file": "t10k-images.idx3-ubyte",
                  "gz": "t10k-images-idx3-ubyte.gz",
              },
              "label": {
                  "file": "t10k-labels.idx1-ubyte",
                  "gz": "t10k-labels-idx1-ubyte.gz",
              },
          }
      }
  
      def __init__(self, path="./MNIST_Data"):
          self.__path = path
          self.__filepaths = {
              "train": {
                  "image": {
                      "file": os.path.join(
                          path, self.__filenames["train"]["image"]["file"]),
                      "gz": os.path.join(
                          path, self.__filenames["train"]["image"]["gz"]),
                  },
                  "label": {
                      "file": os.path.join(
                          path, self.__filenames["train"]["label"]["file"]),
                      "gz": os.path.join(
                          path, self.__filenames["train"]["label"]["gz"]),
                  },
              },
              "test": {
                  "image": {
                      "file": os.path.join(
                          path, self.__filenames["test"]["image"]["file"]),
                      "gz": os.path.join(
                          path, self.__filenames["test"]["image"]["gz"]),
                  },
                  "label": {
                      "file": os.path.join(
                          path, self.__filenames["test"]["label"]["file"]),
                      "gz": os.path.join(
                          path, self.__filenames["test"]["label"]["gz"]),
                  },
              },
          }
  
          if not os.path.exists(self.__path):
              os.makedirs(self.__path)
  
      def get_train_image(self):
          self.__prepare_file("train", "image")
          return self.__read_file("train", "image", "file")
  
      def get_train_label(self):
          self.__prepare_file("train", "label")
          return self.__read_file("train", "label", "file")
  
      def get_test_image(self):
          self.__prepare_file("test", "image")
          return self.__read_file("test", "image", "file")
  
      def get_test_label(self):
          self.__prepare_file("test", "label")
          return self.__read_file("test", "label", "file")
  
      def __check_file_exist(self, *keys):
          path = self.__filepaths
          for key in keys:
              path = path[key]
          file_path = path["file"]
          gz_path = path["gz"]
  
          print(f"check file exist :\n[{file_path}]\n[{gz_path}]")
  
          is_file_exist = os.path.exists(file_path)
          is_gz_exist = os.path.exists(gz_path)
  
          return is_file_exist, is_gz_exist
  
      def __fetch_file(self, *keys):
          filepath = self.__filepaths
          for key in keys:
              filepath = filepath[key]
  
          filename = self.__filenames
          for key in keys:
              filename = filename[key]
  
          print(f"fetch file :\n[{filename}]")
          url = f"{self.__base_url}/{filename}"
          with open(filepath, "wb") as f:
              response = requests.get(url)
              f.write(response.content)
  
      def __unzip_file(self, *keys):
          filepath = self.__filepaths
          for key in keys:
              filepath = filepath[key]
          gz_path = filepath["gz"]
          file_path = filepath["file"]
          print(f"unzip file :\n[{gz_path}]")
  
          with gzip.open(gz_path, 'rb') as gz_file:
              with open(file_path, "wb") as unzip_file:
                  unzip_file.write(gz_file.read())
  
      def __prepare_file(self, *keys):
          is_file_exist, is_gz_exist = self.__check_file_exist(*keys)
  
          if not is_file_exist and not is_gz_exist:
              self.__fetch_file(*keys, "gz")
              self.__unzip_file(*keys)
              return
  
          if not is_file_exist:
              self.__unzip_file(*keys)
              return
  
      def __read_file(self, *keys):
          filepath = self.__filepaths
          for key in keys:
              filepath = filepath[key]
          return open(filepath, "rb")
  