Accessing List of Tuples

5,312 views
Skip to first unread message

Franz Allan Valencia See

unread,
Nov 23, 2010, 11:34:51 AM11/23/10
to robotframe...@googlegroups.com
Good day,

I just hacked out a simple Testing Library for robot framework.However, my keyword returns a list of tuples....How do I access a specific entry in that list of tuples?

For example, I can have something like [['hard', 'coded'], ['values', 'here']] stored in @{listOfTuples}
However, when I do a `Log @{listOfTuples}[0][0]`, instead of getting "hard", I am getting "['hard', 'coded'][0]". This then leaves me to believe that nested indexing is not allowed.

Any ideas on how to do this in RobotFramework?

Thanks,
--
Franz Allan Valencia See | Java Software Engineer
fran...@gmail.com
LinkedIn: http://www.linkedin.com/in/franzsee
Twitter: http://www.twitter.com/franz_see

Janne Härkönen

unread,
Nov 23, 2010, 11:56:13 PM11/23/10
to fran...@gmail.com, robotframe...@googlegroups.com
On Tue, Nov 23, 2010 at 6:34 PM, Franz Allan Valencia See
<fran...@gmail.com> wrote:
> Good day,
>
> I just hacked out a simple Testing Library for robot framework.However, my
> keyword returns a list of tuples....How do I access a specific entry in that
> list of tuples?
>
> For example, I can have something like [['hard', 'coded'], ['values',
> 'here']] stored in @{listOfTuples}
> However, when I do a `Log @{listOfTuples}[0][0]`, instead of getting "hard",
> I am getting "['hard', 'coded'][0]". This then leaves me to believe that
> nested indexing is not allowed.

Hello Franz,

this is a quirk in the way RF variables work. List (@) variables only
support one level of indexing, however there are two ways to utilize
the extended variable syntax[1] to access specific tuple in your
example:

1) You can use set variable keyword like this:

| @{tuples}= | Some keyword that returns list of tuples |
| ${elem}= | Set Variable | @{tuples}[0]
| Log Many | ${elem[0]} | ${elem[1]}

2) From the get go, assign the whole return value as a scalar var:

| ${tuples}= | Some keyword that returns list of tuples |
| Log Many | ${tuples[0][0]} | ${tuples[0][1]}

Note that in the latter example, both the indexing actions are before
the closing '}'.

[1] http://robotframework.googlecode.com/svn/tags/robotframework-2.5.4/doc/userguide/RobotFrameworkUserGuide.html#extended-variable-syntax

hope this helps,
__j

--
Janne Härkönen | Software Craftsman @ Reaktor
http:// reaktor.fi  | http://twitter.com/#!/janneharkonen

Franz Allan Valencia See

unread,
Nov 24, 2010, 3:51:58 AM11/24/10
to Janne Härkönen, robotframe...@googlegroups.com
Thanks, they worked great :-)

Cheers,

--
Franz Allan Valencia See | Java Software Engineer
fran...@gmail.com
LinkedIn: http://www.linkedin.com/in/franzsee
Twitter: http://www.twitter.com/franz_see

binithb

unread,
Mar 1, 2013, 5:09:17 AM3/1/13
to robotframe...@googlegroups.com, Janne Härkönen
Hi Franz,

I am currently checking out your Python db library.
I kind of guesss this question of yours originated from the requirement to parse a select query output.
I think so because I am having some trouble working with a list of tuples when I ran the MySQL query "show databases" and I reached this thread while googling for a possible solution.

When I run this query I am getting an output like

[('information_schema',), ('test',), ('django3db',), ('mysql',)]

Now this data can as well be represented as a simple list instead of the current list of tuples.
Whats the best way to covert this list of tuples to a simple list in your opinion

thanks,
-binith

binithb

unread,
Mar 8, 2013, 7:42:23 AM3/8/13
to robotframe...@googlegroups.com, Janne Härkönen
Hi there,

Updating how I did it



<Convert Column Values of Records to List>
*** Settings ***
Library           Collections

*** Keywords ***
Convert Column Values of Records to List
    [Arguments]    ${column}=0    @{records}    # Extract column values from from records and return as a list
    Log Many    @{records}
    @{column_value_list}=    Create List
    : FOR    ${value}    IN    @{records}
    \    Log    ${value}    level=DEBUG
    \    Append To List    ${column_value_list}    ${value[${column}]}
    Log Many    ${column_value_list}
    [Return]    @{column_value_list}


This will be useful wherever there is a need to covert a list of tuples to a simple list.

As in


Library           DatabaseLibrary

*** Test Cases ***
Verify Databases
    [Setup]    Connect to Product Database
    @{query_results}    DatabaseLibrary.Query    show databases;
    Log Many    @{query_results}
    @{column_value_list}=    Convert Column Values of Records to List    0    @{query_results}
    Collections.List Should Contain Sub List    ${column_value_list}    ${database_name_list}
    [Teardown]    Disconnect From Database
Reply all
Reply to author
Forward
0 new messages