Skip to content

error checks and support for printing sorted by length #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions caduceus/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,40 @@ def build_snakes(common_file, sci_file):
with open(common_file, 'rt') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
s = Snake(0, 0, row["common name"], row["scientific name"])
snakes.append(s)
try:
s = Snake(0, 0, row['common name'], row['scientific name'])
snakes.append(s)
except ValueError:
print("Caduceus expects specific columns in csv files to work.\
See help(build_snakes) for the specifications")

for snake in snakes:
with open(sci_file, 'rt') 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'])
try:
snake.weight = int(row['weight'])
snake.length = int(row['length'])
except ValueError:
print("Caduceus expects specific columns in csv files \
to work. See help(build_snakes) for the specifications")

return snakes

def print_snakes_by_length(snakes):
'''
prints common names of snakes, one per line, sorted by length
Args:
snakes: a list of Snake objects
'''

sorterer = SnakeSorter(snakes)
sorterer.sort_by_length()

for snek in sorterer.sorted_snakes:
print('{name}: {len}cm'.format(name=snek.common_name, len=snek.length))

def print_snakes_by_weight(snakes):
'''
Prints common names of snakes, one per line, sorted by weight
Expand Down