Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same function is giving different results in opposite order in Python. Why?

I'm using this code:

def copy_part_of_space(row,column,lenght):
    #Copy String to Presentation Space (15)
    #Prerequisite Connect Presentation Space
    #Prerequisite function: connect_pcomm(presentation_space)    
    function_number = c_int(8)
    data_string = create_string_buffer(lenght*2*2) #number of unicode char *2*2
    lenght = c_int(lenght)
    ps_position = c_int(((row - 1) * 80)+ column)
    foo = hllapi(byref(function_number), data_string, byref(lenght), byref(ps_position))
    data_string.value
    return {{
        0 : 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.',
        1 : 'Your program is not connected to a host session.',
        4 : 'The host presentation space contents were copied. The connected host     presentation space was waiting for host response.',
        5 : 'The host presentation space was copied. The keyboard was locked.',
        9 : 'A system error was encountered.',
        'x' : 'Undocumented error found. Run in circles.',
        }.get(foo, 'x'),data_string.value}

The idea is to copy some information from a terminal; The functions needs to return the status information (using the dictionary and 0,1,4,5,9,x parameters) AND the copied information - using data_string.value

To run some tests I was using this code that uses the function above:

for a in range(15,22):
    print copy_part_of_space(a,7,8)

This is the results:

   set(['The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', '36343581'])
   set(['36343663', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.'])
   set(['The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', '36343708'])
   set(['36344673', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.'])
   set(['36344740', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.'])
   set(['The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', '36344758'])
   set(['36344869', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.'])

As you can see, sometimes I get the status information before what was copied from the host application - like the first line.

But sometimes I get the information that was copied before the status information, like line two.

I am not familiar with using dict to return information, so I guess that could be a problem, specially when mixing with the fact that I'm trying to return two variables.

Can anyone explain why is this happening?

I know that I could simply use the dict and save the return information to a variable before passing the return, but I really thought this is a more elegant solution - am I wrong?

like image 666
Johnny Bigoode Avatar asked Feb 26 '26 11:02

Johnny Bigoode


2 Answers

sets are unordered (or better, their order is arbitrary). Nothing you can do about this except use an ordered datatype instead.

For example by removing the set constructor {...}:

return {
    0 : 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.',
    1 : 'Your program is not connected to a host session.',
    4 : 'The host presentation space contents were copied. The connected host     presentation space was waiting for host response.',
    5 : 'The host presentation space was copied. The keyboard was locked.',
    9 : 'A system error was encountered.',
    'x' : 'Undocumented error found. Run in circles.',
    }.get(foo, 'x'), data_string.value

Now this code returns a tuple instead (the first element is the lookup result from the "error message dictionary", the second one whatever is contained in data_string.value).

like image 120
Tim Pietzcker Avatar answered Mar 01 '26 05:03

Tim Pietzcker


You're specifically returning a set, which is defined to be an unordered datattype. That is, the elements of a set may be returned in any order. Sets are optimized for membership testing (if x in set:). Sets are like the keys of a dictionary: they can be iterated over in any order.

I suspect a better data type for you would be a tuple: return (a, b)

Then the results will always be in the same order.

Note the differences in literal notation:

  • Dictionary has colons to pair items.: {'a': 'b', 'c': 'd').
  • Set has no colons, and is just arbitrary-ordered items: {'a', 'b', 'c', 'd'}
  • Tuple uses parenthesis: ('a', 'b', 'c', 'd')
  • List uses square brackets, and is mutable: ['a', 'b', 'c', 'd']
like image 37
Max Avatar answered Mar 01 '26 03:03

Max



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!