h2o-py/h2o/h2o.py
def init(url=None, ip=None, port=None, name=None, https=None, insecure=None, username=None, password=None,
         cookies=None, proxy=None, start_h2o=True, nthreads=-1, ice_root=None, log_dir=None, log_level=None,
         enable_assertions=True, max_mem_size=None, min_mem_size=None, strict_version_check=None, ignore_config=False,
         extra_classpath=None, jvm_custom_args=None, bind_to_localhost=True, **kwargs):
    """
    Attempt to connect to a local server, or if not successful start a new server and connect to it.

    :param url: Full URL of the server to connect to (can be used instead of `ip` + `port` + `https`).
    :param ip: The ip address (or host name) of the server where H2O is running.
    :param port: Port number that H2O service is listening to.
    :param name: Cluster name. If None while connecting to an existing cluster it will not check the cluster name.
                 If set then will connect only if the target cluster name matches. If no instance is found and decides to start a local
                 one then this will be used as the cluster name or a random one will be generated if set to None.
    :param https: Set to True to connect via https:// instead of http://.
    :param insecure: When using https, setting this to True will disable SSL certificates verification.
    :param username: Username and
    :param password: Password for basic authentication.
    :param cookies: Cookie (or list of) to add to each request.
    :param proxy: Proxy server address.
    :param start_h2o: If False, do not attempt to start an h2o server when connection to an existing one failed.
    :param nthreads: "Number of threads" option when launching a new h2o server.
    :param ice_root: Directory for temporary files for the new h2o server.
    :param log_dir: Directory for H2O logs to be stored if a new instance is started. Ignored if connecting to an existing node.
    :param log_level: The logger level for H2O if a new instance is started. One of TRACE,DEBUG,INFO,WARN,ERRR,FATA. Default is INFO. Ignored if connecting to an existing node.
    :param enable_assertions: Enable assertions in Java for the new h2o server.
    :param max_mem_size: Maximum memory to use for the new h2o server. Integer input will be evaluated as gigabytes.  Other units can be specified by passing in a string (e.g. "160M" for 160 megabytes).
    :param min_mem_size: Minimum memory to use for the new h2o server. Integer input will be evaluated as gigabytes.  Other units can be specified by passing in a string (e.g. "160M" for 160 megabytes).
    :param strict_version_check: If True, an error will be raised if the client and server versions don't match.
    :param ignore_config: Indicates whether a processing of a .h2oconfig file should be conducted or not. Default value is False.
    :param extra_classpath: List of paths to libraries that should be included on the Java classpath when starting H2O from Python.
    :param kwargs: (all other deprecated attributes)
    :param jvm_custom_args: Customer, user-defined argument's for the JVM H2O is instantiated in. Ignored if there is an instance of H2O already running and the client connects to it.
    """
    global h2oconn
    assert_is_type(url, str, None)
    assert_is_type(ip, str, None)
    assert_is_type(port, int, str, None)
    assert_is_type(name, str, None)
    assert_is_type(https, bool, None)
    assert_is_type(insecure, bool, None)
    assert_is_type(username, str, None)
    assert_is_type(password, str, None)
    assert_is_type(cookies, str, [str], None)
    assert_is_type(proxy, {str: str}, None)
    assert_is_type(start_h2o, bool, None)
    assert_is_type(nthreads, int)
    assert_is_type(ice_root, str, None)
    assert_is_type(log_dir, str, None)
    assert_is_type(log_level, str, None)
    assert_satisfies(log_level, log_level in [None, "TRACE", "DEBUG", "INFO", "WARN", "ERRR", "FATA"])
    assert_is_type(enable_assertions, bool)
    assert_is_type(max_mem_size, int, str, None)
    assert_is_type(min_mem_size, int, str, None)
    assert_is_type(strict_version_check, bool, None)
    assert_is_type(extra_classpath, [str], None)
    assert_is_type(jvm_custom_args, [str], None)
    assert_is_type(bind_to_localhost, bool)
    assert_is_type(kwargs, {"proxies": {str: str}, "max_mem_size_GB": int, "min_mem_size_GB": int,
                            "force_connect": bool, "as_port": bool})

    def get_mem_size(mmint, mmgb):
        if not mmint:  # treat 0 and "" as if they were None
            if mmgb is None: return None
            return mmgb << 30
        if is_type(mmint, int):
            # If the user gives some small number just assume it's in Gigabytes...
            if mmint < 1000: return mmint << 30
            return mmint
        if is_type(mmint, str):
            last = mmint[-1].upper()
            num = mmint[:-1]
            if not (num.isdigit() and last in "MGT"):
                raise H2OValueError("Wrong format for a *_memory_size argument: %s (should be a number followed by "
                                    "a suffix 'M', 'G' or 'T')" % mmint)
            if last == "T": return int(num) << 40
            if last == "G": return int(num) << 30
            if last == "M": return int(num) << 20

    scheme = "https" if https else "http"
    proxy = proxy[scheme] if proxy is not None and scheme in proxy else \
        kwargs["proxies"][scheme] if "proxies" in kwargs and scheme in kwargs["proxies"] else None
    mmax = get_mem_size(max_mem_size, kwargs.get("max_mem_size_GB"))
    mmin = get_mem_size(min_mem_size, kwargs.get("min_mem_size_GB"))
    auth = (username, password) if username and password else None
    check_version = True
    verify_ssl_certificates = True

    # Apply the config file if ignore_config=False
    if not ignore_config:
        config = H2OConfigReader.get_config()
        if url is None and ip is None and port is None and https is None and "init.url" in config:
            url = config["init.url"]
        if proxy is None and "init.proxy" in config:
            proxy = config["init.proxy"]
        if cookies is None and "init.cookies" in config:
            cookies = config["init.cookies"].split(";")
        if auth is None and "init.username" in config and "init.password" in config:
            auth = (config["init.username"], config["init.password"])
        if strict_version_check is None:
            if "init.check_version" in config:
                check_version = config["init.check_version"].lower() != "false"
            elif os.environ.get("H2O_DISABLE_STRICT_VERSION_CHECK"):
                check_version = False
        else:
            check_version = strict_version_check
        if insecure is None:
            if "init.verify_ssl_certificates" in config:
                verify_ssl_certificates = config["init.verify_ssl_certificates"].lower() != "false"
            else:
                verify_ssl_certificates = not insecure

    if not start_h2o:
        print("Warning: if you don't want to start local H2O server, then use of `h2o.connect()` is preferred.")
    try:
        h2oconn = H2OConnection.open(url=url, ip=ip, port=port, name=name, https=https,
                                     verify_ssl_certificates=verify_ssl_certificates,
                                     auth=auth, proxy=proxy,cookies=cookies, verbose=True,
                                     _msgs=("Checking whether there is an H2O instance running at {url} ",
                                            "connected.", "not found."))
    except H2OConnectionError:
        # Backward compatibility: in init() port parameter really meant "baseport" when starting a local server...
        if port and not str(port).endswith("+") and not kwargs.get("as_port", False):
            port = str(port) + "+"
        if not start_h2o: raise
        if ip and not (ip == "localhost" or ip == "127.0.0.1"):
            raise H2OConnectionError('Can only start H2O launcher if IP address is localhost.')
        hs = H2OLocalServer.start(nthreads=nthreads, enable_assertions=enable_assertions, max_mem_size=mmax,
                                  min_mem_size=mmin, ice_root=ice_root, log_dir=log_dir, log_level=log_level,
                                  port=port, name=name,
                                  extra_classpath=extra_classpath, jvm_custom_args=jvm_custom_args,
                                  bind_to_localhost=bind_to_localhost)
        h2oconn = H2OConnection.open(server=hs, https=https, verify_ssl_certificates=not insecure,
                                     auth=auth, proxy=proxy,cookies=cookies, verbose=True)
    if check_version:
        version_check()
    h2oconn.cluster.timezone = "UTC"
    h2oconn.cluster.show_status()
def init(url=None, ip=None, port=None, name=None, https=None, insecure=None, username=None, password=None,
         cookies=None, proxy=None, start_h2o=True, nthreads=-1, ice_root=None, log_dir=None, log_level=None,
         enable_assertions=True, max_mem_size=None, min_mem_size=None, strict_version_check=None, ignore_config=False,
         extra_classpath=None, jvm_custom_args=None, bind_to_localhost=True, **kwargs):
    """
    Attempt to connect to a local server, or if not successful start a new server and connect to it.

    :param url: Full URL of the server to connect to (can be used instead of `ip` + `port` + `https`).
    :param ip: The ip address (or host name) of the server where H2O is running.
    :param port: Port number that H2O service is listening to.
    :param name: Cluster name. If None while connecting to an existing cluster it will not check the cluster name.
                 If set then will connect only if the target cluster name matches. If no instance is found and decides to start a local
                 one then this will be used as the cluster name or a random one will be generated if set to None.
    :param https: Set to True to connect via https:// instead of http://.
    :param insecure: When using https, setting this to True will disable SSL certificates verification.
    :param username: Username and
    :param password: Password for basic authentication.
    :param cookies: Cookie (or list of) to add to each request.
    :param proxy: Proxy server address.
    :param start_h2o: If False, do not attempt to start an h2o server when connection to an existing one failed.
    :param nthreads: "Number of threads" option when launching a new h2o server.
    :param ice_root: Directory for temporary files for the new h2o server.
    :param log_dir: Directory for H2O logs to be stored if a new instance is started. Ignored if connecting to an existing node.
    :param log_level: The logger level for H2O if a new instance is started. One of TRACE,DEBUG,INFO,WARN,ERRR,FATA. Default is INFO. Ignored if connecting to an existing node.
    :param enable_assertions: Enable assertions in Java for the new h2o server.
    :param max_mem_size: Maximum memory to use for the new h2o server. Integer input will be evaluated as gigabytes.  Other units can be specified by passing in a string (e.g. "160M" for 160 megabytes).
    :param min_mem_size: Minimum memory to use for the new h2o server. Integer input will be evaluated as gigabytes.  Other units can be specified by passing in a string (e.g. "160M" for 160 megabytes).
    :param strict_version_check: If True, an error will be raised if the client and server versions don't match.
    :param ignore_config: Indicates whether a processing of a .h2oconfig file should be conducted or not. Default value is False.
    :param extra_classpath: List of paths to libraries that should be included on the Java classpath when starting H2O from Python.
    :param kwargs: (all other deprecated attributes)
    :param jvm_custom_args: Customer, user-defined argument's for the JVM H2O is instantiated in. Ignored if there is an instance of H2O already running and the client connects to it.
    """
    global h2oconn
    assert_is_type(url, str, None)
    assert_is_type(ip, str, None)
    assert_is_type(port, int, str, None)
    assert_is_type(name, str, None)
    assert_is_type(https, bool, None)
    assert_is_type(insecure, bool, None)
    assert_is_type(username, str, None)
    assert_is_type(password, str, None)
    assert_is_type(cookies, str, [str], None)
    assert_is_type(proxy, {str: str}, None)
    assert_is_type(start_h2o, bool, None)
    assert_is_type(nthreads, int)
    assert_is_type(ice_root, str, None)
    assert_is_type(log_dir, str, None)
    assert_is_type(log_level, str, None)
    assert_satisfies(log_level, log_level in [None, "TRACE", "DEBUG", "INFO", "WARN", "ERRR", "FATA"])
    assert_is_type(enable_assertions, bool)
    assert_is_type(max_mem_size, int, str, None)
    assert_is_type(min_mem_size, int, str, None)
    assert_is_type(strict_version_check, bool, None)
    assert_is_type(extra_classpath, [str], None)
    assert_is_type(jvm_custom_args, [str], None)
    assert_is_type(bind_to_localhost, bool)
    assert_is_type(kwargs, {"proxies": {str: str}, "max_mem_size_GB": int, "min_mem_size_GB": int,
                            "force_connect": bool, "as_port": bool})

    def get_mem_size(mmint, mmgb):
        if not mmint:  # treat 0 and "" as if they were None
            if mmgb is None: return None
            return mmgb << 30
        if is_type(mmint, int):
            # If the user gives some small number just assume it's in Gigabytes...
            if mmint < 1000: return mmint << 30
            return mmint
        if is_type(mmint, str):
            last = mmint[-1].upper()
            num = mmint[:-1]
            if not (num.isdigit() and last in "MGT"):
                raise H2OValueError("Wrong format for a *_memory_size argument: %s (should be a number followed by "
                                    "a suffix 'M', 'G' or 'T')" % mmint)
            if last == "T": return int(num) << 40
            if last == "G": return int(num) << 30
            if last == "M": return int(num) << 20

    scheme = "https" if https else "http"
    proxy = proxy[scheme] if proxy is not None and scheme in proxy else \
        kwargs["proxies"][scheme] if "proxies" in kwargs and scheme in kwargs["proxies"] else None
    mmax = get_mem_size(max_mem_size, kwargs.get("max_mem_size_GB"))
    mmin = get_mem_size(min_mem_size, kwargs.get("min_mem_size_GB"))
    auth = (username, password) if username and password else None
    check_version = True
    verify_ssl_certificates = True

    # Apply the config file if ignore_config=False
    if not ignore_config:
        config = H2OConfigReader.get_config()
        if url is None and ip is None and port is None and https is None and "init.url" in config:
            url = config["init.url"]
        if proxy is None and "init.proxy" in config:
            proxy = config["init.proxy"]
        if cookies is None and "init.cookies" in config:
            cookies = config["init.cookies"].split(";")
        if auth is None and "init.username" in config and "init.password" in config:
            auth = (config["init.username"], config["init.password"])
        if strict_version_check is None:
            if "init.check_version" in config:
                check_version = config["init.check_version"].lower() != "false"
            elif os.environ.get("H2O_DISABLE_STRICT_VERSION_CHECK"):
                check_version = False
        else:
            check_version = strict_version_check
        if insecure is None:
            if "init.verify_ssl_certificates" in config:
                verify_ssl_certificates = config["init.verify_ssl_certificates"].lower() != "false"
            else:
                verify_ssl_certificates = not insecure

    if not start_h2o:
        print("Warning: if you don't want to start local H2O server, then use of `h2o.connect()` is preferred.")
    try:
        h2oconn = H2OConnection.open(url=url, ip=ip, port=port, name=name, https=https,
                                     verify_ssl_certificates=verify_ssl_certificates,
                                     auth=auth, proxy=proxy,cookies=cookies, verbose=True,
                                     _msgs=("Checking whether there is an H2O instance running at {url} ",
                                            "connected.", "not found."))
    except H2OConnectionError:
        # Backward compatibility: in init() port parameter really meant "baseport" when starting a local server...
        if port and not str(port).endswith("+") and not kwargs.get("as_port", False):
            port = str(port) + "+"
        if not start_h2o: raise
        if ip and not (ip == "localhost" or ip == "127.0.0.1"):
            raise H2OConnectionError('Can only start H2O launcher if IP address is localhost.')
        hs = H2OLocalServer.start(nthreads=nthreads, enable_assertions=enable_assertions, max_mem_size=mmax,
                                  min_mem_size=mmin, ice_root=ice_root, log_dir=log_dir, log_level=log_level,
                                  port=port, name=name,
                                  extra_classpath=extra_classpath, jvm_custom_args=jvm_custom_args,
                                  bind_to_localhost=bind_to_localhost)
        h2oconn = H2OConnection.open(server=hs, https=https, verify_ssl_certificates=not insecure,
                                     auth=auth, proxy=proxy,cookies=cookies, verbose=True)
    if check_version:
        version_check()
    h2oconn.cluster.timezone = "UTC"
    h2oconn.cluster.show_status()
Attempt to connect to a local server, or if not successful start a new server and connect to it.

    :param url: Full URL of the server to connect to (can be used instead of `ip` + `port` + `https`).
    :param ip: The ip address (or host name) of the server where H2O is running.
    :param port: Port number that H2O service is listening to.
    :param name: Cluster name. If None while connecting to an existing cluster it will not check the cluster name.
                 If set then will connect only if the target cluster name matches. If no instance is found and decides to start a local
                 one then this will be used as the cluster name or a random one will be generated if set to None.
    :param https: Set to True to connect via https:// instead of http://.
    :param insecure: When using https, setting this to True will disable SSL certificates verification.
    :param username: Username and
    :param password: Password for basic authentication.
    :param cookies: Cookie (or list of) to add to each request.
    :param proxy: Proxy server address.
    :param start_h2o: If False, do not attempt to start an h2o server when connection to an existing one failed.
    :param nthreads: "Number of threads" option when launching a new h2o server.
    :param ice_root: Directory for temporary files for the new h2o server.
    :param log_dir: Directory for H2O logs to be stored if a new instance is started. Ignored if connecting to an existing node.
    :param log_level: The logger level for H2O if a new instance is started. One of TRACE,DEBUG,INFO,WARN,ERRR,FATA. Default is INFO. Ignored if connecting to an existing node.
    :param enable_assertions: Enable assertions in Java for the new h2o server.
    :param max_mem_size: Maximum memory to use for the new h2o server. Integer input will be evaluated as gigabytes.  Other units can be specified by passing in a string (e.g. "160M" for 160 megabytes).
    :param min_mem_size: Minimum memory to use for the new h2o server. Integer input will be evaluated as gigabytes.  Other units can be specified by passing in a string (e.g. "160M" for 160 megabytes).
    :param strict_version_check: If True, an error will be raised if the client and server versions don't match.
    :param ignore_config: Indicates whether a processing of a .h2oconfig file should be conducted or not. Default value is False.
    :param extra_classpath: List of paths to libraries that should be included on the Java classpath when starting H2O from Python.
    :param kwargs: (all other deprecated attributes)
    :param jvm_custom_args: Customer, user-defined argument's for the JVM H2O is instantiated in. Ignored if there is an instance of H2O already running and the client connects to it.