How would someone put multiline "script" in switch? #138
Answered
by
nat-n
mgzenitech
asked this question in
Q&A
|
Right now, I have this nasty expression: control.expr = """
pathlib.Path(".venv/bin/npm").is_file() and set(
pathlib.Path("node-requirements.txt").read_text().split("\\n")[0:-1]).intersection(set(
re.findall(r"[a-z-]+@(?:[0-9]+\\.?)+", subprocess.run(["npm", "ls", "-g"], capture_output = True) \
.stdout.decode("utf-8")
))
)
"""
control.imports = ["re", "subprocess", "pathlib"]Is it possible to write multi statement "script" (something using variables, exception handling, etc)? |
Answered by
nat-n
Apr 20, 2023
Replies: 1 comment 3 replies
|
The point of the expr task is to streamline the use-case where you want to evaluate a single python expression. For the most powerful option would be to use a script task to call a python function defined somewhere in your project (I usually put stuff in src/tasks.py, which doesn't have to be included in the build). In general you can define a shell with |
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Accessing arguments as python variables only works for
scriptorexprtasks, but shell tasks using python as an interpreter aren't as clever because they work by passing a whole script to the python interpreter, rather than composing one dynamically from the given components.For shell tasks you can access the arg value as an environment variable like so:
Notice I avoided calling the variable
path…