From c1249136da381be72fb57613a67ef66141e141e5 Mon Sep 17 00:00:00 2001 From: Erica Date: Thu, 15 Mar 2018 20:07:41 -0400 Subject: [PATCH] Added error handling to file reading; fixes #3 --- caduceus/core.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/caduceus/core.py b/caduceus/core.py index 735d82b..389c857 100644 --- a/caduceus/core.py +++ b/caduceus/core.py @@ -15,20 +15,25 @@ def build_snakes(common_file, sci_file): Returns: an unsorted list of Snake items ''' snakes = [] +# that above may be the docstring? - with open(common_file, 'rb') as csvfile: - reader = csv.DictReader(csvfile) - for row in reader: - s = Snake(0, 0, row["common name"], row["scientific name"]) - snakes.append(s) + try: + with open(common_file, 'rb') as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + s = Snake(0, 0, row["common name"], row["scientific name"]) + snakes.append(s) - for snake in snakes: - with open(sci_file, 'rb') as csvfile2: - reader2 = csv.DictReader(csvfile2) - for row in reader2: - if snake.sci_name == row['scientific name']: - snake.weight = int(row["weight"]) - snake.length = int(row['length']) + for snake in snakes: + with open(sci_file, 'rb') as csvfile2: + reader2 = csv.DictReader(csvfile2) + for row in reader2: + if snake.sci_name == row['scientific name']: + snake.weight = int(row["weight"]) + snake.length = int(row['length']) + + except: #I don't know what sort of error it would be + print "Cadeceus expects specific columns in csv files to work. See help(build_snakes)." return snakes