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.
- 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.
-
$(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)
# Execute a shell command and capture the output
output = $("ls -l")
print(output)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.
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].
To install, use:
pip install scriptpy-syntax
# Thats the name on PyPI, still use `import scriptpy`, `$ scriptpy`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.
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,...]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.