【问题标题】:od.download() == None but it's not empty | Exception handling Opendatasetsod.download() == None 但它不是空的 |异常处理开放数据集
【发布时间】:2021-07-20 08:31:45
【问题描述】:

更新:原来od.download() 按设计返回None

有什么比None 检查od.download()“失败”更好?


我正在使用opendatasets lib 下载.zip 文件。

iris_scans();行print(download),没有if-statement 打印None

但是,在调用scans = iris_scans() 时返回数据,后续打印可以成功显示数据。


if-statement 的目的是“优雅的错误处理”。

注意:我使用了if-statement 而不是try-except,因为download == None 的原因有很多(例如死链接、连接中断等)


pip3 install opendatasets
import opendatasets as od
import zipfile
import os
import shutil
from PIL import Image
import numpy as np

def iris_scans():
  download = od.download('http://www.mae.cuhk.edu.hk/~cvl/iris_database/iris_database.zip')
  """
  if download == None:
    print('Iris Scans - Link could not be established')
    return [[]*1778]
  """
  print(download)

  path_extract = 'iris_database/'
  with zipfile.ZipFile('iris_database.zip', 'r') as zip_ref:
    zip_ref.extractall(path_extract)
  
  os.remove(path_extract + 'readme.txt')
  
  filenames = os.listdir(path_extract)
  scans = []
  for f in filenames:
    img = Image.open(path_extract + f)
    #print("IMG", img)
    matrix = np.array(img)
    #print("MATRIX", matrix)
    scans.append(matrix)
  
  shutil.rmtree(path_extract)
  os.remove(path_extract[:-1] + '.zip')
  
  # Data Augmentation
  scans_90 = [np.rot90(s) for s in scans]
  scans_180 = [np.rot90(s) for s in scans_90]
  scans_270 = [np.rot90(s) for s in scans_180]

  scans_flip = [np.flip(s) for s in scans]
  scans_flip_90 = [np.rot90(s) for s in scans_flip]
  scans_flip_180 = [np.rot90(s) for s in scans_flip_90]
  scans_flip_270 = [np.rot90(s) for s in scans_flip_180]

  scans += scans_90
  scans += scans_180
  scans += scans_270

  scans += scans_flip_90
  scans += scans_flip_180
  scans += scans_flip_270

  return scans

scans = iris_scans()
print(scans[0])
print(len(scans))

【问题讨论】:

  • 查看source on Github,该函数没有返回任何内容,因此它始终为None。我认为您必须使用 try/except 语句。
  • 奇怪的是它的设计...有什么可以替代if download == None,而不是try-except?在这种情况下,我需要有很多行,每行都涵盖所有可能的错误名称。
  • 或者,为了制作try-excepts,Github 资源是否在任何地方列出了所有错误名称?我找不到他们。
  • 查看我链接的文件的第 58 行和第 71 行,它可以引发 urllib.error.URLErrorIOErrorRuntimeError
  • @Henry 参考我的解决方案。

标签: python-3.x nonetype


【解决方案1】:

最初的问题是为下载实现某种形式的异常处理

od.download() == None 设计;所以需要替代if download == None

正如@Henry 指出和协助的那样;下面的Try-except 包含了Github source 中发现的所有异常。

...
import urllib

def iris_scans():
  try:
    download = od.download('http://www.dgcdgyugcwyugyugcasc.com/wqduiuwqdwq') # BROKEN
    ...

    return scans
  
  except (urllib.error.URLError, IOError, RuntimeError) as e:
    print('Iris Scans - failed')
    return [[]*1778]
Iris Scans - failed
[]
1

post 的最佳答案在一行上演示了许多异常。

【讨论】:

  • 应该可以,测试异常使用不存在的 URL,因为这应该会引发错误。
  • 我做到了,然后:File "main.py", line 57, in iris_scans except (URLError, IOError, RuntimeError) as e: NameError: name 'URLError' is not defined。检查答案以获取持续更新。
  • 您需要在开始时import urllib 并将URLError 更改为urllib.error.URLError
  • Tysm @Henry。上帝在地球上的礼物。
  • 很高兴我能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多