Usage of dictionary/arrays

Asked by Sudha

I need help on the below code:

In the libary function, declaring as below:
def test_details(option):
    details={
        "test1": {
                            "ip": "10.10",
                            "string": "TESTING",
                            },
        "test2": {
                            "ip": "10.20",
                            "string": "TESTING2",
                            },
                        }
for ip, list in test_details.iteritems():
        if option in list: return ip

In the main function,declaring only 'option' variable as:
option =" test1"
t=lib.test_details(option["ip"])
print(t)

when i am running the above code, it is giving as error as "ip is not declared".
But i don't want to decalre 'ip' as individually again, whereever i want 'ip' value then it should check above list respective with option(test1 or test2) and it should print the value directly .
Similarly i want to type only string value but not ip value in some places ?

Thank you,
Sudha.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Sudha
Solved:
Last query:
Last reply:
Revision history for this message
Test (c4456517) said :
#1

t=lib.test_details(test1["ip"])
print(t)

Does that work as intended?

Revision history for this message
Test (c4456517) said :
#2

If I understand you correctly, you're trying to dynamically call test1 or test2 by assigning it to a variable?

Revision history for this message
RaiMan (raimund-hocke) said :
#3

The only thing, that is done in your def is declaring LOCALLY a dictionary named details.
So this dictionary is not known outside of def, and hence cannot be referenced as with t=lib.test_details(option["ip"])

The access has to be done in the def too.

But the def is not needed at all.

in the lib script:
details={
        "test1": {
                            "ip": "10.10",
                            "string": "TESTING",
                            },
        "test2": {
                            "ip": "10.20",
                            "string": "TESTING2",
                            },
                        }

in the main script:
t=lib.details("test1)("ip")

... no need to invent the wheel again ;-)

Revision history for this message
Sudha (sudharanipretty) said :
#4

Thanks Raiman. my problem is solved.