If I was to use shell.py, then one of use would be the alias I created for checking uptime/downtime:
from shell import ex
t = (ex('last -x')
| 'grep system'
| 'head -20'
| 'tac'
).stdout()
print(t)
It has many features, pipe is no doubt that has been demonstrated by the example above, the other major features include, taken and modified from README:
Asynchronous execution
>>> from shell import ex
>>> c = asex('echo hello shell.py')
>>> # do something else
...
>>> # wait until process exit and read stdout
>>> c.stdout()
'hello shell.py\n'By default, it’s blocking, the ex.
Parallel execution
>>> from shell import parallel as par
>>> par.ex_all(['sleep 2', 'sleep 2']) # return in 2s
>>> # asynchronous parallel execution
>>> from shell import parallel as par
>>> # return immediately
>>> pe = par.asex_all(['sleep 2', 'sleep 2'])
>>> # do something else
...
>>> pe.wait()Redirection
>>> from shell import ex
>>> ex('echo yolo').wr('/tmp/out')
>>> ex('echo yolo') > '/tmp/out'
>>> # appending
>>> ex('echo yolo').ap('/tmp/out')
>>> ex('echo yolo') >> '/tmp/out'
If you know me, you should already be able to tell that I am not the fan of this type of library. Besides the interestingness, I really can’t see the practical point of such development. Nevertheless, some might actually find usefulness from it somehow. I believe a few people already do, it has 53 stars on GitHub as of writing, plus 2 forks.
Born on 2014-05-01 as Version 0.1.0. It only works for Python 2, although no error is thrown in Python 3 with my simple example, but the output isn’t expected. Strangely, there seem to be special care for Python 3 in the source code, but the results are different, Python 3 gets the byte stream and Python 2 normal string.
shell.py is written by Qingping Hou under the MIT License, currently version 0.5.0 (2014-05-12), for Python 2 and possibly Python 3.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.