Skip to Content
Modern Python Standard Library Cookbook
book

Modern Python Standard Library Cookbook

by Alessandro Molina
August 2018
Intermediate to advanced
366 pages
10h 14m
English
Packt Publishing
Content preview from Modern Python Standard Library Cookbook

How it works...

The first thing the function does is open the file in binary mode. As all hash functions require bytes and we don't even know the content of the file, reading it in binary mode is the most convenient solution.

Then, it checks whether the requested hashing algorithm is available in hashlib. That's done through getattr by trying to grab hashlib.md5, hashlib.sha256, and so on. If the algorithm is not supported, it won't be a valid hashlib attribute (as it won't exist in the module) and will throw AttributeError. To make those easier to understand, they are trapped and a new ValueError is raised that states clearly that the algorithm is not supported.

Once the file is opened and the algorithm is verified, an empty hash gets created ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Matthew Wilkes

Publisher Resources

ISBN: 9781788830829Supplemental Content