将任何python函数转换为unix样式的命令

cbox的Python项目详细描述


#cbox-cli工具箱

[![pypi](https://img.shields.io/pypi/v/cbox.svg)(https://pypi.python.org/pypi/cbox/0.1.0)
[![pypi](https://img.shields.io/pypi/pyversions/cbox.svg)(https://pypi.python.org/pypi/cbox/0.1.0)
[![构建状态](https://travis-ci.org/shmuelamar/cbox.svg?branch=master)(https://travis ci.org/shmuelamar/cbox)
[![appveyor](https://img.shields.io/appveyor/ci/gruntjs/grunt.svg)(https://ci.appveyor.com/project/shmuelamar/cbox)
[![codecov](https://img.shields.io/codecov/c/github/shmuelamar/cbox.svg)(https://codecov.io/gh/shmuelamar/cbox)
[![PYPI](https://img.shields.io/PYPI/wheel/cbox.svg);
[![pypi](https://img.shields.io/pypi/l/cbox.svg)](

\te程序协同工作。*
>;**编写程序来处理文本流,因为这是一个通用接口。*

<;br/>

\features
*支持管道
*并发(线程或异步)
*支持错误处理(重定向到stderr)
*支持f或cli风格的内联代码
*各种输出处理选项(过滤、提前停止..)
*支持多种类型的管道处理(行、字符..)
*描述和参数的自动docstring解析帮助
*自动类型注释和默认解析
*返回基于错误的正确exitcode
*仅支持python3(是的,这是一个功能)
*支持子命令

\quickstart

**安装:**

``bash
`pip install-u cbox
`````

**示例用法:**
``python
`python!/usr/bin/env python3




$./hello.py--help
用法:hello.py[-h]--name name


按姓名问候一个人。

"192.168.1.1\n192.168.2.3\ngoogle.com"cbox—模块re're.findall("(?:\d+)+\d+",s)"
192.168.1.1
192.168.2.3
`````

*有关cbox内联运行的更多信息`cbox--help`*





**langs.txt**
``text
python http://python.org
lisp http://lisp-lang.org
ruby http://ruby-lang.org
```


dave只想从该文件中获取语言列表。

/>
他开始阅读有关*awk*、*grep*、*sed*、*tr*、*cut*和其他的内容,但记不住如何使用所有这些内容-毕竟他是一个python程序员,想使用python。

幸运的是,我们的小dave发现了关于**`cbox`**-一种简单的方法来转换任何python乐趣进入Unix风格的命令行!

现在dave可以轻松地使用python处理文件了!

/usr/bin/env python3
first.py
import cbox


@cbox.stream()
def first(line):
returnline.split()[0]




./first.py
python
lisp
ruby
```

**或内联cli样式:**

```bash
$cat langs.txt cbox's.split()[0]'
```

*注意:**`s`**是输入变量*


现在戴夫很满意了,所以像每一个满意的程序员一样-他想要更多!

dave现在想要一个langs url的列表。


/usr/bin/env python3
nth item.py
import cbox

@cbox.stream()
我们可以传递默认值,并为正确的类型使用类型注释
def nth项(line,n:int=0):
返回每行的nth项。

:param n:开始的项位置数从0
"
返回line.split()[n]

/usr/bin/env python3
$./nth-item.py--help
用法:nth-item.py[-h][-n n]


返回每行的第n个项。

$cat langs.txt./nth-item.py
python
lisp
ruby
`````
```bash
$cat langs.txt/nth-item.py-n 1
http://python.org
http://lisp-lang.org
http://ruby-lang.org
````

dave现在想从每个url中获取状态,为此我们可以使用"请求"。

但是要处理一个大的列表需要太长的时间,所以他最好使用线程。


/usr/bin/env python3
url status.py
import cbox
import请求



@cbox.stream(worker'u type'thread',max'u workers=4)
defurl_status(line):
resp=requests.get(line)
return f{{line}-{{resp.status{resp.status{u.status{u.status(line)}




/>cbox.main(URL_
````


**运行它:**

``bash
$cat lang s.txt/nth-line.py-n 1/url-status.py
http://python.org-200
http://lisp-lang.org-200
http://ruby-lang.org-200
`````
````````````
`````````````
>
**运行它:**



`````` bash
`` bash
`````` TXT CBOX的.sp【1】"cbox-m requests-w thread-c 4"f"{s}-{s}-{requests.get(s.get(s.statu code}"'
http://python.org-200
http://lisp-lang.org-200
http://ruby-lang.org-200
`` ` ` ` `

` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `>!/usr/bin/env python3
numbers only.py
import cbox

@cbox.stream()
def numbersonly(line):
""返回仅包含数字的行。报告给stderr的错误行。
如果检测到任何错误行,则以exitcode 2退出。
"
"如果没有行,则返回line.isnumeric():
raise valueerror('{}不是数字'。格式(行))
返回line


所有错误都重定向到"stderr":

`` bash
$echo-e"123\nabc\n567"./numbersonly.py
123
回溯(最近一次调用的最后一次):
文件"/home/shmulik/cs/cbox/cbox/concurrency.py",第54行,在"简单"运行程序中
yield func(item,**kwargs),无only.py",第11行,仅数字
raise valueerror('{}不是数字'。format(line))
valueerror:abc不是数字

<567

`````


>我们可以通过将"stderr"流重定向到`/dev/null`:
``bash
$echo-e"123\nabc\n567"/numbersonly.py 2>;/dev/null
123
567
```


`:

`` bash
$echo$?
2
```



``巨蟒
/usr/bin/env python3
提取域。py
导入re
导入cbox

@cbox.stream()
def extract_domains(行):
"尝试使用简单正则表达式从输入中提取所有域。"
返回re.findall(r'(?":\w++++\w+",line)或none或none都不能省略


域名.py
google.com
cbox.com
facebook.com
```

/usr/bin/env python3
head.py
导入cbox


counter=0



@cbox.stream()
def head(line,n:int):
"返回第一行‘n‘行’"
全局计数器
counter+=1



如果counter>;n:
提高cbox cbox.stop();还可以提高stopotopic迭()
返回行



如果__姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名:姓名`支持**简单(默认)**,**异步**和**线程**工作线程。我们可以像这样使用异步:

`` python
!!/usr/bin/env python3
tcping.py
import asyncio
import cbox

@cbox.stream(worker_type="asyncio",workers_window=30)
async def tcping(domain,timeout:int=3):
loop=asyncio.get_event_loop()

fut=asyncio.open_connection(domain,80,loop=loop)
尝试:
reader,writer=awit asyncio.wait_对于(fut,timeout=timeout)
writer.close()
status='up'
除非(oserror,asyncio.timeouterror,asyncio.timeouterror):
status='down'



返回{}是{}。format(域,状态)





如果uu-name\uu-main uu-main uu-main:
>cbox.main(tcping)

``````





<阿西NCIO。


>运行它:

``bash
$echo-e"192.168.1.1\n192.168.2.2.3\ngoogle.com"/tcping.py
192.168.1.1.1向下
192.168.2.3向下
google.com向上
``

我们可以在"examples/dir"上找到更多的例子,可以在"examples/dir"上找到更多的例子

;contribution
cbox是一个开源软件,面向所有人。请随意创建prs,向examples/dir添加示例,请求功能并提出问题。


\a.html)已安装):

``bash
$make test setup
```

或者可以使用'pip install-r test requirements.txt`(最好在新的virtualenv中使用)。


现在确保所有测试通过并在本地运行:

``bash
$make test
```

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

推荐PyPI第三方库


热门话题
java用arraylist中的单词替换txt文件中的单词?   java为ProgressBar提供了一个解决方案。是否接受整数?   java添加鼠标侦听器以删除目标   java删除标点符号的简单程序   java JavaScriptException:(TypeError):无法调用null的方法“getBoundingClientRect”   java我可以在没有对话框的情况下卸载应用程序吗?   执行泛型Java类   如果主线程从不调用线程,那么线程中断的原因是什么。中断()?   导致其他组件不显示的java绘制方法   java ObjectMapper追加文件JSON   Netbeans Java:将我的CSV文件放在哪里?   bouncycastle RSAKeyParameters中的java公钥   java为什么我总是在这个字符串数组中输入a1字符串?   java使用匿名绑定连接到AD并搜索用户DN   从JSP文件向Java应用程序发送socket?