def parse_n_jobs(s):
    """
    This function parses a "math"-like string as a function of CPU count.
    It is useful for specifying the number of jobs.

    For example, on an 8-core machine::

        assert parse_n_jobs('0.5 * n') == 4
        assert parse_n_jobs('2n') == 16
        assert parse_n_jobs('n') == 8
        assert parse_n_jobs('4') == 4

    :param str s: string to parse for number of CPUs
    """

    n_jobs = None
    N = cpu_count()

    if isinstance(s, int): n_jobs = s

    elif isinstance(s, float): n_jobs = int(s)

    elif isinstance(s, str):
        m = re.match(r'(\d*(?:\.\d*)?)?(\s*\*?\s*n)?$', s.strip())
        if m is None: raise ValueError('Unable to parse n_jobs="{}"'.format(s))

        k = float(m.group(1)) if m.group(1) else 1
        if m.group(2): n_jobs = k * N
        elif k < 1: n_jobs = k * N
        else: n_jobs = int(k)

    else: raise TypeError('n_jobs argument must be of type str, int, or float.')

    n_jobs = int(n_jobs)
    if n_jobs <= 0:
        warnings.warn('n_jobs={} is invalid. Setting n_jobs=1.'.format(n_jobs))
        n_jobs = 1
    #end if

    return int(n_jobs)
This function parses a "math"-like string as a function of CPU count.
    It is useful for specifying the number of jobs.

    For example, on an 8-core machine::

        assert parse_n_jobs('0.5 * n') == 4
        assert parse_n_jobs('2n') == 16
        assert parse_n_jobs('n') == 8
        assert parse_n_jobs('4') == 4

    :param str s: string to parse for number of CPUs