Skip to main content

A bunch of shortcuts and import magic to simplify scripting in python

Project description

Warning

pysh-stub is deprecated and will be deleted. Please, use pysh instead.

Pysh

Bash is said to be the opposite of riding the bike -- you have to re-learn it every time. So if we're doing anything more complex than running a few commands, chances are google will be needed. But if we switch to python, then the reverse is true -- python is a bit too cumbersome for simple scripts. I try to alleviate this problem by making it easier to use bash from python.

It was inspired by Jupyter's way of handling bash.

Installation

pip install pysh-stub

Quickstart

  • If you have a hello_world.pysh file with the contents below, you can run it using pysh hello_world.pysh:
!echo Hello World
print("Hello World")
  • Most bash is going to be one-to-one translatable to pysh by putting exclamation marks at the beginning of each line:
!ls
!cp some_file some/place
  • It has most of the variables ($@, $#, $1, $2, etc) you would expect:
!echo $*
  • You can also access python variables from bash using f-string notation
my_file = "~/some/file" + ".txt"
!cp {my_file} {my_file + ".bak"}
  • If you wish to get the output of any command into a python variable, you can use double exclamation mark:
# Note that the default output of ls is silenced when !! are used
lines = !!ls -lA
for line in lines.splitlines():
    print(line)

Builtins

I consider the most comfortable way to write python scripts is to import os, pathlib, sys, and re first. Thus, all of these modules are pre-imported. However, instead of "pathlib.Path", we have "P".

I also import typer as I consider it to be the most concise way to write any command line application.

If you wish to customize how you call bash subprocesses, you can use the builtin "sh" function. It supports all the same arguments as subprocess.run but has text=True and shell=True by default.

Magical Commands

We use subprocess.run to run bash commands but that has a few quirks. If you modify the process info in any way within the subprocess (cd, set, unset, etc), this information will not change for the currently running process. Hence we have a few magical commands that do not create a subprocess but instead use roughly equivalent python constructs.

Here's a list of all the magical commands and the approximate conversions we apply to them:

Original Converted
!cd arg os.chdir(arg)
!set -e __pysh_check_returncodes__.set(True)
!set +e __pysh_check_returncodes__.set(False)
!exit arg sys.exit(int(arg))

Notes on syntax

  • Each bash call is parsed until the end of the line so they are closer to statements than expressions. Hence the following is not possible:
print(!!ls)
  • Because we use f-strings to interface between python and bash, you will have to escape non-formatting "{" and "}" by typing them twice. You will also have to wrap these characters in quotes because prior to converting bash into executable format, we tokenize the entire file as if it was valid python. I.e. Unclosed braces, parenthesis, etc will cause a tokenize.TokenError.
# The invalid ways to do it
!echo {
!echo {{

# The valid way to do it
!echo "{{"

FAQ

  • If you have any questions, encounter any bugs or want to suggest/contribute new features, please, use the issues in the github repository

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysh-stub-1.1.4.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

pysh_stub-1.1.4-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file pysh-stub-1.1.4.tar.gz.

File metadata

  • Download URL: pysh-stub-1.1.4.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.4 Linux/5.10.114-1-MANJARO

File hashes

Hashes for pysh-stub-1.1.4.tar.gz
Algorithm Hash digest
SHA256 6a1bdb1ac2c1c8a613330e6631f79f0f03da219ed22fcbe048e95c90d972afd5
MD5 605f556231322ebaff3a293aee12012b
BLAKE2b-256 96b24efb054e393f5713d7c8fe074f56ea86adc55eefd95502a1f74ecb0172bd

See more details on using hashes here.

File details

Details for the file pysh_stub-1.1.4-py3-none-any.whl.

File metadata

  • Download URL: pysh_stub-1.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.4 Linux/5.10.114-1-MANJARO

File hashes

Hashes for pysh_stub-1.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4a3fb051abb4d053d97b11488475fc3a9dd768371657ca4a4184d5f024544369
MD5 f4f7e06f1d8e4f7aff13dfaabd9b40be
BLAKE2b-256 790d56d78e441dd1855ac88ceb3634fbb9a823ecb22eb3d4f4185015d0b3baac

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page