发电机糖。

useless.pipes的Python项目详细描述


发电机周围的假黑糖。如果您编写嵌套for循环或嵌套函数a(b(c([1,2,3]))),这可能会有帮助。

基本上是这样的:

[1, 2, 3] | add(2) | list == [3, 4, 5]

其中

@worker
def add(items, n):
        for i in items:
                yield i + n

最左边的参数总是来自左边的迭代器。其他参数表示绑定状态。使用此工作进程分为两步。

adder = add(2)                # bind a state in a closure
adder([1, 2, 3]) == [3, 4, 5] # apply an iterator

工人往往非常简单和简短,可重复使用和易于测试。

@worker
def echo(items):
        for i in items: yield i
echo = echo()   # echo 'has' no state
assert [1, 2, 3] | echo == [1, 2, 3]

有时候你可以做到这样:

filter_audio_files = fs.filter_by_ext(['.mp3'])

@producer
def folders_with_audio_files(path):
    for root, folders, filenames in os.walk(path):
        if any(filenames | filter_audio_files):
            yield root


@worker
def that_need_fix(paths):
        for path in paths:
                files     = listdir(path) | filter_audio_files | join_path(path) | list
                dos_names = files | get_83DOS_name | list

                if files.sort() != dos_names.sort():
                        yield path
that_need_fix = that_need_fix()


# and the outermost commands in a script then look like this

#give me all folders with mp3-files inside, e.g. print them to stdout
folders_with_audio_files(root)
#give me all folder that need a specific fix, aka dry-mode
folders_with_audio_files(root) | that_need_fix
#actually apply a fix to these folders
folders_with_audio_files(root) | that_need_fix | apply_fix

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将字符串转换为自定义对象   java如何从socket方法获取数据?   Java中的soap读取回车和新行   java在单击时替换图像   java推荐的使用RXJava执行异步任务的方法   java MySql连接器JDBC驱动程序不支持连接池吗?   java将活动堆栈清理到顶部   java计数用户输入的数量   java从webservice下载大文件导致应用程序性能问题   JavaLocalDate。EPOCH不可用   java如何在使用Selenium等待一定时间后,在页面无法加载(get(url))时自动刷新页面   java Calendar setLenient方法不允许检查年份字段的健全性   java Eclipse和intelliJ 安卓 SDK问题   java为什么我可以在没有super关键字的情况下调用父方法?   java iText的PDF格式不好