Python for shell scripting
Project description
Harness the full power of Python in your shell scripts and on the command line.
What is pyl?
pyl is a command line tool that let’s you write terse but readable shell one-liners using Python. It does this by introducing a few powerful syntax extensions, described below.
Why?
Python has a super extensive standard library, making it the ultimate shell-scripting toolbox. The problem is that, while very concise compared to other languages, running Python snippets from within shell scripts is quite clunky. Consider the following code that base64-encodes each line of input:
$ ls | python -c $'import base64, sys\nfor l in sys.stdin: print(base64.b64encode(l.encode()).decode())'
TElDRU5TRQo=
UkVBRE1FLnJzdAo=
cHlwcm9qZWN0LnRvbWwK
c3JjCg==
The biggest problem with inline Python is whitespace: Python requires newlines and indentation, and it’s not convenient to include those in strings on the command line.
With pyl, the above command becomes:
$ ls | pyl 'for l in sys::stdin: { base64::b64encode(l.encode()).decode() }'
TElDRU5TRQo=
UkVBRE1FLnJzdAo=
cHlwcm9qZWN0LnRvbWwK
c3JjCg==
The Solution
pyl introduces a number of syntax extensions that, together, make it easy to write readable shell one-lines that take full advantage of everything Python has to offer:
- Braces
Curly braces that appear immediately after a colon become indentation.
Example:
for i in range(3): { for j in range(4): { print(i * j); } }is equivalent to
for i in range(3): for j in range(4): print(i * j)- Line splitting at ;
Lines are split at semicolons.
In normal Python, statements like if and for need to appear on their own line. with pyl you can just use a semicolon:
Example:
import sys; for arg in sys.argv: { print(arg); }is equivalent to
import sys for arg in sys.argv: print(arg)- Implicit print
If a block (either top-level or indented) ends without a semicolon, the last line is automatically printed (unless the block ends in another block).
In fact, the whole line is passed as-is to print, so you can pass keyword arguments like sep.
Example:
for i in range(10): { i, end='.' }is equivalent to
for i in range(10): print(i, end='.')- Inline-import
The :: operator can be used to access members of modules without explicitly importing them.
Example:
urllib.parse::quote('hello world')is equivalent to
print(__import__('urllib.parse').parse.quote('hello world'))- Environment variables
Environment variables can be accessed with $NAME.
Example:
$HOMEis equivalent to
print(__import__('os').environ['HOME'])- Command line arguments
Command line arguments can be accessed with $INDEX where INDEX is a number.
Example:
$1is equivalent to
print(__import__('sys').argv[1])
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_pyl-0.1.0.tar.gz.
File metadata
- Download URL: python_pyl-0.1.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c34e493a049c4df5457e9918fbd403f4ee8f3880409cd4cd38156695d4b8990
|
|
| MD5 |
80e857f9e9e076cf957700011fb6d5ee
|
|
| BLAKE2b-256 |
ac12debb1fdc9de9227992c08e70fb31ef3e9fbe1a98944eb7a6957d9f48e263
|
Provenance
The following attestation bundles were made for python_pyl-0.1.0.tar.gz:
Publisher:
release.yml on BenjyWiener/pyl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_pyl-0.1.0.tar.gz -
Subject digest:
9c34e493a049c4df5457e9918fbd403f4ee8f3880409cd4cd38156695d4b8990 - Sigstore transparency entry: 193207824
- Sigstore integration time:
-
Permalink:
BenjyWiener/pyl@588aa1d134060e6d004c969dd52724d8e929964d -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/BenjyWiener
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@588aa1d134060e6d004c969dd52724d8e929964d -
Trigger Event:
push
-
Statement type:
File details
Details for the file python_pyl-0.1.0-py3-none-any.whl.
File metadata
- Download URL: python_pyl-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80e19cb05b7043e4d692439397a496c43ee2f62cf0601150c82bdf26c936bfb4
|
|
| MD5 |
93456128722cae273ec974397f8cbe8b
|
|
| BLAKE2b-256 |
ea9dc3037b32411e29a384d630bdd0c3d03711cf76408858fef08777fcca27e8
|
Provenance
The following attestation bundles were made for python_pyl-0.1.0-py3-none-any.whl:
Publisher:
release.yml on BenjyWiener/pyl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_pyl-0.1.0-py3-none-any.whl -
Subject digest:
80e19cb05b7043e4d692439397a496c43ee2f62cf0601150c82bdf26c936bfb4 - Sigstore transparency entry: 193207826
- Sigstore integration time:
-
Permalink:
BenjyWiener/pyl@588aa1d134060e6d004c969dd52724d8e929964d -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/BenjyWiener
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@588aa1d134060e6d004c969dd52724d8e929964d -
Trigger Event:
push
-
Statement type: