"""Download the exact public sample used by tutorial revision 1. Python 3.10+.""" import argparse import hashlib from pathlib import Path import urllib.request URL = 'https://historicaldata.net/file/options_sample_2022H2.zip' SHA256 = '8517b069f4d41e44f950dc79a3cdab694359b0fae91664190011e39dd8e48b76' def digest(path): h = hashlib.sha256() with path.open('rb') as f: while chunk := f.read(1024 * 1024): h.update(chunk) return h.hexdigest() def download(path): if path.exists(): if digest(path) != SHA256: raise ValueError('Existing file differs from tutorial sample. Choose another output path.') return part = path.with_suffix(path.suffix + '.part') # Exclusive creation never overwrites an existing download or user file. with part.open('xb') as out, urllib.request.urlopen(URL, timeout=90) as response: while chunk := response.read(1024 * 1024): out.write(chunk) if digest(part) != SHA256: raise ValueError('Sample version changed or download is incomplete. Kept .part for inspection.') part.rename(path) if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--output', type=Path, default=Path('options_sample_2022H2.zip')) args = parser.parse_args() download(args.output) print('Verified SHA-256 ' + SHA256)