Iterating dictionary items + if statement results in problems

Dave Angel davea at davea.name
Mon Apr 15 13:29:20 EDT 2013


On 04/15/2013 09:37 AM, Ombongi Moraa Fe wrote:
> Hello Team,
>
> Thanks for your input.
>
> |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:
>
> I've changed the key,value pairs in the dictionary because of privacy
> commitment with my provider;
>
> |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.
>
>              Currently, I only have 2 items in the dictionary. However, this
> is a test environment and in the product environ, my items will be as many
> as the number of services created on server for my connection. Currently,
> the production has 10 services (key,value pairs)
>
> I will only have a set number of key,value pairs and I cannot use other
> items outside this range. The connection to providerr wouldn't be establish
> for anything out of the allowed item ranges;
>
> |That's not the whole script, since at the least, you need some code to
> import or create client, value1 and value2.
>
> The other portion of script to import the client and get values of
> parameters value1 and value2 works well. I have tested the script without
> using dictionaries by manually changing the items within the dictionary,
> inside the code and it works successfully, establishing a connection for
> both items; Now I need to iterate this for the sake of numerous items in
> the production environment. And that's where I thought to bring in the if
> statement.
>
> |What makes you think there should be one call given the code above?
> client.service.methodcall must be called for every |loop iteration, so
> there's one call with criteria 'test' and one with 'Running'.  Note there's
> no guarantee that the calls will |always take place in the same order.
>
> Yes, I do understand that the items in the dictionary are not ordered. And
> what I expect is "one call with criteria 'Test' and one with 'Running'. From
> my logs file, the duplicate call always occurs at the last iteration of the
> dictionary items.

Do you mean there are 3 calls to that method, with only two items in the 
dictionary??  More likely, there's some logic inside the method that's 
faking an extra log entry.

>
> In short, my problem arises after I include the if statement inside the
> loop. I am new to python but I am pretty sure my program syntax is correct
> here. If I run  a print statement instead of the
> client.service.methodcall(value1,
> value2), my output is just
>
> Test
> Running
>
> 2 of the services in the production environment will require the criteria
> value. For the rest of the services, the method call will simply be
>
> client.service.methodcall(value1, value2)
>
> I need this criteria value in order to establish a service specific
> connection for these 2 matching key,value pairs; instead of a generalised
> connection as will be the case for any additional keys.this is where the
> else default statement will come in later on. Right now I need to be able
> to resolve the problem with my script as-it-is (with 2 dictionary items)
>

As far as I can see, there's no problem with the script, as far as it 
goes.  If you replace the method call with a print statement, and it 
suddenly starts working, then by definition, there's something wrong 
with the method.  We cannot help there.

Many times, we can play detective, and guess what's going on without 
running a program.  Since we've told you what we each have found, and 
you haven't posted any new version of the code, that's about all we can 
do.  Perhaps someone else who hasn't chimed in can help.  But if I were 
you, I'd post a self-contained program that demonstrates the problem.

Or maybe it's because you're posting in html, rather than the expected 
text, and you have some indent problem that doesn't show up after 
converting round trip.  Please tell your email program to use text 
message format when posting here.




-- 
DaveA



More information about the Python-list mailing list