module datasetsforecast.utils


function extract_file

extract_file(filepath, directory)

function download_file

download_file(directory: str, source_url: str, decompress: bool = False) → None
Download data from source_ulr inside directory. Args:
  • directory (str, Path): Custom directory where data will be downloaded.
  • source_url (str): URL where data is hosted.
  • decompress (bool): Wheter decompress downloaded file. Default False.

function async_download_files

async_download_files(path: Union[str, Path], urls: Iterable[str])
Example:
import os
import tempfile

import requests
gh_url = 'https://api.github.com/repos/Nixtla/datasetsforecast/contents/'
base_url = 'https://raw.githubusercontent.com/Nixtla/datasetsforecast/main'

headers = {}
gh_token = os.getenv('GITHUB_TOKEN')
if gh_token is not None:
    headers = {'Authorization': f'Bearer: {gh_token}'}
resp = requests.get(gh_url, headers=headers)
if resp.status_code != 200:
    raise Exception(resp.text)
urls = [f'{base_url}/{e["path"]}' for e in resp.json() if e['type'] == 'file']
with tempfile.TemporaryDirectory() as tmp:
    tmp = Path(tmp)
    await async_download_files(tmp, urls)
    files = list(tmp.iterdir())
    assert len(files) == len(urls)

function download_files

download_files(directory: Union[str, Path], urls: Iterable[str])
Example:
with tempfile.TemporaryDirectory() as tmp:
     tmp = Path(tmp)
     fname = tmp / 'script.py'
     fname.write_text(f'''
from datasetsforecast.utils import download_files

download_files('{tmp.as_posix()}', {urls})
     ''')
     !python {fname}
     fname.unlink()
     files = list(tmp.iterdir())
     assert len(files) == len(urls)

class Info

Info Dataclass of datasets. Args:
  • groups (Tuple): Tuple of str groups
  • class_groups (Tuple): Tuple of dataclasses.

method __init__

__init__(class_groups: Tuple[dataclass]) → None

method get_group

get_group(group: str)
Gets dataclass of group.