Skip to main content

scriptpy: Python for Scripting

scriptpy is a Python extension that simplifies scripting by seamlessly integrating shell commands and introducing a powerful piping syntax for working with lists and iterables. It aims to make live scripting easier.

Key Features

  • Shell Command Execution: Execute shell commands directly within your Python code using the $(...) syntax. The output of the command is captured as a string.
  • Piping for Lists/Iterables: Chain operations using the | operator to apply a function or attribute access to each element in a list or iterable.
  • Attribute Access Piping: Access attributes and call methods on elements in a list or iterable using the |.attribute syntax.

Syntax Sugar

TL;DR

  • $(command) Run a shell command and return its output as a string (subprocess.run(command).stdout.read()). Note: $(ls) will not work, as ls is not defined. Wrap it in a string or variable: $("ls").

  • left | right = [right(x) for x in left]

  • left |.right = [x.right for x in left]

  • left |.right(arg1, arg2) = [x.right(arg1, arg2) for x in left]

  • Auto-import of fully-qualified functions: for example, os.path.basename works without importing os (applies to all modules too)

Shell Command Execution

# Execute a shell command and capture the output
output = $("ls -l")
print(output)

Piping

The | operator applies the right-hand-side operation to each element in the list or iterable on the left-hand side. The right-hand side can be:

  • A function: The function is called with each element as its argument, and the result is a new list.
  • An attribute: Accesses the attribute of each element, resulting in a new list of those attributes.
  • A method call: Calls the method on each element with the given arguments, resulting in a list of the return values.

Designed for Interactive Use

scriptpy is designed for interactive use. Like the Python interpreter, it prints the last value of the line if there is one. For example, entering [1, 2, 3] will output [1, 2, 3], whereas a = [1, 2, 3] will not print anything.

If your code is not syntactically complete (e.g., ends with an open bracket {, (, or [), scriptpy will try to auto-complete it for you. For instance, entering [1, 2, 3 will still result in [1, 2, 3].

Installation

To install, use:

pip install scriptpy-syntax
# Thats the name on PyPI, still use `import scriptpy`, `$ scriptpy`

Usage

Use from the command line:

scriptpy '<code>'
scriptpy -s '<script-filename>'
cat anything | scriptpy -d- "<code>" # -d is for data file, -d - is to make the "data" var stdin.read().
# for example:
curl -s 'https://api.github.com/repos/matan-h/Transfer/commits'|scriptpy -d- '"\n".join(json.loads(data) | .get("commit") | .get("message"))' # print all commits from Transfer.

if you want a more complete and interactive way to use this library check out my project f7, which is a GUI to manipulate your selection. (you select, press f7 key, then enter a scriptpy expression to change that selection.), also it supports many features such as prefixes and modes this originally was built to use in f7.

Usage as a python library

from scriptpy import custom_eval
# custom_eval(src: str, globals_: dict | None = None,verbose=False)
res:list = custom_eval(
  src='numbers | str |.zfill(3)',
  globals_={"numbers":list(range(40))},verbose=False)

print(res) # [000,001,002,...]

Contributing

Contributions are welcome! If you'd like to suggest a feature, report a bug or an error, or propose any improvements, please open an issue.

If you'd like to support the project, you can buy me a coffee ☕️. Your support helps keep development going and is greatly appreciated.

Metadata

Release files for scriptpy-syntax 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scriptpy-syntax 0.1.1
File Size Uploaded
scriptpy_syntax-0.1.1.tar.gz 11.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scriptpy-syntax 0.1.1
File Interpreter ABI Platform
scriptpy_syntax-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 25.3 kB

Release files / scriptpy_syntax-0.1.1.tar.gz

Download URL scriptpy_syntax-0.1.1.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
abe22f674efab71c608eaebeb46f4f9ae713ea2d3a98c9c940a8a077c5b77bd7
BLAKE2b-256 checksum
How to use checksums
6e49b8b493b78d6a69defeec8599d3b0f99576ea1e676c20a2eb199823076e6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.13.3 Linux/6.14.11-300.fc42.x86_64

Release files / scriptpy_syntax-0.1.1-py3-none-any.whl

Download URL scriptpy_syntax-0.1.1-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eeba3c1d4cf15b25cb082ef734fbd395fb337ec62dc67e8cc77bd3869277d873
BLAKE2b-256 checksum
How to use checksums
96539fa10bf5bf3b248135a7a6292e79698b62f61effede505138c32c3a5056f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.13.3 Linux/6.14.11-300.fc42.x86_64

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page