[Tutor] pass tuples to user defined function(beginner)

Dave Angel d at davea.name
Tue Nov 29 06:07:16 CET 2011


On 11/28/2011 11:25 PM, bob gailer wrote:
> On 11/28/2011 12:47 PM, James Reynolds wrote:
>>
>>
>> On Mon, Nov 28, 2011 at 12:32 PM, Mayo Adams <mayoadams at gmail.com 
>> <mailto:mayoadams at gmail.com>> wrote:
>>
>>     I am trying to pass a set of tuple strings from a file to a 
>> function I
>>     have defined.  Each tuple is on a separate line, and looks something
>>     like this:
>>      ('note',2048)
>>
>
> As already pointed out - this is a string (a representation of a 
> tuple), not a tuple.
>
> Your code must parse the string to extract the string representations 
> of the values, then convert as needed to the desired Python values.
>
> Tasks like this are not trivial.
>
> Using eval as already noted is the easiest but not the best solution.
>
> Perhaps we should back up and ask the overall objective of the 
> program. Where did this file come from? Could we store the data in 
> some other (easier to parse) manner?
>
This file came from the OP's other thread, "Re: [Tutor] write list of 
tuples to file (beginner)"  At that point, he didn't say what the file 
was for.
> If each line will always contain a string literal and an integer 
> literal then the simplest encoding is:
> note 2048
>
> the program then is:
>
> for line in file(path):
>   val1, val2 = line.split)
>   findindex(val1, int(val2))
>
Well, this also assumes the string has no whitespace in its internals.

As i said in my last response, the first thing that's needed is a 
complete spec on what these arguments might be.  We have an example of 
1, that happens not to have any quotes, commas, spaces or anything else 
interesting in the string.  And the number happens to be an integer.  
can we assume much?  Not really.

-- 

DaveA



More information about the Tutor mailing list