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

Dave Angel d at davea.name
Mon Nov 28 19:08:20 CET 2011


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>  wrote:
>
>> I am trying to pass a set of tuple strings from a file to a function I
That's an undefined term.  Python doesn't have a type called 'tuple 
strings'.
>> have defined.  Each tuple is on a separate line, and looks something
>> like this:
>>   ('note',2048)
>> The function has two parameters , and is defined thus: def
>> findindex(dval,ticks):
>> Apparently, I need to cast the second value as an integer in the body
>> of the function in order to work with it as such, but when I attempt
>> int(ticks) I get
>> ValueError: invalid literal for int() with base 10: '2048)'
>>
>> Upon searching for elucidation of this on the internet I find nothing
>> but esoterica, at least as far as I am concerned.
>> Any pointers would make me happy.
>>
>> --
>> Mayo Adams
>>
>>
>>
>> mayoadams at gmail.com
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> Your problem is in the error message:
>
> ValueError: invalid literal for int() with base 10: '2048)'
>
> observer, Python tells you this isn't a number: '2048)'
>
> Indeed, this is correct, because you '2048)' is not a number.
>
> What you really want to pass to '2048', which int('2048')
>
> can understand just fine.
>
> So, trim off the parenthesis, or something.
>
> Alternatively, since you aren't actually passing a "tuple" but something
> that looks like a python tuple as a string, you could eval it:
>
> a = "('note',2048)"
> b = eval(a)
>
> print a, type(a), b, type(b)
>
>>> ('note',2048)<type 'str'>  ('note', 2048)<type 'tuple'>
> In working with the second, you do normal tuple operations, like b[1] to
> access that index
>
> When passing to a function, you do this: findindex(*b)
>
My two-cents:

Avoid eval like the plague.   It's not a tool for the beginner, it's an 
escape hatch when all else fails.  So let's look at the first approach.

First comment is that the stuff in a text file is not treated in any way 
the same as the stuff in the source code.  Parentheses, quotes, and 
commas do not have any special meaning.

Whenever writing data to a file, if you expect to be able to read the 
same data back (as opposed to giving it to a human), you need to be 
careful about how you format it.  In effect, the writing and the reading 
have to be considered at the same time, and you have to decide just what 
constraints on the data, and what you'll do if the data doesn't meet 
those constraints.  Nothing magic about the parentheses, and I wouldn't 
have written them in the first place.

So what were the constraints on the original data?  If you're sure 
neither field can have any commas in it, then you could use line.split() 
with a comma as a delimiter.  And once you've split, you strip off the 
leading parenthesis on the zeroth item, and the trailing parens on the 
1th item.

At that point a simple int() function on the 1th item will convert the 
digit characters to an integer.

-- 

DaveA



More information about the Tutor mailing list