def get_word_set_total_score(board, word_set, num_move_locations):
    total_score = 0
    word_score = 0

    for word_location_set in word_set:
        word_score = 0
        word_multiplier = 1

        for location in word_location_set:
            square = board.board_square_dict[location]
            word_multiplier *= square.word_multiplier
            word_score += square.tile.point_value * square.letter_multiplier

        word_score *= word_multiplier
        total_score += word_score

    if num_move_locations == config.PLAYER_RACK_SIZE:
        total_score += config.BINGO_SCORE

    return total_score

get_word_set_total_score(board=  abcdefghijklmno1 _______________2 _______________3 _______________4 _______________5 _______________6 _______________7 _______________8 _______BAKER___9 _______________10_______________11_______________12_______________13_______________14_______________15_______________, word_set={frozenset(), frozenset({('h', 8), ('j', 8), ('l', 8), ('i', 8), ('k', 8)})}, num_move_locations=5)


def rolling_median_by_h(x, h, w, name):
    """Compute a rolling median of x, after first aggregating by h.

    Right-aligned. Computes a single median for each unique value of h. Each
    median is over at least w samples.

    For each h where there are fewer than w samples, we take samples from the previous h,
    moving backwards. (In other words, we ~ assume that the x's are shuffled within each h.)

    Parameters
    ----------
    x: Array.
    h: Array of horizon for each value in x.
    w: Integer window size (number of elements).
    name: Name for metric in result dataframe

    Returns
    -------
    Dataframe with columns horizon and name, the rolling median of x.
    """
    # Aggregate over h
    df = pd.DataFrame({'x': x, 'h': h})
    grouped = df.groupby('h')
    df2 = grouped.size().reset_index().sort_values('h')
    hs = df2['h']

    res_h = []
    res_x = []
    # Start from the right and work backwards
    i = len(hs) - 1
    while i >= 0:
        h_i = hs[i]
        xs = grouped.get_group(h_i).x.tolist()

        # wrap in array so this works if h is pandas Series with custom index or numpy array
        next_idx_to_add = np.array(h == h_i).argmax() - 1
        while (len(xs) < w) and (next_idx_to_add >= 0):
            # Include points from the previous horizon. All of them if still
            # less than w, otherwise just enough to get to w.
            xs.append(x[next_idx_to_add])
            next_idx_to_add -= 1
        if len(xs) < w:
            # Ran out of points before getting enough.
            break
        res_h.append(hs[i])
        res_x.append(np.median(xs))
        i -= 1
    res_h.reverse()
    res_x.reverse()
    return pd.DataFrame({'horizon': res_h, name: res_x})

rolling_median_by_h(x=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), h=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), w=1, name='x')

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7ff75ca12880>


def create_request_parameters(parent, request_model, params=None, index=None):
    """
    Handle request parameters that can be filled in from identifiers,
    resource data members or constants.

    By passing ``params``, you can invoke this method multiple times and
    build up a parameter dict over time, which is particularly useful
    for reverse JMESPath expressions that append to lists.

    :type parent: ServiceResource
    :param parent: The resource instance to which this action is attached.
    :type request_model: :py:class:`~boto3.resources.model.Request`
    :param request_model: The action request model.
    :type params: dict
    :param params: If set, then add to this existing dict. It is both
                   edited in-place and returned.
    :type index: int
    :param index: The position of an item within a list
    :rtype: dict
    :return: Pre-filled parameters to be sent to the request operation.
    """
    if params is None:
        params = {}

    for param in request_model.params:
        source = param.source
        target = param.target

        if source == 'identifier':
            # Resource identifier, e.g. queue.url
            value = getattr(parent, xform_name(param.name))
        elif source == 'data':
            # If this is a data member then it may incur a load
            # action before returning the value.
            value = get_data_member(parent, param.path)
        elif source in ['string', 'integer', 'boolean']:
            # These are hard-coded values in the definition
            value = param.value
        elif source == 'input':
            # This is provided by the user, so ignore it here
            continue
        else:
            raise NotImplementedError(f'Unsupported source type: {source}')

        build_param_structure(params, target, value, index)

    return params

create_request_parameters(parent=dynamodb.Table(name='MyTable'), request_model={_definition=OrderedDict([('operation', 'Scan'), ('params', [OrderedDict([('target', 'TableName'), ('source', 'identifier'), ('name', 'Name')])])]), operation='Scan'}, params=None, index=None)

{target='TableName', source='identifier', name='Name', path=None, value=None}


def get_mylogin_cnf_path():
    """Return the path to the login path file or None if it doesn't exist."""
    mylogin_cnf_path = os.getenv('MYSQL_TEST_LOGIN_FILE')

    if mylogin_cnf_path is None:
        app_data = os.getenv('APPDATA')
        default_dir = os.path.join(app_data, 'MySQL') if app_data else '~'
        mylogin_cnf_path = os.path.join(default_dir, '.mylogin.cnf')

    mylogin_cnf_path = os.path.expanduser(mylogin_cnf_path)

    if exists(mylogin_cnf_path):
        logger.debug("Found login path file at '{0}'".format(mylogin_cnf_path))
        return mylogin_cnf_path
    return None

get_mylogin_cnf_path()


def _check(self) -> None:
        """Check the syntax of the expression.

        :raises ValueError:
            If the expression is incomplete.  This error is aimed at debugging
            the expression so it is very verbose.
        """
        stack_size = 0
        for i, token_ in enumerate(self._tokens):
            stack_size -= token_.pops
            if stack_size < 0:
                raise ValueError(
                    self._format_syntax_error(
                        f"'{token_}' takes {token_.pops} argument(s) but the stack"
                        f" will only have {stack_size + token_.pops} element(s)",
                        i,
                    )
                )
            stack_size += token_.puts
        if stack_size == 0:
            raise ValueError(
                self._format_syntax_error("expression does not produce a result")
            )
        if stack_size > 1:
            raise ValueError(
                self._format_syntax_error(
                    f"expression produces too many results ({stack_size}), "
                    "expected 1"
                )
            )

_check(self=CompleteExpression([Literal(1)]), self[0]=Literal(1))


def contains_sublist(list_: List[Any], sublist: List[Any]) -> bool:
    """Determine if a `list` contains a `sublist`.

    :param list_:
        list to search for the `sublist` in.
    :param sublist:
        Sub list to search for.

    :return:
        True if `list` contains `sublist`.

    """
    # Adapted from: https://stackoverflow.com/a/12576755
    if not sublist:
        return False
    for i in range(len(list_)):
        if list_[i] == sublist[0] and list_[i : i + len(sublist)] == sublist:
            return True
    return False

contains_sublist(list_=[1, 2, 3, 4], sublist=[1, 2])


def delete_sublist(list_: List[Any], sublist: List[Any]) -> List[Any]:
    """Remove a `sublist` from the given `list_`.

    :param list_:
        List to remove the `sublist` from.
    :param sublist:
        Sublist to remove from `list_`.

    :return:
        A copy of `list_` with the `sublist` removed.
    """
    if not sublist:
        return list_[:]
    for i in range(len(list_)):
        if list_[i] == sublist[0] and list_[i : i + len(sublist)] == sublist:
            return list_[:i] + list_[i + len(sublist) :]
    return list_[:]

delete_sublist(list_=[1, 2, 3, 4], sublist=[1, 2])


def bounded_product(sequence, n=None, seed=None):
    """
    Returns a shuffled, bounded cartesian product of the input sequence.
    Designed to cover as wide a range of permutations as possible with a limited number of iterations.
    Will manifest the whole list in memory, so not suitable for super large sequences.

    :param sequence: iterable
    :param n: length of returned list
    :param seed: random seed for reproducibility
    :return: list
    """
    p = list(itertools.product(*sequence))
    if seed is not None:
        random.seed(seed)
    random.shuffle(p)
    return p if n is None else p[:n]

bounded_product(sequence=([[0, 1, 1], [1, 2, 2], [0, 2, 2]], [True, False], [[[['global'], 'all']], [[['local'], 'all']], [[['sparse_variable'], 'all']], [[['sparse_fixed'], 'all']]], [[True, False], [False, True]], [[{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]]), n=50, seed=None)

[([0, 1, 1], True, [[['global'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['global'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], True, [[['global'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['global'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], True, [[['local'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['local'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], True, [[['local'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['local'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], True, [[['sparse_variable'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['sparse_variable'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], True, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], True, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], True, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], False, [[['global'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], False, [[['global'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], False, [[['global'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], False, [[['global'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], False, [[['local'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], False, [[['local'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], False, [[['local'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis... True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], True, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], True, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], True, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], True, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], True, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], True, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['global'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['global'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['global'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['global'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['local'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['local'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['local'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['local'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['sparse_variable'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['sparse_variable'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False])]

[([0, 1, 1], False, [[['sparse_variable'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], False, [[['local'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([1, 2, 2], True, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([1, 2, 2], True, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([1, 2, 2], True, [[['local'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], True, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], True, [[['sparse_variable'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], True, [[['local'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([1, 2, 2], True, [[['global'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([1, 2, 2], False, [[['global'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['global'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], True, [[['global'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['global'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], False, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], True, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], False, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([1, 2, 2], False, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([1, 2, 2], True, [[['local'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['local'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['sparse_variable'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([1, 2, 2], False, [[['local'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, '...abled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([1, 2, 2], False, [[['local'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['local'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([1, 2, 2], True, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([1, 2, 2], True, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], True, [[['local'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([1, 2, 2], False, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([1, 2, 2], True, [[['local'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([1, 2, 2], True, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['global'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], True, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['sparse_variable'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], True, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['global'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], False, [[['global'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([1, 2, 2], False, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['sparse_variable'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['sparse_fixed'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 2, 2], False, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True]), ([0, 1, 1], True, [[['sparse_variable'], 'all']], [True, False], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 1, 1], True, [[['sparse_fixed'], 'all']], [False, True], [{'enabled': True, 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, False]), ([0, 2, 2], False, [[['local'], 'all']], [True, False], [{'enabled': True, 'type': 'bfloat16', 'loss_scale': 0, 'loss_scale_window': 1000, 'hysteresis': 2, 'min_loss_scale': 1}, True])]


def set_up_autotuning(encoded_config, overwrite_values):
        config = json.loads(base64.urlsafe_b64decode(encoded_config).decode("utf-8"))
        overwrite_values = overwrite_values if overwrite_values else {}
        for tuning_param in AUTOTUNING_ARGS:
            # TODO: This is for autotuning specifically, may cause surprises for someone with a weird setup
            if tuning_param in config:
                overwrite_values[tuning_param] = config[tuning_param]
        return overwrite_values

set_up_autotuning(encoded_config='eyJ0cmFpbl9iYXRjaF9zaXplIjogNCwgInRyYWluX21pY3JvX2JhdGNoX3NpemVfcGVyX2dwdSI6IDQsICJvcHRpbWl6ZXIiOiB7InR5cGUiOiAic20zIiwgInBhcmFtcyI6IHt9fSwgImZwMTYiOiB7InR5cGUiOiAiZnAxNiIsICJlbmFibGVkIjogdHJ1ZX0sICJ6ZXJvX29wdGltaXphdGlvbiI6IHsic3RhZ2UiOiAwLCAiYWxsZ2F0aGVyX3BhcnRpdGlvbnMiOiB0cnVlLCAicmVkdWNlX3NjYXR0ZXIiOiB0cnVlLCAiYWxsZ2F0aGVyX2J1Y2tldF9zaXplIjogNTAwMDAwMDAwLCAib3ZlcmxhcF9jb21tIjogZmFsc2UsICJyZWR1Y2VfYnVja2V0X3NpemUiOiA1MDAwMDAwMDAsICJjb250aWd1b3VzX2dyYWRpZW50cyI6IGZhbHNlfSwgIndhbGxfY2xvY2tfYnJlYWtkb3duIjogdHJ1ZSwgImNvbW1zX2xvZ2dlciI6IHsiZW5hYmxlZCI6IHRydWUsICJ2ZXJib3NlIjogdHJ1ZSwgInByb2ZfYWxsIjogdHJ1ZSwgImRlYnVnIjogZmFsc2V9fQ==', overwrite_values={'train_iters': 32})

{'train_batch_size': 4, 'train_micro_batch_size_per_gpu': 4, 'optimizer': {'type': 'sm3', 'params': {}}, 'fp16': {'type': 'fp16', 'enabled': True}, 'zero_optimization': {'stage': 0, 'allgather_partitions': True, 'reduce_scatter': True, 'allgather_bucket_size': 500000000, 'overlap_comm': False, 'reduce_bucket_size': 500000000, 'contiguous_gradients': False}, 'wall_clock_breakdown': True, 'comms_logger': {'enabled': True, 'verbose': True, 'prof_all': True, 'debug': False}}

{'train_iters': 32, 'train_batch_size': 4}

{'train_iters': 32, 'train_batch_size': 4, 'train_micro_batch_size_per_gpu': 4}

{'train_iters': 32, 'train_batch_size': 4, 'train_micro_batch_size_per_gpu': 4, 'zero_optimization': {'stage': 0, 'allgather_partitions': True, 'reduce_scatter': True, 'allgather_bucket_size': 500000000, 'overlap_comm': False, 'reduce_bucket_size': 500000000, 'contiguous_gradients': False}}


def test_model_training_options(monkeypatch, key, value):
    # TODO: Possibly add testing over world_size=2 back in
    neox_args = NeoXArgs.from_dict(BASE_CONFIG)
    if getattr(neox_args, key) == value:
        pytest.skip("Skipping to avoid redundancy as no change in base config")
    if key == "precision" and value == "bfloat16":
        pytest.xfail(
            reason="Assumes that ZeRO optimization stage has been set in the YAML"
        )
    param_dict = {key: value}
    run_train_test(monkeypatch, overwrite_values=param_dict)

test_model_training_options(monkeypatch={_setattr=[], _setitem=[], _cwd=None, _savesyspath=None}, key='gpt_j_residual', value=False)

NeoXArgs(distributed_backend='nccl', local_rank=None, rank=None, lazy_mpu_init=False, short_seq_prob=0.1, eod_mask_loss=False, adlr_autoresume=False, adlr_autoresume_interval=1000, seed=1234, onnx_safe=False, deepscale=False, deepscale_config=None, deepspeed_mpi=False, deepspeed_slurm=False, user_script=None, iteration=None, do_train=None, do_valid=None, do_test=None, save_iters=[], global_num_gpus=1, text_gen_type='unconditional', temperature=0.0, top_p=0.0, top_k=0, return_logits=False, maximum_tokens=64, prompt_end='\n', sample_input_file=None, sample_output_file='samples.txt', num_samples=1, recompute=False, eval_results_prefix='', eval_tasks=None, use_wandb=False, wandb_group=None, wandb_team=None, wandb_project='neox', wandb_host='https://api.wandb.ai', wandb_init_all_ranks=False, git_hash='7a8fa2f0', log_dir=None, tensorboard_dir=None, log_interval=10, log_grad_pct_zeros=False, log_param_norm=False, log_grad_norm=False, log_optimizer_states=False, log_gradient_noise_scale=False, gradient_noise_scale_n_batches=5, gradient_noise_scale_cpu_offload=False, pipe_parallel_size=1, model_parallel_size=1, pipe_partition_method='type:transformer|mlp', world_size=None, is_pipe_parallel=True, data_path='data/enwik8/enwik8_text_document', use_shared_fs=True, train_data_paths=None, label_data_paths=None, test_data_paths=None, valid_data_paths=None, train_data_weights=None, valid_data_weights=None, test_data_weights=None, weight_by_num_documents=False, weighted_sampler_alpha=1.0, data_impl='mmap', mmap_warmup=False, save=None, s3_path=None, s3_chunk_size=104857600, config_files=None, load=None, checkpoint_validation_with_forward_pass=False, checkpoint_scale='linear', checkpoint_factor=1000, extra_save_iters=None, no_save_optim=False, no_save_rng=False, no_load_optim=False, no_load_rng=False, finetune=False, batch_size=4, train_iters=20, eval_iters=10, keep_last_n_checkpoints=None, eval_interval=100000, split='969, 30, 1', vocab_file='data/gpt2-vocab.json', merge_file='data/gpt2-merges.txt', num_workers=1, exit_interval=None, attention_dropout=0, hidden_dropout=0, weight_decay=0.1, checkpoint_activations=True, checkpoint_num_layers=1, deepspeed_activation_checkpointing=True, contiguous_checkpointing=False, checkpoint_in_cpu=False, synchronize_each_layer=True, profile_backward=False, partition_activations=True, gas=1, clip_grad=1.0, hysteresis=2, dynamic_loss_scale=True, loss_scale=None, loss_scale_window=1000.0, min_scale=1.0, char_level_ppl=False, use_mup=False, coord_check=False, save_base_shapes=False, base_shapes_file=None, mup_init_scale=1.0, mup_attn_temp=1.0, mup_output_temp=1.0, mup_embedding_mult=1.0, mup_rp_embedding_mult=1.0, mup_width_scale=2, tokenizer_type='GPT2BPETokenizer', padded_vocab_size=None, optimizer_type='sm3', use_bnb_optimizer=False, zero_stage=0, zero_reduce_scatter=True, zero_contiguous_gradients=False, zero_reduce_bucket_size=500000000, zero_allgather_bucket_size=500000000, lr=0.001, lr_decay_style='cosine', lr_decay_iters=20, min_lr=0.0, warmup=0.01, override_lr_scheduler=False, use_checkpoint_lr_scheduler=False, precision='fp16', num_layers=2, hidden_size=8, num_attention_heads=4, seq_length=1024, max_position_embeddings=1024, norm='layernorm', use_qk_layernorm=False, layernorm_epsilon=1e-05, rms_norm_epsilon=1e-08, scalenorm_epsilon=1e-08, pos_emb='rotary', rpe_num_buckets=32, rpe_max_distance=128, opt_pos_emb_offset=0, no_weight_tying=True, attention_config=['global', 'global'], sparsity_config={}, num_unique_layers=None, param_sharing_style='grouped', make_vocab_size_divisible_by=128, activation='gelu', scaled_upper_triang_masked_softmax_fusion=False, scaled_masked_softmax_fusion=False, bias_gelu_fusion=False, bias_dropout_fusion=False, rope_fusion=False, fp16_lm_cross_entropy=False, init_method_std=0.02, apply_query_key_layer_scaling=False, use_cpu_initialization=False, attention_softmax_in_fp32=False, rotary_pct=1.0, rotary_emb_base=10000, init_method='small_init', output_layer_init_method='wang_init', gmlp_attn_dim=64, gpt_j_residual=False, gpt_j_tied=False, use_bias_in_norms=True, use_bias_in_attn_linear=True, mlp_type='regular', soft_prompt_tuning=None, output_layer_parallelism='column', deepspeed=True, train_batch_size=4, train_micro_batch_size_per_gpu=4, gradient_accumulation_steps=1, optimizer={'type': 'sm3', 'params': {}}, scheduler=None, fp32_allreduce=False, prescale_gradients=False, gradient_predivide_factor=1.0, sparse_gradients=False, fp16={'type': 'fp16', 'enabled': True}, bf16=None, amp=None, gradient_clipping=1.0, zero_optimization={'stage': 0, 'allgather_partitions': True, 'reduce_scatter': True, 'allgather_bucket_size': 500000000, 'overlap_comm': False, 'reduce_bucket_size': 500000000, 'contiguous_gradients': False}, curriculum_learning=None, curriculum_seqlen=0, steps_per_print=10, wall_clock_breakdown=True, dump_state=False, flops_profiler=None, communication_data_type=None, autotuning=None, activation_checkpointing=None, sparse_attention=None, data_efficiency=None, tensorboard=None, wandb=None, csv_monitor=None, elasticity=None, comms_logger=None, compression_training=None, checkpoint=None, data_types=None, deepspeed_extra_args={'comms_logger': {'enabled': True, 'verbose': True, 'prof_all': True, 'debug': False}}, hostfile='None', include='localhost:1', exclude=None, num_nodes=-1, num_gpus=None, master_port=29500, master_addr=None, launcher='pdsh', force_multi=False, detect_nvlink_pairs=False, autotuning_run=None, no_ssh_check=False, comment=None, account=None)


def _get_nested(data, key):
    """
    Return value for a hierrachical key (like a.b.c).
    Return None if nothing found.
    If there is a key with . in the name, and a subdictionary,
    the former is preferred:

    >>> print(_get_nested({'a.b': 10, 'a':{'b': 20}}, 'a.b'))
    10
    >>> print(_get_nested({'a': {'b': 20}}, 'a.b'))
    20
    >>> print(_get_nested({'a': {'b': {'c': 30}}}, 'a.b.c'))
    30
    """

    if not data or not isinstance(data, dict):
        return None
    if '.' not in key:
        return data.get(key)
    if key in data:
        return data[key]

    parts = key.split('.')
    for i in range(len(parts))[::-1]:
        prefix = ".".join(parts[:i])
        if prefix in data:
            return _get_nested(data[prefix], ".".join(parts[i:]))

    return None

_get_nested(data={'server': {'address': '0.0.0.0'}, 'cache': {'type': 'redis'}}, key='adapters.active')


def sample(logits: torch.Tensor, temperature: float = 1.0, top_k: Optional[int] = None) -> torch.Tensor:
    logits = logits[0, -1]
    # optionally crop the logits to only the top k options
    if top_k is not None:
        v, i = torch.topk(logits, min(top_k, logits.size(-1)))
        # do not use `torch.where` as in nanogpt because it will repeat top-k collisions
        logits = torch.full_like(logits, float("-inf")).scatter_(-1, i, v)
    # optionally scale the logits and sample from a probability distribution
    if temperature > 0.0:
        probs = torch.nn.functional.softmax(logits / temperature, dim=-1)
        return multinomial_num_samples_1(probs)
    return torch.argmax(logits, dim=-1, keepdim=True)

sample(logits=tensor([[[ 0.2595,  0.0058,  0.6498, -0.5155,  0.0366, -0.2944,  0.0555,          -0.4013],         [ 0.5342, -0.9790, -0.0861,  0.2530,  0.1378, -0.4913,  0.1915,          -0.3248],         [ 0.5289, -0.3149,  0.4052,  0.2107, -0.8788, -0.9871,  0.6539,          -0.2213]]], device='cuda:0'), temperature=0.8, top_k=None)


def test_generate(monkeypatch, generated, stop_tokens, expected):
    import chat.base as chat
    import generate.base as generate

    input_idx = torch.tensor([5, 3])
    max_returned_tokens = len(input_idx) + 8
    model = MagicMock()
    model.config.block_size = 100
    model.max_seq_length = 100
    it = iter(generated)

    def multinomial(*_, **__):
        out = next(it)
        return torch.tensor([out])

    monkeypatch.setattr(generate, "multinomial_num_samples_1", multinomial)
    actual = chat.generate(model, input_idx, max_returned_tokens, stop_tokens=stop_tokens)
    actual = list(actual)

    assert len(actual) == len(expected)
    if not actual:
        assert actual == expected
    else:
        for t in actual:
            assert t.dtype == torch.long
        assert torch.cat(actual).tolist() == expected

test_generate(monkeypatch={_setattr=[], _setitem=[], _cwd=None, _savesyspath=None}, generated=repeat(1), stop_tokens=(), expected=[1, 1, 1, 1, 1, 1, 1, 1])

{_setattr=[(<module 'generate.base' from '/local/rcs/jinjun/code/pytrace-collector/logs/self_collected/tried/Lightning-AI+lit-gpt/Lightning-AI+lit-gpt/generate/base.py'>, 'multinomial_num_samples_1', <function multinomial_num_samples_1 at 0x7f805238a160>)], _setitem=[], _cwd=None, _savesyspath=None}

<function test_generate.<locals>.multinomial at 0x7f7fd0f7e430>

<generator object generate at 0x7f803c55ff90>


def render_pep440_feature(pieces):
    """Build up version string, used within "feature" branch of repository.

    Our goal: MERGE-POINT.post.devN+gHEX.BRANCH-NAME.M[.dirty]
        +) MERGE-POINT = Most recent common ancestor for `develop` and `master`
          *) Does not yet handle branch from `release-*`
        +) N = DISTANCE from the MERGE-POINT of `develop` and `master`
        +) M = DISTANCE from the MERGE-POINT of "feature" and `develop`

    Exceptions:
    1: no tags. 0.post.devDISTANCE+gHEX[.dirty]
    """
    if pieces["closest-tag"] and pieces["develop"]:
        rendered = pieces["closest-tag"]
        distance_to_develop = pieces["distance-to-develop"]
        distance_to_merge = pieces["distance-to-master"]
        distance_merge_to_tag = (pieces["distance"] - distance_to_merge)
        distance_dev_to_merge = (distance_to_merge - distance_to_develop)
        if (distance_merge_to_tag > 0):
            rendered += ".%d" % distance_merge_to_tag
        rendered += ".post.dev%d" % distance_dev_to_merge
        rendered += plus_or_dot(pieces)
        rendered += "g%s" % pieces["short"]
        rendered += ".%s" % pieces["branch"]
        rendered += ".%d" % distance_to_develop
    else:
        # exception #1
        rendered = "0.post.dev%d" % (pieces["distance"] - 1)
        rendered += plus_or_dot(pieces)
        rendered += "g%s" % pieces["short"]
    if pieces["dirty"]:
        rendered += ".dirty"
    return rendered

render_pep440_feature(pieces={'long': 'e60d005d389cb31b6e99f937e35adbe3fccb7aaf', 'short': 'e60d005', 'error': None, 'dirty': False, 'closest-tag': '0.8', 'distance': 3, 'branch': 'HEAD', 'distance-to-master': 0, 'develop': None, 'distance-to-develop': None, 'date': '2018-05-05T22:17:58-0700', 'authors': ['Padraic Shafer']})


def encode_number(n):
    b128_digits = []
    while n:
        b128_digits.insert(0, (n & 0x7f) | 0x80)
        n = n >> 7
    if not b128_digits:
        b128_digits.append(0)
    b128_digits[-1] &= 0x7f
    return b''.join([int.to_bytes(d, 1, 'big') for d in b128_digits])

encode_number(n=840)


def _import_plugins(plugin_names):
    plugins = []
    for name, path in plugin_names.items():
        log.debug("Importing plugin %s: %s", name, path)
        if '.' not in path:
            plugins.append(__import__(path))
        else:
            package, module = path.rsplit('.', 1)
            module = __import__(path, fromlist=[module])
            plugins.append(module)
    return plugins

_import_plugins(plugin_names={'__builtin__': 'awscli.handlers'})


def _make_stage(self, planes: int, num_blocks: int, num_se_blocks: int) -> nn.Sequential:
        """Build a stage of MobileOne model.

        :param planes: Number of output channels.
        :param num_blocks: Number of blocks in this stage.
        :param num_se_blocks: Number of SE blocks in this stage.
        :return: A stage of MobileOne model.
        """
        # Get strides for all layers
        strides = [2] + [1] * (num_blocks - 1)
        blocks = []
        for ix, stride in enumerate(strides):
            use_se = False
            if num_se_blocks > num_blocks:
                raise ValueError("Number of SE blocks cannot " "exceed number of layers.")
            if ix >= (num_blocks - num_se_blocks):
                use_se = True

            # Depthwise conv
            blocks.append(
                MobileOneBlock(
                    in_channels=self.in_planes,
                    out_channels=self.in_planes,
                    kernel_size=3,
                    stride=stride,
                    padding=1,
                    groups=self.in_planes,
                    inference_mode=self.inference_mode,
                    use_se=use_se,
                    num_conv_branches=self.num_conv_branches,
                )
            )
            # Pointwise conv
            blocks.append(
                MobileOneBlock(
                    in_channels=self.in_planes,
                    out_channels=planes,
                    kernel_size=1,
                    stride=1,
                    padding=0,
                    groups=1,
                    inference_mode=self.inference_mode,
                    use_se=use_se,
                    num_conv_branches=self.num_conv_branches,
                )
            )
            self.in_planes = planes
            self.cur_layer_idx += 1
        return nn.Sequential(*blocks)

_make_stage(self=MobileOne(  (stage0): MobileOneBlock(    (se): Identity()    (activation): ReLU()    (rbr_conv): ModuleList(      (0): Sequential(        (conv): Conv2d(3, 48, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)        (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)      )    )    (rbr_scale): Sequential(      (conv): Conv2d(3, 48, kernel_size=(1, 1), stride=(2, 2), bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  )), planes=48, num_blocks=2, num_se_blocks=0, self._backward_hooks=OrderedDict(), self._backward_pre_hooks=OrderedDict(), self._buffers=OrderedDict(), self._depth=3, self._forward_hooks=OrderedDict(), self._forward_hooks_always_called=OrderedDict(), self._forward_hooks_with_kwargs=OrderedDict(), self._forward_pre_hooks=OrderedDict(), self._forward_pre_hooks_with_kwargs=OrderedDict(), self._in_channels=3, self._is_full_backward_hook=None, self._load_state_dict_post_hooks=OrderedDict(), self._load_state_dict_pre_hooks=OrderedDict(), self._modules=OrderedDict([('stage0', MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_conv): ModuleList(    (0): Sequential(      (conv): Conv2d(3, 48, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  )  (rbr_scale): Sequential(    (conv): Conv2d(3, 48, kernel_size=(1, 1), stride=(2, 2), bias=False)    (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  )))]), self._non_persistent_buffers_set=set(), self._out_channels=(3, 48, 48, 128, 256, 1024), self._parameters=OrderedDict(), self._state_dict_hooks=OrderedDict(), self._state_dict_pre_hooks=OrderedDict(), self.cur_layer_idx=1, self.in_planes=48, self.inference_mode=False, self.num_conv_branches=4, self.training=True, self.use_se=False)

[MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_conv): ModuleList(    (0-3): 4 x Sequential(      (conv): Conv2d(48, 48, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=48, bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  )  (rbr_scale): Sequential(    (conv): Conv2d(48, 48, kernel_size=(1, 1), stride=(2, 2), groups=48, bias=False)    (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  ))]

[MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_conv): ModuleList(    (0-3): 4 x Sequential(      (conv): Conv2d(48, 48, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=48, bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  )  (rbr_scale): Sequential(    (conv): Conv2d(48, 48, kernel_size=(1, 1), stride=(2, 2), groups=48, bias=False)    (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  )), MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_skip): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  (rbr_conv): ModuleList(    (0-3): 4 x Sequential(      (conv): Conv2d(48, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  ))]

[MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_conv): ModuleList(    (0-3): 4 x Sequential(      (conv): Conv2d(48, 48, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=48, bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  )  (rbr_scale): Sequential(    (conv): Conv2d(48, 48, kernel_size=(1, 1), stride=(2, 2), groups=48, bias=False)    (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  )), MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_skip): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  (rbr_conv): ModuleList(    (0-3): 4 x Sequential(      (conv): Conv2d(48, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  )), MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_skip): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  (rbr_conv): ModuleList(    (0-3): 4 x Sequential(      (conv): Conv2d(48, 48, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=48, bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  )  (rbr_scale): Sequential(    (conv): Conv2d(48, 48, kernel_size=(1, 1), stride=(1, 1), groups=48, bias=False)    (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  ))]

[MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_conv): ModuleList(    (0-3): 4 x Sequential(      (conv): Conv2d(48, 48, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=48, bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  )  (rbr_scale): Sequential(    (conv): Conv2d(48, 48, kernel_size=(1, 1), stride=(2, 2), groups=48, bias=False)    (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  )), MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_skip): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  (rbr_conv): ModuleList(    (0-3): 4 x Sequential(      (conv): Conv2d(48, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  )), MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_skip): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  (rbr_conv): ModuleList(    (0-3): 4 x Sequential(      (conv): Conv2d(48, 48, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=48, bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  )  (rbr_scale): Sequential(    (conv): Conv2d(48, 48, kernel_size=(1, 1), stride=(1, 1), groups=48, bias=False)    (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  )), MobileOneBlock(  (se): Identity()  (activation): ReLU()  (rbr_skip): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)  (rbr_conv): ModuleList(    (0-3): 4 x Sequential(      (conv): Conv2d(48, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)      (bn): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)    )  ))]


def __setitem__(self, key, value):
        # type: (_K, _V) -> None
        """Store a new views, potentially discarding an old value.
        """
        if key not in self:
            if len(self) >= self.cache_size:
                self.popitem(last=False)
        OrderedDict.__setitem__(self, key, value)

__setitem__(self=LRUCache(), key='foo', value=1)


def config(_config=None, **kwargs):
    """
    A decorator for setting the default kwargs of `BaseHandler.crawl`.
    Any self.crawl with this callback will use this config.
    """
    if _config is None:
        _config = {}
    _config.update(kwargs)

    def wrapper(func):
        func._config = _config
        return func
    return wrapper

config(_config=None, kwargs={'age': 864000})

<function config.<locals>.wrapper at 0x7f6a10388ee0>


def base64url_decode(input: Union[bytes, str]) -> bytes:
    input_bytes = force_bytes(input)

    rem = len(input_bytes) % 4

    if rem > 0:
        input_bytes += b"=" * (4 - rem)

    return base64.urlsafe_b64decode(input_bytes)

base64url_decode(input='hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg')


def to_dict(self, default=None):
        """
        Converts sequence of (Key, Value) pairs to a dictionary.

        >>> type(seq([('a', 1)]).to_dict())
        dict

        >>> seq([('a', 1), ('b', 2)]).to_dict()
        {'a': 1, 'b': 2}

        :param default: Can be a callable zero argument function. When not None, the returned
            dictionary is a collections.defaultdict with default as value for missing keys. If the
            value is not callable, then a zero argument lambda function is created returning the
            value and used for collections.defaultdict
        :return: dictionary from sequence of (Key, Value) elements
        """
        dictionary = {}
        for e in self.sequence:
            dictionary[e[0]] = e[1]
        if default is None:
            return dictionary
        else:
            if hasattr(default, '__call__'):
                return collections.defaultdict(default, dictionary)
            else:
                return collections.defaultdict(lambda: default, dictionary)

to_dict(self=[(1, 2), (2, 10), (7, 2)], default=None, self._base_sequence=[(1, 2), (2, 10), (7, 2)], self._lineage=Lineage: sequence)


def sqlite3(conn, sql, parameters=None, *args, **kwargs):
    """
    Additional entry point to Sequence which query data from sqlite db file.

    >>> seq.sqlite3('examples/users.db', 'select id, name from users where id = 1;').first()
    [(1, "Tom")]

    :param conn: path or sqlite connection, cursor
    :param sql: SQL query string
    :return: Sequence wrapping SQL cursor
    """

    if parameters is None:
        parameters = ()

    if isinstance(conn, (sqlite3api.Connection, sqlite3api.Cursor)):
        return seq(conn.execute(sql, parameters))
    elif isinstance(conn, str):
        with sqlite3api.connect(conn, *args, **kwargs) as input_conn:
            return seq(input_conn.execute(sql, parameters))
    else:
        raise ValueError('conn must be a must be a file path or sqlite3 Connection/Cursor')

sqlite3(conn=1, sql='SELECT * from user', parameters=None, args=(), kwargs={})


def secrets_dir(env=os.getenv('D2_ENVIRONMENT', None),
                basedir=os.getenv('D2_SECRETS_BASEDIR', None)):
    if env is not None:
        env_str = str(env)
    else:
        cwd = os.getcwd()
        default_file = os.path.join(cwd, '.python_secrets_environment')
        if os.path.exists(default_file):
            with open(default_file, 'r') as f:
                env_str = f.read().strip()
        else:
            env_str = os.path.basename(cwd)
    if basedir is None:
        basedir = os.path.join(
                HOME,
                'secrets' if sys.platform.startswith('win') else '.secrets')
    return os.path.join(basedir, env_str)

secrets_dir(env=None, basedir=None)


def _identify_environment(environment=None):
    """
    Returns the environment identifier.

    There are multiple ways to define the default environment (in order
    of priority):

    1. The --environment command line option;
    2. The content of the file .python_secrets_environment in the current
       working directory;
    3. The value specified by environment variable D2_ENVIRONMENT; or
    4. The basename of the current working directory.
    """
    cwd = os.getcwd()
    if environment is None:
        env_file = os.path.join(cwd, '.python_secrets_environment')
        if os.path.exists(env_file):
            with open(env_file, 'r') as f:
                environment = f.read().replace('\n', '')
        else:
            environment = os.getenv('D2_ENVIRONMENT',
                                    os.path.basename(cwd))
    return environment

_identify_environment(environment=None)


def parse_route_template(template):
    rbuilder = ["^"]
    fbuilder = []
    position = 0
    schema = {}

    for match in template_var_re_finditer(template):
        param_name = match.group("name")
        param_type = match.group("type") or "id"
        # TODO: Handle KeyError, maybe we want to use a custom error here.
        param_formatchar, param_re, param_schema = _schema_map[param_type]
        schema[param_name] = param_schema

        rbuilder.append(re.escape(template[position:match.start()]))
        rbuilder.append(param_re.format(param_name))

        fbuilder.append(template[position:match.start()])
        fbuilder.append("{")
        fbuilder.append(param_name)
        fbuilder.append(":")
        fbuilder.append(param_formatchar)
        fbuilder.append("}")

        position = match.end()

    rbuilder.append(re.escape(template[position:]))
    rbuilder.append("$")
    fbuilder.append(template[position:])

    return (valid.Schema(schema),
            re.compile("".join(rbuilder)),
            u"".join(fbuilder).format)

parse_route_template(template='get/{variable}')

{'variable': And(<class 'str'>, <function Match at 0x7f0c58d64940>)}

<re.Match object; span=(4, 14), match='{variable}'>


def get_generation(ba_name, **kwargs):
    # get data
    c = client_factory(ba_name)
    data = c.get_generation(**kwargs)

    # log
    if len(data) == 0:
        msg = '%s: No generation data at %s with args %s' % (ba_name, datetime.utcnow().isoformat(),
                                                    kwargs)
        logger.warn(msg)

    # return
    return data

get_generation(ba_name='CAISO', kwargs={'latest': True})

{options={'data': 'gen', 'latest': True, 'yesterday': False, 'start_at': False, 'end_at': False, 'sliceable': False, 'forecast': False, 'market': 'RT5M', 'freq': '10m'}, NAME='CAISO', session=<requests.sessions.Session object at 0x7fcc37a98b50>}

"CAISO: No generation data at 2024-04-03T22:46:21.338362 with args {'latest': True}"


def get_load(ba_name, **kwargs):
    # get data
    c = client_factory(ba_name)
    data = c.get_load(**kwargs)

    # log
    if len(data) == 0:
        msg = '%s: No load data at %s with args %s' % (ba_name, datetime.utcnow().isoformat(),
                                                    kwargs)
        logger.warn(msg)

    # return
    return data

get_load(ba_name='PJM', kwargs={'latest': True})

{options={'data': 'load', 'latest': True, 'start_at': None, 'end_at': None, 'forecast': False, 'sliceable': False}, NAME='PJM', session=<requests.sessions.Session object at 0x7fcc37745f10>}

"PJM: No load data at 2024-04-03T22:47:45.327834 with args {'latest': True}"


def format_decimal(self, altitude=None):
        """
        Format decimal degrees with altitude::

            >>> p = Point(41.5, -81.0, 12.3)
            >>> p.format_decimal()
            '41.5, -81.0, 12.3km'
            >>> p = Point(41.5, 0, 0)
            >>> p.format_decimal()
            '41.5, 0.0'

        :param bool altitude: Whether to include ``altitude`` value.
            By default it is automatically included if it is non-zero.
        """
        coordinates = [str(self.latitude), str(self.longitude)]

        if altitude is None:
            altitude = bool(self.altitude)
        if altitude:
            if not isinstance(altitude, str):
                altitude = 'km'
            coordinates.append(self.format_altitude(altitude))

        return ", ".join(coordinates)

format_decimal(self=Point(41.5, 81.0, 2.5), altitude=None, self.altitude=2.5, self.latitude=41.5, self.longitude=81.0)


def _global_import(name):
    p = __import__(name, globals(), locals(), level=1)
    lst = p.__all__ if '__all__' in dir(p) else dir(p)
    if lst:
        globals().pop(name, None)
        for k in lst:
            if not k.startswith('__'):
                globals()[k] = p.__dict__[k]
                __all__.append(k)

_global_import(name='base')

['DataFlow', 'ProxyDataFlow', 'RNGDataFlow', 'DataFlowTerminated']


def global_import(name):
    p = __import__(name, globals(), locals(), level=1)
    lst = p.__all__ if '__all__' in dir(p) else []
    del globals()[name]
    for k in lst:
        if not k.startswith('__'):
            globals()[k] = p.__dict__[k]
            __all__.append(k)

global_import(name='model_desc')


def xldate_as_tuple(xldate, datemode):
    if datemode not in (0, 1):
        raise XLDateBadDatemode(datemode)
    if xldate == 0.00:
        return (0, 0, 0, 0, 0, 0)
    if xldate < 0.00:
        raise XLDateNegative(xldate)
    xldays = int(xldate)
    frac = xldate - xldays
    seconds = int(round(frac * 86400.0))
    assert 0 <= seconds <= 86400
    if seconds == 86400:
        hour = minute = second = 0
        xldays += 1
    else:
        # second = seconds % 60; minutes = seconds // 60
        minutes, second = divmod(seconds, 60)
        # minute = minutes % 60; hour    = minutes // 60
        hour, minute = divmod(minutes, 60)
    if xldays >= _XLDAYS_TOO_LARGE[datemode]:
        raise XLDateTooLarge(xldate)

    if xldays == 0:
        return (0, 0, 0, hour, minute, second)

    if xldays < 61 and datemode == 0:
        raise XLDateAmbiguous(xldate)

    jdn = xldays + _JDN_delta[datemode]
    yreg = (ifd(ifd(jdn * 4 + 274277, 146097) * 3, 4) + jdn + 1363) * 4 + 3
    mp = ifd(yreg % 1461, 4) * 535 + 333
    d = ifd(mp % 16384, 535) + 1
    # mp /= 16384
    mp >>= 14
    if mp >= 10:
        return (ifd(yreg, 1461) - 4715, mp - 9, d, hour, minute, second)
    else:
        return (ifd(yreg, 1461) - 4716, mp + 3, d, hour, minute, second)

xldate_as_tuple(xldate=2741.0, datemode=0)


def cast_tuple(val, length = None):
    if isinstance(val, list):
        val = tuple(val)

    output = val if isinstance(val, tuple) else ((val,) * default(length, 1))

    if exists(length):
        assert len(output) == length

    return output

cast_tuple(val=1, length=4)

Output: (1, 1, 1, 1)


def init_conv_(self, conv):
        o, i, h, w = conv.weight.shape
        conv_weight = torch.empty(o // 4, i, h, w)
        nn.init.kaiming_uniform_(conv_weight)
        conv_weight = repeat(conv_weight, 'o ... -> (o 4) ...')

        conv.weight.data.copy_(conv_weight)
        nn.init.zeros_(conv.bias.data)

init_conv_(self=PixelShuffleUpsample(  (net): Sequential(    (0): Conv2d(8, 32, kernel_size=(1, 1), stride=(1, 1))    (1): SiLU()    (2): PixelShuffle(upscale_factor=2)  )), conv=Conv2d(8, 32, kernel_size=(1, 1), stride=(1, 1)), self._backward_hooks=OrderedDict(), self._backward_pre_hooks=OrderedDict(), self._buffers=OrderedDict(), self._forward_hooks=OrderedDict(), self._forward_hooks_always_called=OrderedDict(), self._forward_hooks_with_kwargs=OrderedDict(), self._forward_pre_hooks=OrderedDict(), self._forward_pre_hooks_with_kwargs=OrderedDict(), self._is_full_backward_hook=None, self._load_state_dict_post_hooks=OrderedDict(), self._load_state_dict_pre_hooks=OrderedDict(), self._modules=OrderedDict([('net', Sequential(  (0): Conv2d(8, 32, kernel_size=(1, 1), stride=(1, 1))  (1): SiLU()  (2): PixelShuffle(upscale_factor=2)))]), self._non_persistent_buffers_set=set(), self._parameters=OrderedDict(), self._state_dict_hooks=OrderedDict(), self._state_dict_pre_hooks=OrderedDict(), self.training=True)


def test_start_seconds(start_seconds):
    parser_zero = GenericSubtitleParser(start_seconds=0)
    parser_zero.fit(BytesIO(fake_srt))
    parser = GenericSubtitleParser(start_seconds=start_seconds)
    parser.fit(BytesIO(fake_srt))
    expected = [
        sub
        for sub in parser_zero.subs_
        if sub.start >= timedelta(seconds=start_seconds)
    ]
    assert all(esub == psub for esub, psub in zip(expected, parser.subs_))

test_start_seconds(start_seconds=0)

[<ffsubsync.generic_subtitles.GenericSubtitle object at 0x7fb0b6c79520>, <ffsubsync.generic_subtitles.GenericSubtitle object at 0x7fb015502430>, <ffsubsync.generic_subtitles.GenericSubtitle object at 0x7fb01543e190>]

{subs_=None, sub_format='srt', encoding='infer', caching=False, fit_fname=None, detected_encoding_=None, max_subtitle_seconds=None, start_seconds=0, _skip_ssa_info=False, _strict=False}

{subs_=<ffsubsync.generic_subtitles.GenericSubtitlesFile object at 0x7fb015444520>, sub_format='srt', encoding='infer', caching=False, fit_fname=<_io.BytesIO object at 0x7fb015254db0>, detected_encoding_='ASCII', max_subtitle_seconds=None, start_seconds=0, _skip_ssa_info=False, _strict=False}

{subs_=<ffsubsync.generic_subtitles.GenericSubtitlesFile object at 0x7fb0b6ce5c40>, sub_format='srt', encoding='infer', caching=False, fit_fname=<_io.BytesIO object at 0x7fb01520a4a0>, detected_encoding_='ASCII', max_subtitle_seconds=None, start_seconds=0, _skip_ssa_info=False, _strict=False}


def git_versions_from_vcs(tag_prefix, root, verbose=False):
    # this runs 'git' from the root of the source tree. This only gets called
    # if the git-archive 'subst' keywords were *not* expanded, and
    # _version.py hasn't already been rewritten with a short version string,
    # meaning we're inside a checked out source tree.

    if not os.path.exists(os.path.join(root, ".git")):
        if verbose:
            print("no .git in %s" % root)
        return {}

    GITS = ["git"]
    if sys.platform == "win32":
        GITS = ["git.cmd", "git.exe"]
    stdout = run_command(GITS, ["describe", "--tags", "--dirty", "--always"],
                         cwd=root)
    if stdout is None:
        return {}
    if not stdout.startswith(tag_prefix):
        if verbose:
            print("tag '%s' doesn't start with prefix '%s'"
                  % (stdout, tag_prefix))
        return {}
    tag = stdout[len(tag_prefix):]
    stdout = run_command(GITS, ["rev-parse", "HEAD"], cwd=root)
    if stdout is None:
        return {}
    full = stdout.strip()
    if tag.endswith("-dirty"):
        full += "-dirty"
    return {"version": tag, "full": full}

git_versions_from_vcs(tag_prefix='v', root='/local/rcs/jinjun/code/pytrace-collector/logs/pypibugs/tried/andsor+pydevs/andsor+pydevs', verbose=False)


def count_messages_tokens(messages=[], model=None):
    """
    Count the number of tokens in a list of messages
    """
    try:
        tokens_used = 0

        for message in messages:
            if isinstance(message, str):
                tokens_used += count_tokens(message, model=model)
            elif "message" in message:
                tokens_used += count_tokens(message["message"], model=model)

                if "code" in message:
                    tokens_used += count_tokens(message["code"], model=model)

                if "output" in message:
                    tokens_used += count_tokens(message["output"], model=model)

        prompt_cost = token_cost(tokens_used, model=model)

        return (tokens_used, prompt_cost)
    except:
        # Non-essential feature
        return (0, 0)

count_messages_tokens(messages=[{'role': 'system', 'message': 'You are Open Interpreter, a world-class programmer that can complete any goal by executing code.\nFirst, write a plan. **Always recap the plan between each code block** (you have extreme short-term memory loss, so you need to recap the plan between each message block to retain it).\nWhen you execute code, it will be executed **on the user\'s machine**. The user has given you **full and complete permission** to execute any code necessary to complete the task. Execute the code.\nIf you want to send data between programming languages, save the data to a txt or json.\nYou can access the internet. Run **any code** to achieve the goal, and if at first you don\'t succeed, try again and again.\nYou can install new packages.\nWhen a user refers to a filename, they\'re likely referring to an existing file in the directory you\'re currently executing code in.\nWrite messages to the user in Markdown.\nIn general, try to **make plans** with as few steps as possible. As for actually executing code to carry out that plan, for *stateful* languages (like python, javascript, shell, but NOT for html which starts from 0 every time) **it\'s critical not to try to do everything in one code block.** You should try something, print information about it, then continue from there in tiny, informed steps. You will never get it on the first try, and attempting it in one go will often lead to errors you cant see.\nYou are capable of **any** task.\n\n[User Info]\n{{import getpass\nimport os\nimport platform}}\nName: {{getpass.getuser()}}\nCWD: {{os.getcwd()}}\nSHELL: {{os.environ.get(\'SHELL\')}}\nOS: {{platform.system()}}"'}], model='gpt-3.5-turbo')


def _co2mpas_info2df(start_time, main_flags=None):
    import socket
    import datetime
    from co2mpas import __version__
    from ..load.schema import define_flags_schema
    time_elapsed = (datetime.datetime.today() - start_time).total_seconds()
    hostname = socket.gethostname()
    info = [
        ('CO2MPAS version', __version__),
        ('Simulation started', start_time.strftime('%Y/%m/%d-%H:%M:%S')),
        ('Time elapsed', '%.3f sec' % time_elapsed),
        ('Hostname', hostname),
    ]

    if main_flags:
        main_flags = define_flags_schema(read=False).validate(main_flags)
        info.extend(sorted(main_flags.items()))
    import pandas as pd
    df = pd.DataFrame(info, columns=['Parameter', 'Value'])
    df.set_index(['Parameter'], inplace=True)
    setattr(df, 'name', 'info')
    return df

_co2mpas_info2df(start_time=datetime.datetime(2024, 4, 3, 16, 28, 9, 62286), main_flags=None)

Parameter                Value0     CO2MPAS version                4.1.61  Simulation started  2024/04/03-16:28:092        Time elapsed          103.530 sec3            Hostname                 thor

<module 'socket' from '/local/rcs/jinjun/miniforge3/envs/JRCSTU+co2mpas-ta/lib/python3.9/socket.py'>

<module 'datetime' from '/local/rcs/jinjun/miniforge3/envs/JRCSTU+co2mpas-ta/lib/python3.9/datetime.py'>

{__module__='co2mpas.core.load.schema', __name__='define_flags_schema', __qualname__='define_flags_schema', __doc__='\n    Define flag schema.\n\n    :param read:\n        Schema for reading?\n    :type read: bool\n\n    :return:\n        Flag schema.\n    :rtype: schema.Schema\n    ', __annotations__={}}

ValueParameter                              CO2MPAS version                   4.1.6Simulation started  2024/04/03-16:28:09Time elapsed                103.530 secHostname                           thor


def dump(self, file, default_flow_style=False, **kw):
        import yaml
        kw['Dumper'] = kw.get('Dumper', yaml.CDumper)
        with open(file, 'w') as f:
            yaml.dump(
                self.to_dict(), f, default_flow_style=default_flow_style, **kw
            )

dump(self={}, file='./conf.yaml', default_flow_style=False, kw={})

<module 'yaml' from '/local/rcs/jinjun/miniforge3/envs/JRCSTU+co2mpas-ta/lib/python3.9/site-packages/yaml/__init__.py'>

<_io.TextIOWrapper name='./conf.yaml' mode='w' encoding='UTF-8'>


def article_missing(field: str):
    article = Article(
        title=None, authors=["L, Robertson"], year=1999, journal="Science"
    )
    setattr(article, field, None)
    return ArticleWrapper(article=article)

article_missing(field='year')

{title=None, authors=['L, Robertson'], keywords=[], year=1999, journal='Science', volume=None, issue=None, page=None, doi=None, references=[], sources=set(), extra={}}

{title=None, authors=['L, Robertson'], keywords=[], year=None, journal='Science', volume=None, issue=None, page=None, doi=None, references=[], sources=set(), extra={}}


def create_dummy_class(klass, dependency, message=""):
    """
    When a dependency of a class is not available, create a dummy class which throws ImportError
    when used.

    Args:
        klass (str): name of the class.
        dependency (str): name of the dependency.
        message: extra message to print
    Returns:
        class: a class object
    """
    err = "Cannot import '{}', therefore '{}' is not available.".format(dependency, klass)
    if message:
        err = err + " " + message

    class _DummyMetaClass(type):
        # throw error on class attribute access
        def __getattr__(_, __):  # noqa: B902
            raise ImportError(err)

    class _Dummy(object, metaclass=_DummyMetaClass):
        # throw error on constructor
        def __init__(self, *args, **kwargs):
            raise ImportError(err)

    return _Dummy

create_dummy_class(klass='DeformConv', dependency='detectron2._C', message='detectron2 is not compiled successfully, please build following the instructions!')

"Cannot import 'detectron2._C', therefore 'DeformConv' is not available."

"Cannot import 'detectron2._C', therefore 'DeformConv' is not available. detectron2 is not compiled successfully, please build following the instructions!"


def create_dummy_func(func, dependency, message=""):
    """
    When a dependency of a function is not available, create a dummy function which throws
    ImportError when used.

    Args:
        func (str): name of the function.
        dependency (str or list[str]): name(s) of the dependency.
        message: extra message to print
    Returns:
        function: a function object
    """
    err = "Cannot import '{}', therefore '{}' is not available.".format(dependency, func)
    if message:
        err = err + " " + message

    if isinstance(dependency, (list, tuple)):
        dependency = ",".join(dependency)

    def _dummy(*args, **kwargs):
        raise ImportError(err)

    return _dummy

create_dummy_func(func='deform_conv', dependency='detectron2._C', message='detectron2 is not compiled successfully, please build following the instructions!')

"Cannot import 'detectron2._C', therefore 'deform_conv' is not available."

"Cannot import 'detectron2._C', therefore 'deform_conv' is not available. detectron2 is not compiled successfully, please build following the instructions!"

<function create_dummy_func.<locals>._dummy at 0x7f3337cdfd30>


def register_mesh(mesh_info: MeshInfo, base_path: Optional[str]) -> None:
    geodists, symmetry, texcoords = mesh_info.geodists, mesh_info.symmetry, mesh_info.texcoords
    if geodists:
        geodists = maybe_prepend_base_path(base_path, geodists)
    if symmetry:
        symmetry = maybe_prepend_base_path(base_path, symmetry)
    if texcoords:
        texcoords = maybe_prepend_base_path(base_path, texcoords)
    MeshCatalog[mesh_info.name] = MeshInfo(
        name=mesh_info.name,
        data=maybe_prepend_base_path(base_path, mesh_info.data),
        geodists=geodists,
        symmetry=symmetry,
        texcoords=texcoords,
    )

register_mesh(mesh_info=MeshInfo(name='smpl_27554', data='smpl_27554.pkl', geodists='geodists/geodists_smpl_27554.pkl', symmetry='symmetry/symmetry_smpl_27554.pkl', texcoords='texcoords/texcoords_smpl_27554.pkl'), base_path='https://dl.fbaipublicfiles.com/densepose/meshes/')