Shell scripting... but in glorious Python!
Project description
Pyash
Hate shell scripting? Love Python! Meet the horrible Frankenstein of the two!
Pyash takes the best part of shell scripting, the ability to stream data through great chains of shell programs, and adds that ability to Python. In the process it retains the same familiar syntax but in a more Pythonic form!
from pyash import cat, grep
cat(".gitignore") | grep("env") > "out.txt"
Importing Shell Programs
Importing shell programs is as easy as
from pyash import grep, find, bash
Pyash automatically searches your PATH
variable and loads any executable programs it finds, making them available through import.
Try to avoid importing *
as you'll find basic builtin functions like print()
suddenly don't work!
Running Shell Programs
Once you've imported the programs you want to import you can use them as you would any normal Python function splitting arguments or keeping them all together as you want:
print(grep("-i", "-r", "env", "."))
print(grep("-i -r env ."))
It's important to note that all programs are lazy evaluated, i.e. a program only runs when Pyash knows where data is going.
For example, the above grep
commands will only execute when you print their output.
That means that if you don't care about the output you'll need to make sure you call run()
to make the program execute:
kubectl("delete", "pod", "my-pod").run()
Piping Data
Data can be piped between, to and from files using the same syntax you use in shell scripts:
print(kubectl("get", "pods") | grep("postgres"))
echo("I'm a pretty butterfly") > butterfly.txt
echo("Flutter! Flutter! Flutter!") >> butterfly.txt
(docker("container", "stop") < "my_containers.txt").run()
Note that when you start piping a program's output somewhere else it begins execution, so the 2nd and 3rd examples don't need you to call run()
.
If you need to both pipe in from a file and out from a file you need to place parentheses around the pipe in, this just seems to be a limitation of our abuse of Python.
(cat() | grep("env") < ".gitignore") > "myfile.txt"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyash-0.1.0.tar.gz
.
File metadata
- Download URL: pyash-0.1.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d512e064940eac34d00b92012a9f7ec6a5950e7053b6d9e86ee4c6673f078d4 |
|
MD5 | 3a45ba27d83cf6f091f2344b1275c2aa |
|
BLAKE2b-256 | 6e220ac129c99c8608479fae2159541261e62a0d97d9952dc1cdffcb6b8f56f5 |
File details
Details for the file pyash-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: pyash-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aaab0d1dcd55619aec431ad0cbb7d8c5c3836c5b21c13fd2fcc608981ab4325e |
|
MD5 | e361d4b2131861b038d1cfda8a528610 |
|
BLAKE2b-256 | e8ee15efafa38d14faaf07a94efbca1226022aff68b851556ef2cb386583725d |