def from_url(cls, url: str, errors="warn") -> Tuple[Optional["TimeSeries"], Platform]: """Instantiate a TimeSeries (or Scenario) given an ``ixmp://`` URL. The following are equivalent:: from ixmp import Platform, TimeSeries mp = Platform(name='example') scen = TimeSeries(mp 'model', 'scenario', version=42) and:: from ixmp import TimeSeries scen, mp = TimeSeries.from_url('ixmp://example/model/scenario#42') Parameters ---------- url : str See :meth:`parse_url <ixmp.utils.parse_url>`. errors : 'warn' or 'raise' If 'warn', a failure to load the TimeSeries is logged as a warning, and the platform is still returned. If 'raise', the exception is raised. Returns ------- ts, platform : 2-tuple of (TimeSeries, :class:`Platform`) The TimeSeries and Platform referred to by the URL. """ assert errors in ("warn", "raise"), "errors= must be 'warn' or 'raise'" platform_info, scenario_info = parse_url(url) platform = Platform(**platform_info) try: ts = cls(platform, **scenario_info) except Exception as e: if errors == "warn": log.warning( f"{e.__class__.__name__}: {e.args[0]}\n" f"when loading {cls.__name__} from url: {repr(url)}") return None, platform else: raise else: return ts, platform
def test_parse_url(url, p, s): platform_info, scenario_info = utils.parse_url(url) # Expected platform and scenario information is returned assert platform_info == p assert scenario_info == s