I know i'm being lazy but... bitwise compare

Hi
I am sorry about this I have googled bitwise python a bit and tried a couple of things that I’m not happy with.
So I thought I’d just ask anyway.

I am trying to test the modifier key state against those in the user prefs (key_map_item)
so that when the appropriate mod key combo matches that of the the pref then that
function is called.

Any input would be gratefully accepted.

hopefully the code is self documented enough.

                    
    match = False
    any= key_map_item.oskey and key_map_item.ctrl and key_map_item.shift and key_map_item.alt
    #match the mod keys
    oskey = key_map_item.oskey and event.oskey # "OSkey"
    ctrl = key_map_item.ctrl and event.ctrl # "Ctrl"
    shift = key_map_item.shift and event.shift #"Shift"
    alt = key_map_item.alt and event.alt # "Alt"
    if any and ( oskey or ctrl or shift or alt): match = True

any is a python keyword.
It is not a good habit to shadow keywords.

you could just say


anymodpressed = any((k.oskey,k.ctrl,k.shift,k.alt))

and after that test for each sub-condition explicitly in an if-else construct.

It is often better (more pythonic) to write out each condition explicitly.

As for bitwise comparison, that shouln’t be necessary here, testing only for boolean truth values of python objects. Do I detect a background in C programming here?

it would take a bucket of if to equal a mask
like say
prefs_combo = 1100
key_combo = 1100
=True
or
prefs_combo = 0100
key_combo = 0100
= True
or
prefs_combo = 0101
key_combo = 0100
= False

and all the permutations can be handled in one test using a bit mask
I dont know the pythonese for the bit shiftting

good tip on the keyword. I am usually a lot more verbose
I tend to make long winded variables like
the_variable_that_counts_the_number_of_times_we_go_through_this_stupid_loop
and
test_to_see_if_the_water_is_fine
not really that bad but I do try and make it easy so when I go back a couple of years later I can get a bit of an idea whats going on
I hate writing documentation and I never read it unless ALL else fails anyway I developed this habit

yep C Perl PHP Java Javascript yada yada yada

Ive forgotten more than I know
small programer small town == big flexibilty

went away came back and check this
a bit more googleing and
it aint fancy but I think it should do

prefs_mask = “%i%i%i%i” % (key_map_item.oskey , key_map_item.ctrl , key_map_item.shift , key_map_item.alt)
key_down_mask = “%i%i%i%i” % (event.oskey , event.ctrl , event.shift , event.alt)

then simply
if prefs_mask == key_down_mask : match = True

there is more than one way to skin a cat

I see not a single bitwise operation in this thread?
Those are:

OR
>>> 2 | 4
6

AND
>>> 2 & 4
0

XOR
>>> 2 ^ 4
6

NOT
>>> ~2
-3

Use bin() to turn an integer into a binary representation.

But I don’t see a reason for this at all? You should actually never need such operations in Python.

You can use any() and all() or come up with your own comparison function.

yes you are correct
I didn’t know of the any and all functions and first thought bit comparison was the way to go.

here’s what I ended up with cleaner and clearer


key_match = False
prefs_mask = [prefs_item.oskey, prefs_item.ctrl, prefs_item.shift, prefs_item.alt]
key_down_mask =[event.oskey , event.ctrl , event.shift , event.alt]
if all(prefs_mask) and any(key_down_mask): key_match = True
if prefs_mask == key_down_mask : key_match = True

Thanks for all your help CoDEmanX