Code Monkey home page Code Monkey logo

shshsh's Introduction

shshsh ๐Ÿโค๏ธ๐Ÿš

PyPI

Shshsh is a bridge connects python and shell.

  • A simple way to write shell commands in Python.
  • Flexibility.
  • Support for directly chaining a Python function in a pipeline.

Installation

To install shshsh with pip, run: pip install shshsh

To install shshsh with conda, run: conda install shshsh -c conda-forge

To install shshsh from source, clone the repository and run: pip install poetry;poetry install

โš ๏ธ If you are using python3.8: Be very careful with python function Pipe, there are known bugs!

Basic Usage

You can use I >> "[command]" or Sh("[command]")in any Python project.

Here's an example of getting all file which name contains "test":

from shshsh import I, IZ

for filename in I >> "ls" | "grep test":
    print(filename)

# zero mode, split line by "\x00"
for filename in IZ >> "ls" | "grep test":
    print(filename)

Also, you can safely pass parameter without worrying about command injection; shshsh will help you escape all bash control characters:

from shshsh import I
from sys import stdout

res = (I >> "echo #{}") % "dangerous; cat /etc/passwd" | stdout
res.wait()
# dangerous; cat /etc/passwd

To operate on the current working directory (cwd):

from shshsh.utils import cwd

# change dir
after_change_path = cwd("../../")

# to get current cwd, just don't give any parameter
cwd()

Python functions or iterables can be part of the chain. You no longer have to search Google (or chatgpt) repeatedly to write sed or awk ๐Ÿ˜‡:

from shshsh import I
from sys import stdout

# as map function
def add_suffix(line: str) -> str:
    return line + ".py"

res = I >> "ls -alh" | add_suffix | "grep test" | stdout
res.wait()

# as data source
def data_source():
    for i in range(10):
        yield f"test{i}"

res = I >> data_source() | "grep test1" | stdout
res.wait()

By default, stderr will directly redirect to current Python process's stderr.

But you can also keep its result using the redirect expr >= for stderr and > for stdout:

from shshsh import I, keep

res = I >> "ls not_exist" >= keep
res.wait()

print(res.stderr.read())

The redirect expression can redirect the stream to any kind of IO object:

from shshsh import I

with open("res", "w") as f:
    # redirect stdout to file.
    res = I >> "echo 123" > f
    # redirect stderr to file.
    res1 = I >> "ls not_exist" >= f
    res1.wait()
    res.wait()

shshsh's People

Contributors

levinotik avatar zqqqqz2000 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.