Buttons840
Buttons840

Reputation: 9637

Design help: Configurable behavior based on input

I am working in Python, but this is a general design question so general answers are welcome. I will explain the context not as part of the question, but to serve as an example:

I have a script which receives a CSV file, it uses the fields in this file to make automated phone calls. The fields represent names to be spoken, dates to be spoken, and the phone numbers to call. For example, input like "555-555-4321,Bob,Jill,3/30/2011" a phone call might be placed to 555-555-4321 and a robotic message delivered saying "Bob, don't forget Jills birthday is next Wednesday, one week from now."

My question is what design patterns would be useful for making this system configurable? More specifically, I'd like to specify what the format the input lines take, and some behaviors for generating the voice message. Some fields, like "Bob," can be as simple as "speak the field." Other fields, like the date, require some transformation in order to be spoken (ie, how does "3/30/2011" become "next Wednesday"). I'd also like to have various line formats, for example, input such as "555-555-4321,Bob,6:00" might call Bob every day at 6:00 and say "wake up!"

My goal is to have a web interface which allows defining and configuring these types of things. I know how to solve these problems by hacking my source code, but hacking the source code is a long ways from a simple and user friendly front-end.

Upvotes: 0

Views: 121

Answers (1)

Erik Johnson
Erik Johnson

Reputation: 1164

I'm solving a related but not identical problem currently.

My solution is to create a control list of the same length as the target csv lines, where each element in the control list is the name of a useMethod. In my case the useMethod is an editor widget; in your case, it would be a function that defines how the field is interpreted by your text-to-speech engine. For each line, you can then iterate over the fields, calling the appropriate processing widget.

So for your example of "555-555-4321,Bob,Jill,3/30/2011",

import csv
def phoneNumber(number):
    ...
def userName(name):
    ...
def targetDate(datestring):
    ...
control = [phoneNumber, userName, userName, targetDate]
with open("csvFile", "r") as inFile:
    reader = csv.reader(inFile)
    for row in reader:
        for op, item in zip(control, row):
            op(item)

I note that this only works if the csv file has constant interpretation per element, but if it has variant interpretation then a csv file is the wrong storage method. I also note that you'd need some other control object to generate the rest of the sentence; this is left as an exercise for the reader. :)

This allows you to have a library of interpreter functions that can be assigned to fields in the csv file by simply changing the control string. A new control string would invoke a different order of field interpretations with no need to change the source code, and the new string could be entered on command line, stored in the first line of the csv file, or brought in some other way.

edit: And noting your addendum on using a web interface to configure, that would be a straight-forward way to provide a new control list.

Upvotes: 1

Related Questions