[Tutor] thesaurus

Dave Angel davea at ieee.org
Sun Jul 12 02:17:53 CEST 2009


Pete Froslie wrote:
> the trailing comma is getting me much closer right away! -- will read about
> the other options you suggest (rstrip(); stdout: write()) I clearly have a
> few other issues I need to sort out now. i guess I've been assuming that
> print was a good way to trace what is happening rather than thinking about
> its results in the external file as well..
>
> As for the inline: simply put, I'm an artist trying to learn how to code for
> my work, so my decisions are not altogether as informed as they could be.
> Thanks for taking the time to make that suggestion.. I will try to rework it
> to make use of functions. Then I will address the next set of issues..
>
> This seems the basic form for a function:
>
> *def** hello*():
>   
>>     *print* "Hello World!"
>>     *return*
>>
>> I assume each part that can be separated will be done so in this format at
>>     
> the start of the code; consequently, allowing me to call 'hello' later when
> I need it. Does this also mean that I will be able to call those functions
> separately later when I import 'thesaurus.py' into a new code also?
>
> Pete F
>
>   
>>     
That's exactly right, although you probably want to include arguments 
and return values to really understand how a function would work.

Part of the idea of a function is to isolate one set of functionality.  
That can be for at least three reasons:  1) there's more than one 
implementation, and you want to be able to easily switch between them.  
2) More than one caller might want to use that functionality.  3) it's 
easier to write, comment, and test a small piece of code with 
well-defined interfaces, than a whole program.

I'm assuming you do this inside a source file, a script.  And yes, you 
can then import the module and reuse much of it from other scripts.


There are lots of built-in functions (and methods, which are similar in 
concept) that you're already using.  There, the main advantage to you is 
that you can re-use something that was written for generality, with the 
assumption that it's been adequately tested.


So the question becomes how do you factor this program into pieces.  
There are usually several choices, with tradeoffs, and usually the 
"correct" one depends on how you expect things to change over time.  For 
the present purposes, I think the biggest change might be how you test 
it.  So try this:

1) a function that given a word, produces a synonym.  For testing 
purposes, you can hard-code it with a simple table.  That way you're not 
hammering on that website, maybe exceeding your daily quota for 
lookups.  Also the table will be faster.  So you write two of these 
functions, with identical interfaces, and similar names, and quickly 
choose between them.

2) a function that reads a file, modifies parts of it (by calling the 
first function), and writes the file back.  Again, you might have 
multiple versions.  For example, the simplest version of it simply 
prints the results instead of actually writing the file.  Or maybe it 
deliberately writes to a different file.  So you can rerun the program 
without having to separately copy the original version back on top.

3) a test harness of some sort.

def synomym1(word):
    """ return synonym of the word """
    table={"dog":"mammal", "person":"human"}
    if word in table:
        return table[word]
    return word

def synonym2(name):
       --- do some stuff with the web lookup ---
      return result


def processfile(filename, lookup):
       --- loop through file, looking up each word
                newword = lookup(word)
                --- write the translated word out, to stdout, or to a 
new file, or whatever



if __name__ == "__main__":
     processfile("myfile.txt", synonym1)    #typically, the filename 
will come from sys.argv.  You might even use argv to decide which of the 
synonym functions to use.


Now when you want to switch to using the website, you just change the 
last line in the file to use the other function.

Note that this is not the organization you used.  I'm assuming here that 
you really want to translate all the words, not just a single word per 
pass.  But hopefully this gives you enough clues to get you going.

DaveA



More information about the Tutor mailing list