Python syntax extensions to make live scripts easier..
Project description
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
|.attributesyntax.
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, aslsis 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.basenameworks without importingos(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.
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 scriptpy_syntax-0.1.1.tar.gz.
File metadata
- Download URL: scriptpy_syntax-0.1.1.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.14.11-300.fc42.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abe22f674efab71c608eaebeb46f4f9ae713ea2d3a98c9c940a8a077c5b77bd7
|
|
| MD5 |
367a565a2ca752f894536bf737db3e83
|
|
| BLAKE2b-256 |
6e49b8b493b78d6a69defeec8599d3b0f99576ea1e676c20a2eb199823076e6b
|
File details
Details for the file scriptpy_syntax-0.1.1-py3-none-any.whl.
File metadata
- Download URL: scriptpy_syntax-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.14.11-300.fc42.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eeba3c1d4cf15b25cb082ef734fbd395fb337ec62dc67e8cc77bd3869277d873
|
|
| MD5 |
d968f1ca9edeefb89ab7a1c6e9c2298f
|
|
| BLAKE2b-256 |
96539fa10bf5bf3b248135a7a6292e79698b62f61effede505138c32c3a5056f
|