From: MRAB on
Alex Hall wrote:
> On 3/23/10, MRAB <python(a)mrabarnett.plus.com> wrote:
[snip]
>> Incidentally, you might want to change:
>>
>> if(not action_to_take):
>>
>> to:
>>
>> if action_to_take is None:
>>
>> in case any of the values happen to be 0 (if not now, then possibly at
>> some time in the future).
> Sorry, could you explain why you suggested this? I do not follow.
> Because of the if statement "if action_to_take:", I figured it was
> saying "if action_to_take was successfully set" or something else
> having a boolean value. Guess not?
>
The code:

globalFuncs.get (msg.wParam)

returns None if the key isn't in the dict.

'if' and 'while' statements treat other objects than just True as True,
in fact anything for which bool() returns True. For example:

bool(100) returns True
bool([1, 2, 3]) returns True
bool('some text') returns True

but:

bool(0) returns False
bool([]) returns False
bool('') returns False
bool(None) returns False

I also just noticed that you don't give action_to_take a default value
before checking whether it's a hotkey. Suppose that "msg.message ==
win32con.WM_HOTKEY" was False:

if msg.message == win32con.WM_HOTKEY:
action_to_take=globalFuncs.get (msg.wParam)
if(not action_to_take):

It would get to "if(not action_to_take):" and either find that
action_to_take wasn't defined, or use the value from the previous pass
through the loop.
From: Tim Golden on
On 23/03/2010 17:01, Alex Hall wrote:
> Hi all, but mainly Tim Golden:
> Tim, I am using your wonderful message loop for keyboard input, the
> one on your site that you pointed me to a few months ago. It has been
> working perfectly as long as I had only one dictionary of keys mapping
> to one dictionary of functions, but now I want two of each. My program
> has different modes, which may have varying keystrokes, and I also
> have some global keystrokes which are the same across all modes, like
> exiting or switching modes. I cannot figure out how to make the
> message loop look in two dictionaries at onc. I tried using an if,
> saying that if action_to_take was not set in the mode-specific
> dictionary then look at the global dictionary, but it is like it is
> never looking in the global dictionary at all. I get no syntax errors
> or problems when running the program, so it has to be something in my
> logic.

There's definitely some confusion, as MRAB picked up, with the
globalkeys / globalfuncs thing. I can see no problem with simply
defining an additional pair of dictionaries parallel with each of
the keys/funcs dictionaries (note that the numbers are different
from those of othe other dictionaries as these are the registration
ids of the hotkeys):

global_keys = {
100: (68, win32com.MOD_ALT | win32con.MOD_CONTROL ...),
...
}

global_funcs = {
100 : dict.ShowLookupDialog,
...
}

Then your central action code could be something like:

action_to_take = global_funcs.get (msg.wParam) or HOTKEY_ACTIONS.get (msg.wParam)
if action_to_take:
action_to_take ()

This assumes that the funcs dictionaries will only ever contain a
valid function, so the only possible Falsish value will arise from
the get returning None.

TJG

BTW, you're shadowing a couple of builtins: global & dict which
might give you problems later if you needed them. There are other
issues with the structure of your code but nothing a little
refactoring couldn't sort out if you felt so inclined.
From: Alex Hall on
Thanks, it seems to be working for now... Hopefully that trend continues!

On 3/24/10, Tim Golden <mail(a)timgolden.me.uk> wrote:
> On 23/03/2010 17:01, Alex Hall wrote:
>> Hi all, but mainly Tim Golden:
>> Tim, I am using your wonderful message loop for keyboard input, the
>> one on your site that you pointed me to a few months ago. It has been
>> working perfectly as long as I had only one dictionary of keys mapping
>> to one dictionary of functions, but now I want two of each. My program
>> has different modes, which may have varying keystrokes, and I also
>> have some global keystrokes which are the same across all modes, like
>> exiting or switching modes. I cannot figure out how to make the
>> message loop look in two dictionaries at onc. I tried using an if,
>> saying that if action_to_take was not set in the mode-specific
>> dictionary then look at the global dictionary, but it is like it is
>> never looking in the global dictionary at all. I get no syntax errors
>> or problems when running the program, so it has to be something in my
>> logic.
>
> There's definitely some confusion, as MRAB picked up, with the
> globalkeys / globalfuncs thing. I can see no problem with simply
> defining an additional pair of dictionaries parallel with each of
> the keys/funcs dictionaries (note that the numbers are different
> from those of othe other dictionaries as these are the registration
> ids of the hotkeys):
>
> global_keys = {
> 100: (68, win32com.MOD_ALT | win32con.MOD_CONTROL ...),
> ...
> }
>
> global_funcs = {
> 100 : dict.ShowLookupDialog,
> ...
> }
>
> Then your central action code could be something like:
>
> action_to_take = global_funcs.get (msg.wParam) or HOTKEY_ACTIONS.get
> (msg.wParam)
> if action_to_take:
> action_to_take ()
>
> This assumes that the funcs dictionaries will only ever contain a
> valid function, so the only possible Falsish value will arise from
> the get returning None.
>
> TJG
>
> BTW, you're shadowing a couple of builtins: global & dict which
> might give you problems later if you needed them. There are other
> issues with the structure of your code but nothing a little
> refactoring couldn't sort out if you felt so inclined.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


--
Have a great day,
Alex (msg sent from GMail website)
mehgcap(a)gmail.com; http://www.facebook.com/mehgcap
First  |  Prev  | 
Pages: 1 2
Prev: Python is cool!!
Next: Unicode blues in Python3