コード例 #1
0
def change_doctestcommand_to_use_dashm_flag():
    r"""
    VimRegex: # note sure how to execute replace command in vim in one lin
    %s/python\s*\([A-Za-z_]+[\\/]\S*\)\.py\(.*\)/python -m \1 \2

    """
    # http://stackoverflow.com/questions/18737863/passing-a-function-to-re-sub-in-python
    # CANNOT USE [^ ] FOR SOME GOD DAMN REASON USE /S instead
    regex_list = ['python [A-Za-z_]+[\\/]\S* --allexamples']
    dpath_list = [
        ut.ensure_crossplat_path(ut.truepath('~/code/utool/utool')),
        ut.ensure_crossplat_path(ut.truepath('~/code/ibeis/ibeis')),
        ut.ensure_crossplat_path(ut.truepath('~/code/vtool/vtool')),
        ut.ensure_crossplat_path(ut.truepath('~/code/plottool/plottool')),
        ut.ensure_crossplat_path(ut.truepath('~/code/guitool/guitool')),
    ]
    #ut.named_field_repl(['python ', ('modrelpath',),])
    #['python ', ('modrelpath', 'utool[\\/].*'), '--allexamples'])
    res = ut.grep(regex_list,
                  recursive=True,
                  dpath_list=dpath_list,
                  verbose=True)
    found_filestr_list, found_lines_list, found_lxs_list = res
    fpath = res[0][0]

    import re
    keypat_list = [
        ('prefix', 'python\s*'),
        ('modrelpath', '[A-Za-z_]+[\\/]\S*'),
        ('suffix', '.*'),
    ]
    namedregex = ut.named_field_regex(keypat_list)

    # Define function to pass to re.sub
    def replmodpath(matchobj):
        groupdict_ = matchobj.groupdict()
        relpath = groupdict_['modrelpath']
        prefix = groupdict_['prefix']
        suffix = groupdict_['suffix']
        modname = relpath
        modname = modname.replace('\\', '.')
        modname = modname.replace('/', '.')
        modname = modname.replace('.py', '')
        return prefix + '-m ' + modname + suffix

    for fpath in found_filestr_list:
        text = ut.read_from(fpath)
        #matchobj = re.search(namedregex, text, flags=re.MULTILINE)
        #print(text)
        #for matchobj in re.finditer(namedregex, text):
        #    print(ut.get_match_text(matchobj))
        #    print('--')
        newtext = re.sub(namedregex, replmodpath, text)
        # Perform replacement
        ut.write_to(fpath, newtext)
コード例 #2
0
def change_doctestcommand_to_use_dashm_flag():
    r"""
    VimRegex: # note sure how to execute replace command in vim in one lin
    %s/python\s*\([A-Za-z_]+[\\/]\S*\)\.py\(.*\)/python -m \1 \2

    """
    # http://stackoverflow.com/questions/18737863/passing-a-function-to-re-sub-in-python
    # CANNOT USE [^ ] FOR SOME GOD DAMN REASON USE /S instead
    regex_list = ['python [A-Za-z_]+[\\/]\S* --allexamples']
    dpath_list = [
        ut.ensure_crossplat_path(ut.truepath('~/code/utool/utool')),
        ut.ensure_crossplat_path(ut.truepath('~/code/ibeis/ibeis')),
        ut.ensure_crossplat_path(ut.truepath('~/code/vtool/vtool')),
        ut.ensure_crossplat_path(ut.truepath('~/code/plottool/plottool')),
        ut.ensure_crossplat_path(ut.truepath('~/code/guitool/guitool')),
    ]
    #ut.named_field_repl(['python ', ('modrelpath',),])
    #['python ', ('modrelpath', 'utool[\\/].*'), '--allexamples'])
    res = ut.grep(regex_list, recursive=True, dpath_list=dpath_list, verbose=True)
    found_filestr_list, found_lines_list, found_lxs_list = res
    fpath = res[0][0]

    import re
    keypat_list = [
        ('prefix', 'python\s*'),
        ('modrelpath', '[A-Za-z_]+[\\/]\S*'),
        ('suffix', '.*'),
    ]
    namedregex = ut.named_field_regex(keypat_list)

    # Define function to pass to re.sub
    def replmodpath(matchobj):
        groupdict_ = matchobj.groupdict()
        relpath = groupdict_['modrelpath']
        prefix = groupdict_['prefix']
        suffix = groupdict_['suffix']
        modname = relpath
        modname = modname.replace('\\', '.')
        modname = modname.replace('/', '.')
        modname = modname.replace('.py', '')
        return prefix + '-m ' + modname + suffix

    for fpath in found_filestr_list:
        text = ut.read_from(fpath)
        #matchobj = re.search(namedregex, text, flags=re.MULTILINE)
        #print(text)
        #for matchobj in re.finditer(namedregex, text):
        #    print(ut.get_match_text(matchobj))
        #    print('--')
        newtext = re.sub(namedregex, replmodpath, text)
        # Perform replacement
        ut.write_to(fpath, newtext)
コード例 #3
0
ファイル: ingest_database.py プロジェクト: heroinlin/ibeis
def get_name_texts_from_gnames(gpath_list, img_dir, fmtkey='{name:*}[aid:d].{ext}'):
    """
    Args:
        gpath_list (list): list of image paths
        img_dir (str): path to image directory
        fmtkey (str): pattern string to parse names from (default = '{name:*}[aid:d].{ext}')

    Returns:
        list: name_list - based on the parent folder of each image

    CommandLine:
        python -m ibeis.dbio.ingest_database --test-get_name_texts_from_gnames

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.dbio.ingest_database import *  # NOQA
        >>> gpath_list = ['e_f0273_f.jpg', 'f0001_f.jpg', 'f0259_l_3.jpg', 'f0259_f_1.jpg',  'f0259_f (1).jpg', 'f0058_u16_f.jpg']
        >>> img_dir = ''
        >>> fmtkey = FMT_KEYS.elephant_fmt
        >>> result = get_name_texts_from_gnames(gpath_list, img_dir, fmtkey)
        >>> print(result)
    """
    # These define regexes that attempt to parse the insane and contradicting
    # naming schemes of the image sets that we get.
    INGEST_FORMATS = {
        FMT_KEYS.name_fmt: ut.named_field_regex([
            ('name', r'[a-zA-Z]+'),  # all alpha characters
            ('id',   r'\d*'),        # first numbers (if existant)
            ( None,  r'\.'),
            ('ext',  r'\w+'),
        ]),

        FMT_KEYS.snails_fmt: ut.named_field_regex([
            ('name', r'[a-zA-Z]+\d\d'),  # species and 2 numbers
            ('id',   r'\d\d'),  # 2 more numbers
            ( None,  r'\.'),
            ('ext',  r'\w+'),
        ]),

        FMT_KEYS.giraffe1_fmt: ut.named_field_regex([
            ('name',  r'G\d+'),
            ('under', r'_'),
            ('id',    r'\d+'),
            ( None,   r'\.'),
            ('ext',   r'\w+'),
        ]),

        FMT_KEYS.seal2_fmt: ut.named_field_regex([
            ('name',  r'Phs\d+'),  # Phs and then numbers
            ('id',    r'[A-Z]+'),  # 1 or more letters
            ( None,   r'\.'),
            ('ext',   r'\w+'),
        ]),

        # this one defines multiple possible regex types. yay standards
        FMT_KEYS.elephant_fmt: [
            ut.named_field_regex([
                ('prefix',  r'(e_)?'),
                ('name', r'[a-zA-Z0-9]+'),
                ('view', r'_[rflo]'),
                ('id',    r'([ _][^.]+)?'),
                ( None,   r'\.'),
                ('ext',   r'\w+'),
            ]),
            ut.named_field_regex([
                ('prefix',  r'(e_)?'),
                ('name', r'[a-zA-Z0-9]+'),
                ('id',    r'([ _][^.]+)?'),
                ('view', r'_[rflo]'),
                ( None,   r'\.'),
                ('ext',   r'\w+'),
            ])],
    }
    regex_list = INGEST_FORMATS.get(fmtkey, fmtkey)
    gname_list = ut.fpaths_to_fnames(gpath_list)
    def parse_format(regex_list, gname):
        if not isinstance(regex_list, list):
            regex_list = [regex_list]
        for regex in regex_list:
            result = ut.regex_parse(regex, gname)
            if result is not None:
                return result
        return None

    parsed_list = [parse_format(regex_list, gname) for gname in gname_list]

    anyfailed = False
    for gpath, parsed in zip(gpath_list, parsed_list):
        if parsed is None:
            print('FAILED TO PARSE: %r' % gpath)
            anyfailed = True
    if anyfailed:
        msg = ('FAILED REGEX: %r' % regex_list)
        raise Exception(msg)

    _name_list = [parsed['name'] for parsed in parsed_list]
    name_list = list(map(normalize_name, _name_list))
    return name_list