-1

Update: turns out od.download() returns None by design.

What might be better than a None check for od.download() "failure"?


I am downloading a .zip file using opendatasets lib.

In iris_scans(); line print(download), without the if-statement prints None.

However, at invocation scans = iris_scans() data is returned and subsequent prints can display data successfully.


The purpose of the if-statement is for "Graceful error handling".

Note: I've used an if-statement instead of try-except as there are many possibilities why download == None (e.g. dead link, connection interrupt etc.)


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))
  • 1
    Looking at the [source on Github](https://github.com/JovianML/opendatasets/blob/6a26edea54d6fbf15eb3cceda68d1bc7c1c4902f/opendatasets/utils/network.py), the function doesn't return anything, so it will always be None. I think you have to use a try/except statement. – Henry Jul 20 '21 at 09:12
  • Strange that it would by design... What is an alternative to `if download == None`, that isn't `try-except`? I would need to have many lines that each cover all possible error names in that case. –  Jul 20 '21 at 09:18
  • Or, for making `try-except`s, does that Github resource list all Error names anywhere? I cannot find them. –  Jul 20 '21 at 09:22
  • 1
    Looking at line 58 and 71 of the file I linked, it can raise either `urllib.error.URLError`, `IOError` or `RuntimeError`. – Henry Jul 20 '21 at 09:38
  • @Henry Refer to my solution. –  Jul 20 '21 at 09:49

1 Answers1

0

The original question was a road-block on the path of implementing some form of Exception Handling for the download.

od.download() == None by design; so an alternative to if download == None needed to be made.

As pointed out and assisted by @Henry; the below Try-except incorporates all exceptions found in the 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

Top answer to this post demos many exceptions on one line.

  • 1
    That should work, to test exceptions use a non-existent URL as this should then raise an error. – Henry Jul 20 '21 at 10:03
  • I did that and: `File "main.py", line 57, in iris_scans except (URLError, IOError, RuntimeError) as e: NameError: name 'URLError' is not defined`. Check answer for continual updates. –  Jul 20 '21 at 10:08
  • 1
    You need to `import urllib` at the start and change `URLError` to `urllib.error.URLError` – Henry Jul 20 '21 at 10:11
  • Tysm @Henry. God's gift on Earth. –  Jul 20 '21 at 10:17
  • 1
    Glad I could help :) – Henry Jul 20 '21 at 10:19