Bash to Python Function Mapping: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(One intermediate revision by the same user not shown) | |||
Line 2: | Line 2: | ||
* [[Calling_Python_from_bash#Overview|Calling Python from bash]] | * [[Calling_Python_from_bash#Overview|Calling Python from bash]] | ||
=Overview= | =Overview= | ||
This article describes a method to map bash functions to Python functions. The method is somewhat awkward, and a much better idea | This article describes a method to map bash functions to Python functions. The method is somewhat awkward, and a much better idea would be to write the entire logic in Python, and then call the Python program from a thin bash wrapper, as described here [[Calling_Python_from_bash#Running_a_Python_Program_with_a_Bash_Wrapper|Running a Python Program with a Bash Wrapper]]. | ||
=Solution= | =Solution= | ||
Given an external module <code>my_module.py</code> with the following content: | Given an external module <code>my_module.py</code> with the following content: |
Latest revision as of 16:55, 2 May 2023
Internal
Overview
This article describes a method to map bash functions to Python functions. The method is somewhat awkward, and a much better idea would be to write the entire logic in Python, and then call the Python program from a thin bash wrapper, as described here Running a Python Program with a Bash Wrapper.
Solution
Given an external module my_module.py
with the following content:
def my_function(arg1, arg2, arg3):
print('this is my_function(' + arg1 + ", " + arg2 + ", " + arg3 + ")")
the module can be called generically from a bash script as follows:
#!/usr/bin/env bash
function call-python() {
local python_interpreter=$1
local module_path=$2
local function_name=$3
[[ -z ${python_interpreter} ]] && fail "'python_interpreter' not provided"
[[ -z ${module_path} ]] && fail "'module_path' not provided"
[[ -z ${function_name} ]] && fail "'function_name' not provided"
shift 3
local args
for i in "${@}"; do
[[ -z ${args} ]] && args="'${i}'" || args="${args}, '${i}'"
done
local module_name
module_name=$(basename "${module_path}" .py)
# instead of PATH python3 you may want to use the interpreter from a specific virtual environment
(PYTHONPATH="$(dirname ${module_path})"; export PYTHONPATH; ${python_interpreter} <<EOF
import ${module_name}
${module_name}.${function_name}(${args})
EOF
)
}
call-python ./my_module.py my_function blue red green
The output will be:
this is my_function(blue, red, green)
⚠️ Numbers will be converted to strings before being passed to the function, the implementation is incomplete.