From: Alex Hall on
Hi all,
I have a file with a dictionary and a function. The dictionary holds
the name of the function, and the function references the dictionary.
If I put the dictionary first, the function is happy but the
dictionary says the function is not defined. If I switch the two and
put the function first, the function says the dictionary does not
exist. Does anyone have an idea as to how I can make both of them
happy? Thanks!

Example:

myVar={
1:myFunc
}

def myFunc():
myOtherVar=myVar

would result in myVar saying myFunc does not exist. Reverse it, though:

def myFunc():
myOtherVar=myVar

myVar={
1:myFunc
}

and the function myFunc does not see the dictionary. I basically
cannot win either way, and I need a way to resolve this. If you are
curious, the dictionary holds function names and maps to a second
dictionary of keystrokes, allowing me to map a keystroke to call a
function. Thanks!

--
Have a great day,
Alex (msg sent from GMail website)
mehgcap(a)gmail.com; http://www.facebook.com/mehgcap
From: Jason Tackaberry on
Hi Alex,

On Sun, 2010-03-14 at 14:26 -0400, Alex Hall wrote:
> Reverse it, though:
>
> def myFunc():
> myOtherVar=myVar
>
> myVar={
> 1:myFunc
> }
>
> and the function myFunc does not see the dictionary.

The code you provided works just fine (as one would expect). If you can
provide an example doesn't work, we should be able to explain why and
provide advice.

Cheers,
Jason.

From: Chris Rebert on
On Sun, Mar 14, 2010 at 10:26 AM, Alex Hall <mehgcap(a)gmail.com> wrote:
> Hi all,
> I have a file with a dictionary and a function. The dictionary holds
> the name of the function, and the function references the dictionary.
> If I put the dictionary first, the function is happy but the
> dictionary says the function is not defined. If I switch the two and
> put the function first, the function says the dictionary does not
> exist. Does anyone have an idea as to how I can make both of them
> happy?
<snip>
> Reverse it, though:
>
> def myFunc():
>  myOtherVar=myVar
>
> myVar={
>  1:myFunc
> }
>
> and the function myFunc does not see the dictionary.

Please be more specific in what you mean by it not "seeing" the
dictionary, because the "reversed" approach *should* work:

$ python
Python 2.6.4 (r264:75706, Feb 25 2010, 01:21:39)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
.... bar = baz
.... print bar
....
>>> baz = {1:foo}
>>> foo()
{1: <function foo at 0x37b870>}

Cheers,
Chris
--
http://blog.rebertia.com
From: Alex Hall on
Below is pasted the function which is looking for the "funcs"
dictionary, as well as the dictionary. They appear in my py file in
this order, yet I get an error in nextMode() that "global name 'funcs'
is not defined". Oddly, the keys dictionary works fine; it is defined
above the nextMode function.

def nextMode():
global HOTKEYS
global HOTKEY_ACTIONS
global mode
global modes
global modeNum
global modeNames
global funcs
#mode=mode+1
#check to make sure the newly selected mode is enabled
tmp=0
while(tmp<modeNum):
mode=(mode+1)%modeNum
if(sys.modules[modeNames[mode]].enabled=='True'):
break #break on the first enabled mode we find
#end if
tmp+=1
#end while
HOTKEYS=keys[mode]
HOTKEY_ACTIONS=funcs[mode]
registerHotkeys()
speak("Now in "+str(modes[mode])+" mode.")
#end def

#we now have the default mode to be used, but what if it is disabled?
if(sys.modules[modeNames[mode]].enabled=='False'):
nextMode()
#end if

funcs=[]
#this dict MUST be defined after all the functions it uses have been
#ARM function dictionary
funcs.append({
1 : exitProgram,
2 : arm.sayLoad1,
3 : arm.sayLoad2,
4 : arm.sayLoad3,
5 : arm.sayLoad4,
6 : arm.sayProcAvg,
7 : arm.sayUsedRam,
8 : arm.sayDisk1Info,
9 : arm.sayDisk2Info,
10 : nextMode,
11: clipboard.toClipboard
})

#weather function dictionary
funcs.append({
1 : exitProgram,
2 : weather.getCurrent,
3 : weather.getToday,
4 : weather.getTomorrow,
5 : weather.getTwoDays,
6 : weather.getThreeDays,
7 : weather.switchLocation,
8 : arm.sayDisk1Info,
9 : arm.sayDisk2Info,
10 : nextMode,
11 : clipboard.toClipboard
})
funcs.append({
1 : exitProgram,
2 : network.speed,
3 : arm.sayLoad2,
4 : arm.sayLoad3,
5 : arm.sayLoad4,
6 : arm.sayProcAvg,
7 : arm.sayUsedRam,
8 : arm.sayDisk1Info,
9 : arm.sayDisk2Info,
10 : nextMode,
11 : clipboard.toClipboard
})


HOTKEY_ACTIONS=funcs[mode]


On 3/14/10, Chris Rebert <clp2(a)rebertia.com> wrote:
> On Sun, Mar 14, 2010 at 10:26 AM, Alex Hall <mehgcap(a)gmail.com> wrote:
>> Hi all,
>> I have a file with a dictionary and a function. The dictionary holds
>> the name of the function, and the function references the dictionary.
>> If I put the dictionary first, the function is happy but the
>> dictionary says the function is not defined. If I switch the two and
>> put the function first, the function says the dictionary does not
>> exist. Does anyone have an idea as to how I can make both of them
>> happy?
> <snip>
>> Reverse it, though:
>>
>> def myFunc():
>>  myOtherVar=myVar
>>
>> myVar={
>>  1:myFunc
>> }
>>
>> and the function myFunc does not see the dictionary.
>
> Please be more specific in what you mean by it not "seeing" the
> dictionary, because the "reversed" approach *should* work:
>
> $ python
> Python 2.6.4 (r264:75706, Feb 25 2010, 01:21:39)
> [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> def foo():
> ... bar = baz
> ... print bar
> ...
>>>> baz = {1:foo}
>>>> foo()
> {1: <function foo at 0x37b870>}
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>


--
Have a great day,
Alex (msg sent from GMail website)
mehgcap(a)gmail.com; http://www.facebook.com/mehgcap
From: Chris Rebert on
> On 3/14/10, Chris Rebert <clp2(a)rebertia.com> wrote:
>> On Sun, Mar 14, 2010 at 10:26 AM, Alex Hall <mehgcap(a)gmail.com> wrote:
>>> Hi all,
>>> I have a file with a dictionary and a function. The dictionary holds
>>> the name of the function, and the function references the dictionary.
>>> If I put the dictionary first, the function is happy but the
>>> dictionary says the function is not defined. If I switch the two and
>>> put the function first, the function says the dictionary does not
>>> exist. Does anyone have an idea as to how I can make both of them
>>> happy?
>> <snip>
>>> Reverse it, though:
>>>
>>> def myFunc():
>>> myOtherVar=myVar
>>>
>>> myVar={
>>> 1:myFunc
>>> }
>>>
>>> and the function myFunc does not see the dictionary.
>>
>> Please be more specific in what you mean by it not "seeing" the
>> dictionary, because the "reversed" approach *should* work:
>>
>> $ python
>> Python 2.6.4 (r264:75706, Feb 25 2010, 01:21:39)
>> [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> def foo():
>> ... bar = baz
>> ... print bar
>> ...
>>>>> baz = {1:foo}
>>>>> foo()
>> {1: <function foo at 0x37b870>}

On Sun, Mar 14, 2010 at 11:12 AM, Alex Hall <mehgcap(a)gmail.com> wrote:
> Below is pasted the function which is looking for the "funcs"
> dictionary, as well as the dictionary. They appear in my py file in
> this order, yet I get an error in nextMode() that "global name 'funcs'
> is not defined". Oddly, the keys dictionary works fine; it is defined
> above the nextMode function.

Please include the full exception Traceback.
Also, please don't top-post in the future.

> def nextMode():
>  global HOTKEYS
>  global HOTKEY_ACTIONS
>  global mode

You don't need a `global` declaration unless your function needs to
rebind the global variable in question.
So you can remove the next 4 global declarations; they're unnecessary.

>  global modes
>  global modeNum
>  global modeNames
>  global funcs
>  #mode=mode+1
>  #check to make sure the newly selected mode is enabled
>  tmp=0
>  while(tmp<modeNum):
>  mode=(mode+1)%modeNum
>  if(sys.modules[modeNames[mode]].enabled=='True'):
>   break #break on the first enabled mode we find
>  #end if
>  tmp+=1
>  #end while
>  HOTKEYS=keys[mode]
>  HOTKEY_ACTIONS=funcs[mode]
>  registerHotkeys()
>  speak("Now in "+str(modes[mode])+" mode.")
> #end def
>
> #we now have the default mode to be used, but what if it is disabled?
> if(sys.modules[modeNames[mode]].enabled=='False'):
>  nextMode()

How is this call supposed to work when `funcs` (which nextMode() uses)
hasn't been defined yet?!

Cheers,
Chris
--
http://blog.rebertia.com