0

ディレクトリ内のアイテムのセットにパターンを追加または削除する 2 つのユーティリティ関数があります。名前の変更を行う行を除いて、機能はまったく同じです。これにより、それらを1つの機能にマージできると信じています。

以下に 2 つの関数を示します。

def append_items(source, pattern, dirs = True, recurse = False):
    """
    Append the pattern to all items within a directory

    source = act on this directory
    pattern = add this to the start of the file
    dirs = apply to directorys
    recurse = work recursively 
    """
    for item in os.listdir(source):
        path =  os.path.join(source, item)
        if "svn" not in item:
            if os.path.isdir(path):
                # Recurse first
                if recurse:
                    append_items(path, pattern, dirs, recurse)
                if dirs:
                    rename(path, path + pattern)
            elif os.path.isfile(path):
                name, ext = os.path.splitext(item)
                # Append pattern, add extension back
                new_path = os.path.join(source, "%s%s" % (name, pattern) + ext)
                rename(path, new_path)

#----------------------------------------------------------------------------------------------------------

def remove_string_from_items(source, pattern, dirs = True, recurse = False):
    """
    Remove a pattern from all items within a directory

    source =  directory
    pattern = text to replace
    """
    for item in os.listdir(source):
        path =  os.path.join(source, item)
        if "svn" not in item:
            if os.path.isdir(path):
                # Recurse first
                if recurse:
                    remove_string_from_items(path, pattern, dirs, recurse)
                if dirs and pattern in item:
                    target = os.path.join(source, string.replace(item, pattern, ""))
                    rename(path, target)
            elif os.path.isfile(path) and pattern in item:
                target = os.path.join(source, string.replace(item, pattern, ""))
                rename(path, target)

誰でもよりクリーンなソリューションを教えてもらえますか?

4

2 に答える 2

1

以下は、コードの繰り返しが少ないものと同等である必要があります。

def _append_or_remove(source, pattern, dirs = True, recurse = False, append = True):
    for item in os.listdir(source):
        path =  os.path.join(source, item)
        if "svn" not in item:
            if os.path.isdir(path):
                # Recurse first
                if recurse:
                    if append:
                        append_items(path, pattern, dirs, recurse)
                    else:
                        remove_string_from_items(path, pattern, dirs, recurse)
                if dirs and pattern in item:
                    target = os.path.join(source, string.replace(item, pattern, ""))
                    rename(path, target)
            elif os.path.isfile(path) and pattern in item:
                if append:
                    name, ext = os.path.splitext(item)
                    # Append pattern, add extension back
                    target = os.path.join(source, "%s%s" % (name, pattern) + ext)
                else:
                    target = os.path.join(source, string.replace(item, pattern, ""))
                rename(path, target)

def append_items(source, pattern, dirs = True, recurse = False):
    """
    Append the pattern to all items within a directory

    source = act on this directory
    pattern = add this to the start of the file
    dirs = apply to directorys
    recurse = work recursively 
    """
    return _append_or_remove(source, pattern, dirs, recurse, True)

def remove_string_from_items(source, pattern, dirs = True, recurse = False):
    """
    Remove a pattern from all items within a directory

    source =  directory
    pattern = text to replace
    """
    return _append_or_remove(source, pattern, dirs, recurse, False)
于 2012-10-11T16:43:55.717 に答える
0

別の方法として、繰り返されるコードを 2 つの特定の関数 (1 つはディレクトリの名前変更用、もう 1 つはファイル用) によってパラメーター化される関数にカプセル化するバージョンがあります。したがって、特定のappend/remove実装では、定義する必要があるのは、特定のロジックを実行するパラメータ関数だけです。これは に渡され_workon_itemsます。

def append_items(source, pattern, dirs = True, recurse = False):
    """
    Append the pattern to all items within a directory

    source = act on this directory
    pattern = add this to the start of the file
    dirs = apply to directorys
    recurse = work recursively 
    """
    def dir_rename(path, pattern, source, item):
        rename(path, path + pattern)

    def file_rename(path, pattern, source, item):
        name, ext = os.path.splitext(item)
        # Append pattern, add extension back
        new_path = os.path.join(source, "%s%s" % (name, pattern) + ext)
        rename(path, new_path)

    _workon_items(dir_rename, file_rename, source, pattern, dirs, recurse)

#----------------------------------------------------------------------------------------------------------

def remove_string_from_items(source, pattern, dirs = True, recurse = False):
    """
    Remove a pattern from all items within a directory

    source =  directory
    pattern = text to replace
    """
    def dir_rename(path, pattern, source, item):
        if pattern in item:
            target = os.path.join(source, string.replace(item, pattern, ""))
            rename(path, target)

    def file_rename(path, pattern, source, item):
        if pattern in item:
            target = os.path.join(source, string.replace(item, pattern, ""))
            rename(path, target)

    _workon_items(dir_rename, file_rename, source, pattern, dirs, recurse)

#----------------------------------------------------------------------------------------------------------

def _workon_items(dir_rename, file_rename, source, pattern, dirs, recurse):
    for item in os.listdir(source):
        path =  os.path.join(source, item)
        if "svn" not in item:
            if os.path.isdir(path):
                # Recurse first
                if recurse:
                    _workon_items(dir_rename, file_rename, path, pattern, dirs, recurse)
                if dirs:
                    dir_rename(path, pattern, source, item)
            elif os.path.isfile(path):
                file_rename(path, pattern, source, item)
于 2012-10-11T16:53:51.287 に答える