ZXPY – Shell scripts made simple

Zxpy is a tool that simplifies the integration of shell commands into Python, making your scripting experience smoother and more efficient.

Installation

pip install zxpy

If you have pipx installed, you can try out zxpy without installing it, by running:

pipx run zxpy

If you have an Arch-like distro you can as well use AUR

❯ yay -Ss zxpy

aur/zxpy 1.6.1-1 (+0 0.00)  
   Shell scripts made simple 

❯ yay -S zxpy

Basic Usage

❯ nvim script.py
❯ chmod +x script.py
❯ ./script.py

Examples

Running a Basic Shell Command

With zxpy, you can seamlessly run shell commands directly in your Python script. Take a look at this:

#!/usr/bin/env zxpy

# Example 1: Running a Basic Shell Command
~'echo "Hello, zxpy!"'

In this example, the echo command is executed just like you would in the terminal, but within your Python script.

Example 1: Running a Basic Shell Command  
Hello, zxpy!

Using Variables in Shell Commands

Now, let’s up the ante. Ever wanted to capture the output of a shell command and use it in your Python code? zxpy makes it a breeze:

#!/usr/bin/env zxpy  

# Example 2: Using Variables in Shell Commands 
file_count = ~'ls -1 | wc -l' 
print("Number of files in the current directory:", file_count)`

In this snippet, the output of the ls -1 | wc -l command is stored in the file_count variable, bringing the power of shell commands into your Python code.

Example 2: Using Variables in Shell Commands  
Number of files in the current directory: 3

Finding Files with a Specific Extension

Searching for files is a common task in scripting. With zxpy, this task becomes concise and clear:

#!/usr/bin/env zxpy

# Example 3: Finding Files with a Specific Extension
python_files = ~'find . -name "*.py"'
print("Python files in the current directory:")
print(python_files)

This example demonstrates how to use the find command to locate Python files (*.py) in the current directory effortlessly.

Example 3: Finding Files with a Specific Extension  
Python files in the current directory:  
./run_all_tests.py  
./script.py  
./example.py

Using Python Variables in Shell Commands

Bringing Python variables into your shell commands? Yes, please! Check out how easy it is:

#!/usr/bin/env zxpy

# Example 4: Using Python Variables in Shell Commands
search_term = "TODO"
todo_comments = ~f'grep -r "{search_term}" .'
print(f"Occurrences of '{search_term}' in files:")
print(todo_comments)

Now, you can seamlessly use Python variables within your shell commands, making your scripts more dynamic and adaptable.

Example 3: Finding Files with a Specific Extension  
Python files in the current directory:  
./run_all_tests.py  
./script.py  
./example.py

Ready to explore more? Check out the official zxpy GitHub repository for additional documentation and examples. Happy scripting!