Python subprocess, but actually nice.
Project description
Zap
Python subprocess, but actually nice.
Before:
result = subprocess.run(["git", "status"], capture_output=True, text=True, check=True)
print(result.stdout)
After:
from zap import run
print(run("git status"))
Install
pip install zap-sh
Quick Start
from zap import run
# Run a command
result = run("git status")
print(result.stdout) # raw stdout
print(result.ok) # True if exit code 0
print(result.code) # exit code
print(result.lines) # stdout as list of lines
# Just print it — str() returns stdout
print(run("ls -la"))
Pipe Chaining
# Pipe with strings — clean and simple
result = run("cat logs.txt") | "grep ERROR" | "wc -l"
print(result) # number of error lines
Error Handling
from zap import run, ZapError
# Raises ZapError on non-zero exit (default)
try:
run("git push origin fake-branch")
except ZapError as e:
print(e.stderr)
print(e.code)
# Disable with check=False
result = run("might-fail", check=False)
if not result.ok:
print("Failed:", result.stderr)
Options
# Timeout (seconds)
run("sleep 100", timeout=5) # raises TimeoutError
# Working directory
run("ls", cwd="/tmp")
# Environment variables (merged with os.environ)
run("echo $API_KEY", env={"API_KEY": "secret"})
# Feed stdin
run("cat", stdin="hello from stdin")
# Safe mode — pass a list instead of string
run(["echo", "no shell injection"])
Async
from zap import run_async
result = await run_async("git status")
print(result.ok)
Same API as run() — just await it.
Result Object
| Property | Type | Description |
|---|---|---|
.stdout |
str |
Captured stdout |
.stderr |
str |
Captured stderr |
.code |
int |
Exit code |
.ok |
bool |
True if exit code is 0 |
.lines |
list[str] |
Stdout split into lines |
.json() |
Any |
Parse stdout as JSON |
str(r) |
str |
Returns stdout stripped |
bool(r) |
bool |
Returns .ok |
r | "cmd" |
Result |
Pipe stdout to next command |
How It Works
- String command → runs with
shell=True(convenience for scripting) - List command → runs with
shell=False(safe, no shell injection) - check=True by default → raises
ZapErroron non-zero exit - Zero dependencies → pure Python stdlib
- ~200 lines → read the whole source in 5 minutes
License
MIT
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
zap_sh-0.1.0.tar.gz
(6.1 kB
view details)
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 zap_sh-0.1.0.tar.gz.
File metadata
- Download URL: zap_sh-0.1.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5370f817de53c1c2ec4da9b33b247bc96a0d8f0d9b7ffec518e0be2a854b9827
|
|
| MD5 |
b27cfe4327cd8168e09cbb55f9777672
|
|
| BLAKE2b-256 |
e2cc02b458edc597f3615dc5bdc2010c5cf9bfdb4f4545cea58934f387a1b711
|
File details
Details for the file zap_sh-0.1.0-py3-none-any.whl.
File metadata
- Download URL: zap_sh-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ffd2b5af11d8da5b2da93e905f79a9e88ca80748482cf9ca74a3470c8124979
|
|
| MD5 |
d20ecdab5d95be065abdb3c923b5febb
|
|
| BLAKE2b-256 |
997ba8b638ac46111ad102540f5ad3c1ca57e40f7aa9a12e02396f8c8f06a8da
|