python函数执行shell_提供执行shell函数的Python API就像执行Python函数一样

shellfuncs

68747470733a2f2f7472617669732d63692e636f6d2f74696d6f6675727265722f7368656c6c66756e63732e7376673f746f6b656e3d7152634d7963694b597375455061706f46386e79266272616e63683d6d617374657268747470733a2f2f62616467652e667572792e696f2f70792f7368656c6c66756e63732e73766768747470733a2f2f696d672e736869656c64732e696f2f707970692f707976657273696f6e732f7368656c6c66756e63732e737667

Python API to execute functions written in shell script.

Let's assume you have a shell script counters.sh like this:

count_python_imports() {

find -name '*.py' | xargs grep -e '^import os$' -e '^import sys$' -e '^import re$' | cut -d: -f2 | sort | uniq -c

}

And you want to execute the count_python_imports function within Python. Instead of using cumbersome subprocess wouldn't it be awesome to do something like this:

import shellfuncs

from counters import count_python_imports

returncode, stdout, stderr = count_python_imports()

Yeah, yeah, I know about easier ways of achieving the above, too. Thanks.

Why should I use that?

use existing shell scripts in a pythonic way

complex piping stuff might be easier to implement in shell script

testing shell scripts is a pain in the a** - with Python it'll be easier

Installation

The recommended way to install shellfuncs is to use pip:

pip install shellfuncs

Usage

shellfuncs can be configured on different levels.

The following configuration variables are available:

shell (defaults to /bin/sh)

env (defaults to os.environ)

Configuration via environment variables

Set the default shell via SHELLFUNCS_DEFAULT_SHELL environment variable:

export SHELLFUNCS_DEFAULT_SHELL=/bin/bash

Configuration via context manager

Set the configuration block-wise with a context manager:

import shellfuncs

with shellfuncs.config(shell='/bin/bash'):

from counters import count_python_imports

count_python_imports() # the shell used will be /bin/bash

Configuration for specific function call

Set the configuration when function is executed:

import shellfuncs

from counters import count_python_imports

count_python_imports(shell='/bin/bash')

This project is published under MIT.

A Timo Furrer project.

-

🎉 -

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值