Skip to content

Fixes to the validation script #3

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 1 commit into
base: main
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
19 changes: 16 additions & 3 deletions first_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def hash_for_fname(fname):
# Convert a string filename to a Path object.
fpath = Path(fname)
# Your code here.
return 'not-really-the-hash'
contents = fpath.read_bytes()
hash_value = sha1(contents).hexdigest()
return hash_value
Comment on lines +35 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or:

Suggested change
hash_value = sha1(contents).hexdigest()
return hash_value
return sha1(contents).hexdigest()



# Fill in the function above to make the test below pass.
Expand All @@ -48,13 +50,24 @@ def check_hashes(hash_fname):
# Directory containing hash filenames file.
data_dir = hash_pth.parent
# Read in text for hash filename
hash_file_contents = hash_pth.read_text()

# Split into lines.
lst_of_splitted_lines = hash_file_contents.splitlines()
# For each line:
for line in lst_of_splitted_lines:
# Split each line into expected_hash and filename
[exp_hash, gap, filename] = line.split(sep=' ')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - but you don't even need the brackets around the list on the left hand side. And you can use the default separator (of any number of spaces) to make it a bit simpler, as in:

Suggested change
[exp_hash, gap, filename] = line.split(sep=' ')
exp_hash, filename = line.split()


# Calculate actual hash for given filename.
act_hash = hash_for_fname(data_dir / filename)

# Check actual hash against expected hash
# Return False if any of the hashes do not match.
return False
result = (act_hash == exp_hash)
Comment on lines -57 to +66
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that you end up returning the result of the last test here, so if any of the previous tests failed, the function still returns True. I think you want something like:

        result = result and (act_hash == exp_hash)

to combine the results of the tests.


# Return False if any of the hashes do not match.
return result

#check_hashes(hashes_pth)
assert check_hashes(hashes_pth), 'Check hash list does not return True'
print("Finished!")