就像赛德,但是巨蟒!
pyin的Python项目详细描述
就像赛德,但是Python!
为什么?
有很多unix工具,比如用于处理文本的sed和awk。 来自stdin或磁盘上的文件的数据,但是语法可能不友好,并且 有时用for循环编写一个非常简单的脚本会更容易 还有一些if语句。这个项目想让你陷入其中 for循环并让您编写自己的python表达式以快速获得任务 无需实际编写脚本、处理I/O等即可完成。
命令行界面
此项目旨在从包含的实用程序pyin中使用,尽管 底层的pyin.core.pmap()函数可以用于 非字符串对象。
Usage: pyin [OPTIONS] EXPRESSIONS... It's like sed, but Python! Map Python expressions across lines of text. If an expression evaluates as 'False' or 'None' then the current line is thrown away. If an expression evaluates as 'True' then the next expression is evaluated. If a list or dictionary is encountered it is JSON encoded. All other objects are cast to string. Newline characters are stripped from the end of each line before processing and are added on write unless disabled with '--no-newline'. This utility employs 'eval()' internally but uses a limited scope to help prevent accidental side effects, but there are plenty of ways to get around this so don't pass anything through pyin that you wouldn't pass through 'eval()'. Remove lines that do not contain a specific word: $ cat INFILE | pyin "'word' in line" Capitalize lines containing a specific word: $ cat INFILE | pyin "line.upper() if 'word' in line else line" Only print every other word from lines that contain a specific word: $ cat INFILE | pyin \ > "'word' in line"\ # Get lines with 'word' in them > "line.split()[::2])" \ # Grab every other word > "' '.join(line)" # Convert list from previous expr to str Process all input text as though it was a single line to replace carriage returns with the system newline character: $ cat INFILE | pyin --block \ > "line.replace('\r\n', os.newline)" For a more in-depth explanation about exactly what's going on under the hood, see the the docstring in 'pyin.core.pmap()': $ python -c "help('pyin.core.pmap')" Options: --version Show the version and exit. -i, --infile PATH Input text file. [default: stdin] -o, --outfile PATH Output text file. [default: stdout] --block Operate on all input text as though it was a single line. --no-newline Don't ensure each line ends with a newline character. --help Show this message and exit.
安装
通过PIP:
$ pip install pyin
来自主分支:
$ git clone https://github.com/geowurster/pyin $cd pyin && python setup.py install
那py -x呢?
这个项目的大部分内容都是在很少了解py的情况下编写的,而不是 关于py -x的知识,其用途几乎完全相同。这个 这两个项目的主要区别在于pyin需要I/O和 对计算结果为True或 False
开发
安装:
$ git clone https://github.com/geowurster/pyin $cd pyin $ virtualenv venv &&source venv/bin/activate $ pip install -e .\[dev\]$ py.test tests --cov pyin --cov-report term-missing
许可证
见LICENSE.txt