のねのBlog

パソコンの問題や、ソフトウェアの開発で起きた問題など書いていきます。よろしくお願いします^^。

ifnoneは、coreの関数

def ifnone(a:Any,b:Any)->Any:
    "`a` if `a` is not None, otherwise `b`."
    return b if a is None else a

https://github.com/fastai/fastai/blob/master/fastai/core.py


「aがNoneでなければa、そうでなければbです。」

def test_ifnone():
    assert ifnone(None, 5) == 5
    assert ifnone(5, None) == 5
    assert ifnone(1, 5)    == 1
    assert ifnone(0, 5)    == 0

https://github.com/fastai/fastai/blob/master/tests/test_core.py

untar_data

def untar_data(
  url:str,
  fname:PathOrStr=None,
 dest:PathOrStr=None, 
 data=True, 
 force_download=False) -> Path:

    "Download `url` to `fname` if it doesn't exist, and un-tgz to folder `dest`."

    dest = url2path(url, data) if dest is None else Path(dest)/url2name(url)
    fname = Path(ifnone(fname, _url2tgz(url, data)))
    if force_download or (fname.exists() and url in _checks and _check_file(fname) != _checks[url]):
        print(f"A new version of the {'dataset' if data else 'model'} is available.")
        os.remove(fname)
        shutil.rmtree(dest)
    if not dest.exists():
        fname = download_data(url, fname=fname, data=data)
        data_dir = Config().data_path()
        if url in _checks:
            assert _check_file(fname) == _checks[url], f"Downloaded file {fname} does not match checksum expected! Remove that file from {data_dir} and try your code again."
        tarfile.open(fname, 'r:gz').extractall(dest.parent)
    return dest

fastai/datasets.py at master · fastai/fastai · GitHub
https://docs.fast.ai/datasets.html#untar_data

"->" アノテーション

qiita.com

この、Pathは、PathlibのPathなのかな?
docs.python.jp