Iterating dictionary items + if statement results in problems
Dave Angel
davea at davea.name
Mon Apr 15 08:09:16 EDT 2013
On 04/15/2013 06:50 AM, Ombongi Moraa Fe wrote:
> hello Team,
>
> I have this fairly simple script to iterate the dictionary items and check
> if the items match certain values;
>
> dictionary={'1234567890':001, '0987654321':002}
> for k, v in dictionary.iteritems():
> .....
> ..... #suds client statements;
>
> if (k == '1234567890' and v == 001):
> criteria='Test'
> elif (k == '0987654321' and v == 002):
> criteria='Running'
> client.service.methodcall(value1,value2,criteria)
>
>
That's not the whole script, since at the least, you need some code to
import or create client, value1 and value2.
>
> During the first run of the dictionary items,
What do you mean by that, exactly? Do you mean the first time around
the loop? Or the first time the script is run? Or what?
> the client.service.methodcall
> is called only once as expected; and a success initiation response is
> received from server. However, during the second run, the
> client.service.methodcall is called twice - when i check the log files, i
> see the client send request is done twice. Duplicate send requests of the
> same parameters results in a error in inititating a connection.
>
> Someone please show me why my second run results in the
> client.service.methodcall() running twice. I can't seem to get a hang on
> it.
>
>
Why not take out the middleman, and just add some prints in the loop? I
don't see any point in the loop; if you're sure there are exactly two
items in the dict, just process those two items. If you're not sure,
what do you want to happen when you encounter something that doesn't
match either the if or the elif. Currently, you'll just repeat the last
methodcall.
One final thing, a dict's order is not promised. So you may process
these items in either as "Test" and "Running", or in the reverse order.
My guess is that this is not your actual code at all, and you're trying
to "simplify" it for us. You probably have more than two items in the
dict, and one of them is NOT matching any of the if/elif tests.
Possibly it's not matching because of your mistaken use of octal. Octal
won't hurt for ints below 8, but you probably don't restrict it in the
real code. For example, v = 030 will not match equal in the following:
elif v == 30:
--
DaveA
More information about the Python-list
mailing list