[melbourne-pug] Simple syntactic error?

Anthony Briggs abriggs at westnet.com.au
Fri May 23 01:39:14 CEST 2008


Hmm, 

You've got this in your code:

canvas = appuifw.Canvas(event_callback = keys)

appuifw is a framework, so your keys function will be called in a
different 'context'. Your framework might be using state for something
else, or is otherwise messing around with globals. Try changing
'state' to 'foo_state' or similar and see if that helps.

And yes, you should have 'global state' in each of your functions,
otherwise you'll drive a local variable over the top of your global.

It's hard to tell what's going on without a traceback, but the following
code replicates something which might be similar to your error:

state = None

def keys():
    if state == None:
        print "Foo!", state
    else:
        print "Bar!", state

def cb_calling(arg):
    state = arg

def clear_state():
    global state
    del state

cb_calling("Wibble")
keys()
clear_state()
keys()

===

result:

anthony at anthony:~$ python test.py 
Foo! None
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    keys()
  File "test.py", line 6, in keys
    if state == None:
NameError: global name 'state' is not defined
anthony at anthony:~$


On Fri, May 23, 2008 at 08:53:01AM +1000, Kevin Littlejohn wrote:
> Are you certain cb_calling is even being called?  If it's not, that'll  
> be why your variable isn't carrying any value.
>
> John's right, cb_calling should definitely declare state as global - and 
> for style, I'd want the global declaration in keys() as well (not  
> strictly necessary, but good form).
>
> For debug, I'd drop either an "import pdb ; pdb.set_trace()" (and read  
> up on how to use pdb, it's your friend ;) or a "print args" into the  
> start of cb_calling, see exactly when it's called and what it's doing.  
> But if you're not getting any errors, then it's not a syntactic issue.
>
> KevinL
>
> On 23/05/2008, at 05:53 , g sobers wrote:
>
>> Appreciate the response.
>>
>> Are you getting an AttributeError in keys() or is state just None.
>>
>> No errors, per se, are generated. So, state=none seems most likely.
>>
>>
>> It looks like cb_calling needs to declare state as global
>>
>> def cb_calling(args):
>>   global state
>>   state = args[0]
>>
>> As regards to your suggestion, I just implemented it - to no avail.  
>> Same conditions persist.
>>
>> Where else should I be looking?
>>
>> Best,
>> wirefree
>>
>>
>>
>> On 5/22/08, John La Rooy <john.larooy at gmail.com> wrote:
>> Are you getting an AttributeError in keys() or is state just None.
>> It looks like cb_calling needs to declare state as global
>>
>> def cb_calling(args):
>>    global state
>>    state = args[0]
>>
>> On Fri, May 23, 2008 at 5:10 AM, g sobers <g.forumz at gmail.com> wrote:
>> G'day!
>>
>> Following is a small PyS60 script. The error seems related to basic  
>> syntax - "state" in keys() is not recognized although defined  
>> globally.
>>
>> Would appreciate assistance.
>>
>> =============================================
>> import appuifw, key_codes, e32, telephone
>> state = None
>>
>> def keys(event):
>>    if event['keycode'] == key_codes.EKeyYes:
>>      appuifw.note(u"Doesn't Matter")
>>   elif (event['keycode'] == key_codes.EKeyYes) and (state ==  
>> telephone.EStatusConnected):
>>      appuifw.note(u"Yes was pressed and call active")
>>
>> def cb_calling(args):
>>    state = args[0]
>>
>> def quit():
>>    app_lock.signal()
>>
>> telephone.call_state(cb_calling)
>> canvas = appuifw.Canvas(event_callback = keys)
>> appuifw.app.body = canvas
>> appuifw.app.exit_key_handler = quit
>> app_lock = e32.Ao_lock()
>> app_lock.wait()
>> ==============================================
>>
>> Best,
>> wirefree
>>
>> _______________________________________________
>> melbourne-pug mailing list
>> melbourne-pug at python.org
>> http://mail.python.org/mailman/listinfo/melbourne-pug
>>
>>
>>
>> _______________________________________________
>> melbourne-pug mailing list
>> melbourne-pug at python.org
>> http://mail.python.org/mailman/listinfo/melbourne-pug
>>
>>
>> _______________________________________________
>> melbourne-pug mailing list
>> melbourne-pug at python.org
>> http://mail.python.org/mailman/listinfo/melbourne-pug
>

> _______________________________________________
> melbourne-pug mailing list
> melbourne-pug at python.org
> http://mail.python.org/mailman/listinfo/melbourne-pug


-- 
------------------------------------------------------
 HyPerACtIVe?! HEY, Who ArE yoU cAllInG HYPERaCTive?!
 abriggs at westnet.com.au
------------------------------------------------------


More information about the melbourne-pug mailing list