在 xonsh 中,如何从管道接收到 python 表达式?

In xonsh how can I receive from a pipe to a python expression?

xonsh shell 中如何从管道接收到 python 表达式?使用 find 命令作为管道提供程序的示例:

find $WORKON_HOME -name pyvenv.cfg -print | for p in <stdin>: $(ls -dl @(p))

中的for p in <stdin>:明显是伪代码。我必须用什么来代替它?

注意:在 bash 中,我会使用这样的结构:

... | while read p; do ... done

将输入通过管道传递到 Python 表达式的最简单方法是使用 callable alias 函数,它恰好接受标准输入文件类对象。例如,

def func(args, stdin=None):
    for line in stdin:
        ls -dl @(line.strip())

find $WORKON_HOME -name pyvenv.cfg -print | @(func)

当然你可以跳过 @(func) 把 func 放在 aliases,

aliases['myls'] = func
find $WORKON_HOME -name pyvenv.cfg -print | myls

或者,如果您只想遍历 find 的输出,您甚至不需要管道。

for line in !(find $WORKON_HOME -name pyvenv.cfg -print):
    ls -dl @(line.strip())