r"""
    Formats a flat configuration dict into a short string label. This is useful
    for re-creating command line strings.

    Args:
        cfg (dict):
        name (str): (default = None)
        nonlbl_keys (list): (default = INTERNAL_CFGKEYS)

    Returns:
        str: cfg_lbl

    CommandLine:
        python -m utool.util_gridsearch get_cfg_lbl

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_gridsearch import *  # NOQA
        >>> import utool as ut
        >>> cfg = {'_cfgname': 'test', 'var1': 'val1', 'var2': 'val2'}
        >>> name = None
        >>> nonlbl_keys = ['_cfgstr', '_cfgname', '_cfgtype', '_cfgindex']
        >>> cfg_lbl = get_cfg_lbl(cfg, name, nonlbl_keys)
        >>> result = ('cfg_lbl = %s' % (six.text_type(cfg_lbl),))
        >>> print(result)
        cfg_lbl = test:var1=val1,var2=val2

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_gridsearch import *  # NOQA
        >>> import utool as ut
        >>> cfg = {'var1': 'val1', 'var2': 'val2'}
        >>> default_cfg = {'var2': 'val1', 'var1': 'val1'}
        >>> name = None
        >>> cfg_lbl = get_cfg_lbl(cfg, name, default_cfg=default_cfg)
        >>> result = ('cfg_lbl = %s' % (six.text_type(cfg_lbl),))
        >>> print(result)
        cfg_lbl = :var2=val2

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_gridsearch import *  # NOQA
        >>> import utool as ut
        >>> cfg = {'_cfgname': 'test:K=[1,2,3]', 'K': '1'}
        >>> name = None
        >>> nonlbl_keys = ['_cfgstr', '_cfgname', '_cfgtype', '_cfgindex']
        >>> cfg_lbl = get_cfg_lbl(cfg, name, nonlbl_keys)
        >>> result = ('cfg_lbl = %s' % (six.text_type(cfg_lbl),))
        >>> print(result)
        cfg_lbl = test:K=1

def get_cfg_lbl(cfg, name=None, nonlbl_keys=INTERNAL_CFGKEYS, key_order=None,
                with_name=True, default_cfg=None, sep=''):
    r"""
    Formats a flat configuration dict into a short string label. This is useful
    for re-creating command line strings.

    Args:
        cfg (dict):
        name (str): (default = None)
        nonlbl_keys (list): (default = INTERNAL_CFGKEYS)

    Returns:
        str: cfg_lbl

    CommandLine:
        python -m utool.util_gridsearch get_cfg_lbl

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_gridsearch import *  # NOQA
        >>> import utool as ut
        >>> cfg = {'_cfgname': 'test', 'var1': 'val1', 'var2': 'val2'}
        >>> name = None
        >>> nonlbl_keys = ['_cfgstr', '_cfgname', '_cfgtype', '_cfgindex']
        >>> cfg_lbl = get_cfg_lbl(cfg, name, nonlbl_keys)
        >>> result = ('cfg_lbl = %s' % (six.text_type(cfg_lbl),))
        >>> print(result)
        cfg_lbl = test:var1=val1,var2=val2

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_gridsearch import *  # NOQA
        >>> import utool as ut
        >>> cfg = {'var1': 'val1', 'var2': 'val2'}
        >>> default_cfg = {'var2': 'val1', 'var1': 'val1'}
        >>> name = None
        >>> cfg_lbl = get_cfg_lbl(cfg, name, default_cfg=default_cfg)
        >>> result = ('cfg_lbl = %s' % (six.text_type(cfg_lbl),))
        >>> print(result)
        cfg_lbl = :var2=val2

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_gridsearch import *  # NOQA
        >>> import utool as ut
        >>> cfg = {'_cfgname': 'test:K=[1,2,3]', 'K': '1'}
        >>> name = None
        >>> nonlbl_keys = ['_cfgstr', '_cfgname', '_cfgtype', '_cfgindex']
        >>> cfg_lbl = get_cfg_lbl(cfg, name, nonlbl_keys)
        >>> result = ('cfg_lbl = %s' % (six.text_type(cfg_lbl),))
        >>> print(result)
        cfg_lbl = test:K=1
    """
    import utool as ut
    if name is None:
        name = cfg.get('_cfgname', '')

    if default_cfg is not None:
        # Remove defaulted labels
        cfg = ut.partition_varied_cfg_list([cfg], default_cfg)[1][0]

    # remove keys that should not belong to the label
    _clean_cfg = ut.delete_keys(cfg.copy(), nonlbl_keys)
    _lbl = ut.repr4(_clean_cfg, explicit=True, nl=False, strvals=True,
                       key_order=key_order, itemsep=sep)
    # _search = ['dict(', ')', ' ']
    _search = ['dict(', ')']
    _repl = [''] * len(_search)
    _lbl = ut.multi_replace(_lbl, _search, _repl).rstrip(',')
    if not with_name:
        return _lbl
    if NAMEVARSEP in name:
        # hack for when name contains a little bit of the _lbl
        # VERY HACKY TO PARSE OUT PARTS OF THE GIVEN NAME.
        hacked_name, _cfgstr, _ = parse_cfgstr_name_options(name)
        _cfgstr_options_list = re.split(
            r',\s*' + ut.negative_lookahead(r'[^\[\]]*\]'), _cfgstr)
        #cfgstr_options_list = cfgopt_strs.split(',')
        _cfg_options = ut.parse_cfgstr_list(
            _cfgstr_options_list, smartcast=False, oldmode=False)
        #
        ut.delete_keys(_cfg_options, cfg.keys())
        _preflbl = ut.repr4(_cfg_options, explicit=True, nl=False, strvals=True)
        _preflbl = ut.multi_replace(_preflbl, _search, _repl).rstrip(',')
        hacked_name += NAMEVARSEP + _preflbl
        ###
        cfg_lbl = hacked_name + _lbl
    else:
        cfg_lbl = name + NAMEVARSEP + _lbl
    return cfg_lbl