Running Jobs with Multiple Input Files

Let's create a job with multiple input files. Each input file will trigger an execution of the script. The path to the input file is passed to the script as a command line argument insys.argv[1] .

Create a Python file

This script solves the Spelling Bee puzzle from the New York Times puzzle section. It reads a file that has a line for each puzzle, then writes all the words it can find following the rules of the game. Save this in a file called spellingbee.py:

spellingbee.py
"""
Solve a Spelling Bee puzzle.
"""
from pathlib import Path
import platform
import requests
import sys
def get_allowed_words(to_lower=True):
"""Return a list of words to use as allowable words."""
try:
words = get_allowed_words_from_filesystem(to_lower=to_lower)
except FileNotFoundError as e:
print('Unable to find a dictionary on the file system. Trying online dictionaries.')
words = get_allowed_words_from_web(to_lower=to_lower)
return words
def get_allowed_words_from_filesystem(to_lower=True):
filepaths = [
Path('/usr/dict/words'),
Path('/usr/share/dict/words'),
]
word_filepath = None
for filepath in filepaths:
print('trying {}'.format(filepath))
if filepath.exists():
print('found word list: {}'.format(filepath))
word_filepath = filepath
break
if word_filepath is None:
raise FileNotFoundError('Unable to find a word list on this platform')
with word_filepath.open(mode='r') as f:
words = f.readlines()
if to_lower is True:
words = [word.strip().lower() for word in words]
return words
def get_allowed_words_from_web(to_lower=True):
url = 'https://users.cs.duke.edu/~ola/ap/linuxwords'
print('trying {}'.format(url))
r = requests.get(url)
words = r.content.decode('utf-8').split()
if to_lower is True:
words = [word.strip().lower() for word in words]
return words
def get_matching_words(center_letter, other_letters, min_word_length=5, allowed_words=None):
# if allowed_words is None:
# allowed_words = get_allowed_words()
words = []
allowed_letters = set(center_letter).union(set(other_letters))
for word in allowed_words:
if len(word) < min_word_length:
continue
if center_letter not in word:
continue
if set(word).difference(allowed_letters):
# the word contains letters other than the allowed letters
continue
words.append(word)
return words
def main():
data_file = Path(sys.argv[1])
allowed_words = get_allowed_words()
with data_file.open(mode='r') as f:
for line in f.readlines():
letters = line.strip().split(',')
center_letter = letters[0]
other_letters = letters[1:]
matching_words = get_matching_words(center_letter, other_letters, allowed_words=allowed_words)
print(center_letter, other_letters, len(matching_words), matching_words)
if __name__ == '__main__':
main()

Create a few data files.

Save each in a separate file.

puzzle-0.txt
o,r,l,v,h,n,a
a,t,c,p,i,l,e
s,c,a,o,w,e,n
g,s,w,i,o,a,d
r,o,l,d,c,h,a
s,e,i,o,n,r,u
u,b,a,h,o,t,i
w,d,v,y,h,s,a
e,i,w,t,n,o,h
c,e,i,a,p,t,g
puzzle-1.txt
v,n,e,o,a,r,i
a,n,e,u,r,h,o
a,n,m,r,i,y,o
o,e,d,r,f,s,n
r,a,t,b,o,i,e
n,r,t,s,p,o,e
e,n,y,w,o,l,k
h,a,o,r,n,s,c
t,e,v,d,m,s,a
t,u,c,m,i,o,a
puzzle-2.txt
h,u,i,l,n,w,c
t,a,p,w,d,m,l
u,t,r,e,s,l,o
e,y,c,i,m,r,o
p,i,e,a,c,h,l
h,m,a,o,g,d,f
k,i,p,a,w,f,t
a,r,d,h,e,s,n
t,r,o,l,u,a,i
f,l,n,r,y,a,s

Create a new job

Next, create a new job and ensure you include both script and input files. Then run the job. In the event of a failure, check both stdout and stderr tabs for any hints on what failed.