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

bob gailer bgailer at gmail.com
Tue Nov 29 05:25:42 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 
> <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?

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))

Also see http://docs.python.org/library/json.html

>     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 <mailto:mayoadams at gmail.com>
>     _______________________________________________
>     Tutor maillist  - Tutor at python.org <mailto: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)
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Bob Gailer
Chapel Hill NC

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111128/ec73300b/attachment-0001.html>

More information about the Tutor mailing list