Skip to content
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

gh-154: update example notebook for public release #175

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ _version.py
.coverage*
.envrc
*.lcov
docs/_build
examples/catalog.fits
examples/vmap.fits.gz
examples/nz.npz
869 changes: 348 additions & 521 deletions examples/example.ipynb

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions examples/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Helper functions for examples.
"""

import os
import urllib.request
import hashlib
import heracles


def checksum(
path: str,
*,
md5: str | None = None,
) -> bool:
"""
Check a local file against a given checksum.
"""

md5sum = hashlib.md5() if md5 else None

with open(path, "rb") as fp:
while block := fp.read(4096):
if md5sum:
md5sum.update(block)

if md5sum and md5sum.hexdigest() != md5:
return False

return True


def download(
path: str,
url: str,
*,
overwrite: bool = False,
keep: bool = False,
md5: str | None = None,
progress: heracles.Progress | None = None,
) -> None:
"""
Download a file.
"""

def reporthook(count: int, block_size: int, total_size: int) -> None:
if progress is not None:
current = count * block_size
if total_size > -1 and current > total_size:
current = total_size
progress.update(current, total_size if total_size > -1 else None)

if os.path.exists(path):
if checksum(path, md5=md5):
return
if not overwrite:
raise FileExistsError(path)
good = False
try:
urllib.request.urlretrieve(url, path, reporthook=reporthook)
if not checksum(path, md5=md5):
raise ValueError(f"{path}: invalid checksum, removing")
good = True
finally:
if not good and not keep:
os.unlink(path)