Python – In xonsh, how do I receive python expressions from the pipeline?

In xonsh, how do I receive python expressions from the pipeline?… here is a solution to the problem.

In xonsh, how do I receive python expressions from the pipeline?

In how does the xonsh shell receive python expressions from the pipeline? Example command with find as a pipeline provider:

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

for p in <stdin>: Obviously pseudocode. What do I have to replace it with?

Note: In bash I would use this structure:

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

Solution

The easiest way to pipe input to a Python expression is to use a function of type callable alias, which happens to accept a stdin Class file object. For example,

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

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

Of course you can skip @(func) by putting func in aliases

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

Or, if you just want to iterate through the output of find, you don’t even need a pipeline.

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

Related Problems and Solutions