[Tutor] thesaurus

Pete Froslie froslie at gmail.com
Mon Jul 13 05:35:40 CEST 2009


>
> You don't seem to understand functions.  Part of the point is to *avoid*
> global variables.  If everything a function uses is passed in as arguments,
> and if everything it produces is returned as return value(s), it's much
> easier to see its effect.  And to make sure it doesn't have bugs.


True enough- that's why I seek advice. My trouble was with the bit of code I
was writing inline; while adapting to the functions I misunderstood their
value regarding global variables.  I'm happy to say the code is using
functions more appropriately now and that, as I learn, it will get closer to
something more respectable. I see the advantages for debugging

word_count = 0 #set up variable to increment through text
>
> def increment(x):
>
>>        return  x+1
>> increment(word_count)
>>
>>
>>
> This function doesn't have any effect on global variables.  It returns a
> value that's one more than its argument.  Period.  If you wanted to
> increment word_count using that function, you'd do something like:
>

Thanks-- that makes complete sense; it appears that I just was wrapping my
head around functions right away. Initially, I assumed this function could
be something quick for any increments. Rather than writing out: 'word_count
= word_count + 1' when needed..

I have no clue what this url() function is trying to do.  Nor how you expect
> to use it.  Are you planning to open this file for each word contained in
> it?


yes. Essentially, it grabs each word from a text file and combines it with
the other stuff to create a url string that I send through an API to an
online thesaurus. I was attempting to strip it this way as a weak method for
cleaning out the words I don't want searched from the text file.

Along with the the other functions the code currently scans a text file and
replaces each of its words with one of their synonyms.. slightly legible
gibberish, but that's what I'm interested in for now. It is a project to
help me start learning pyhton as well being intended to take a different
form in an artwork I'm working on.

thanks.
petef



On Sun, Jul 12, 2009 at 7:40 PM, Dave Angel <davea at ieee.org> wrote:

> Pete Froslie wrote:
>
>> so, I've been playing with the functions a bit-- took a little time to get
>> the hang of them after I started that other mess. Initially, I've done the
>> opposite suggested here by creating functions without the input values. I
>> do
>> see the where it will pay off in re-usability that route.. Alan, I'll read
>> your tutorial on modules and functions.
>>
>> Things that are arising for me now:
>>
>> (1)the use of global variables- seems it would be easier to communicate
>> between functions with 'I/O' values if I understood where the best
>> location
>> for declaration is..
>>
>>
>>
> You don't seem to understand functions.  Part of the point is to *avoid*
> global variables.  If everything a function uses is passed in as arguments,
> and if everything it produces is returned as return value(s), it's much
> easier to see its effect.  And to make sure it doesn't have bugs.
>
>> (2)not sure why this function doesn't work:
>>
>> word_count = 0 #set up variable to increment through text
>>
>> def increment(x):
>>
>>
>>>        return  x+1
>>>
>>> increment(word_count)
>>>
>>>
>>>
>> This function doesn't have any effect on global variables.  It returns a
> value that's one more than its argument.  Period.  If you wanted to
> increment word_count using that function, you'd do something like:
>
> word_count = increment(word_count)
>
>  (3) related to strings: Trying a function that will strip unwanted words
>> off
>> of the url before I bounce it off the website. For instance, I don't need
>> to
>> search for a, was, is... This is working, but with strange results-- often
>> it will find an 'a' in the middle of a word and replace it with text and
>> such.
>>
>> using rstrip, but with varying results:
>>
>> #this is the url as a string
>>
>>
>>> http://words.bighugelabs.com/api/2/e413f24701aa30b7d441ca43a64317be/A/
>>>
>>> thesaurus = string.rstrip(url(),"/A/") + '/' # stripping the end off and
>>> re-adding the '/'
>>>
>>>
>>>
>>
>> The url function btw:
>>
>> def url():
>>
>>
>>>    fin = open("journey_test.txt", "r")
>>>    response = re.split(r"[/|/,\n, , ,:\"\"\.?,)(\-\<>\[\]'\r']",
>>> fin.read())
>>>    thesaurus = API_URL + response[word_number] + '/'  #API_URL is
>>> established at the start of the code
>>>    return thesaurus
>>>
>>>
>>>
>>
>> Pete F
>>
>>
>>
>
> I have no clue what this url() function is trying to do.  Nor how you
> expect to use it.  Are you planning to open this file for each word
> contained in it?
>
> DaveA
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090712/00e6c6fc/attachment.htm>


More information about the Tutor mailing list