I Can Show You the World (but only if you console.log() your output)
If a program can be run as an executable, it can be “black boxed” into any other programming language.
This is useful to not have to manage an entire server stack to do a singular task suited to a particular language.
Here’s what a Python workflow looks like relying on a NodeJS function.
// JS
// some NodeJS function in index.js
function chunkDocument(document) {
const chunks = document.map(sentence => {
// do something with the document and put it into chunks
return sentence
})
return chunks
}
To invoke it in Python, run:
# PYTHON
import subprocess
import os
from dotenv import load_dotenv
load_dotenv()
p = subprocess.Popen([os.getenv('NODE_PATH'), os.getenv('JS_FILE') + '/index.js', '--document=' + document], stdout=subprocess.PIPE)
out = p.stdout.read()
decoded = out.decode("utf-8").strip()
print(decoded)
To capture an argument i.e.: `'--document=' + document`
// JS
// https://stackoverflow.com/questions/4351521/how-do-i-pass-command-line-arguments-to-a-node-js-program
const document = argv('document')
if (document && document.length > 0) {
const res = chunkDocument(document)
console.log(res)
}
That last `console.log()` is critical as it is what Python “sees” from the shell. Traditional `return` methods don’t work in between languages, unfortunately.
Other News
Today (1/28) is my 28th birthday. Last year, I made this. This year, I’m eating steak and having a salad. C’est la vie.
To me, wealth is being able to spend any discretionary amount of money to complete a project. Perhaps I’ve been eyeing too much hardware lately…
ars longa, vita brevis
Bram