Guidelines to write python code for arduino yun


Are there some guidelines to follow in writing python code for embedded openwrt in the yun?
Like avoid the exceptions?

I have problem with.. the network (yet another..).

I have a simple arduino code that at a certain point calls via Bridge Process::run a python script. The python script is calling an ip with urllib2 via http get and return it to the Bridge. The http response could be a message error or a number.

I don't know if this is the problem, but if in the python script I have a nested exception handling.. it caused the openwrt to lost the network (and I need to reboot it)

this is an example of the python code:

req = urllib2.Request(ip)
    response = urllib2.urlopen(req, timeout=5)
    t = the_page =
        t = int(t)
        foo() # and go on
    except Exception as e:
        print 'no number'
        print e
except  Exception as e:
    print 'url lib error'
    print e

after the first negative response (the 'no number') the following Bridge call ends up with a: urlopen error host is unreachable and I don't manage anymore to ssh into the yun (but it works good because the bridge it is giving me this error, just no network, no ping)

I tried to tail -f /vat/bridge error log but after the first exception.. I have no more the yun in my network, also the ping says host is down.

This story makes me think that something is wrong with the nested exception and it is a construct to be avoided and so the first question.

any kind of advice?

Does the code run on your computer?

yes it does..

"host unreachable" means the yun cannot access the target host. reasons may be a yun not configured to access internet (try with httpclient example to verify internet connectivity) or a wrong hostname

hi federico
thanks for your effort.

if I write the python script without the nested try except (but with some checks) everything works ok, if I put the nested try except block somehow the network is compromised.

that's why I thought the try except was somehow too heavy and they has to be avoided as general guidelines in python code

I didn't see the nested try. Well, yun or not, nested "try"s should be avoided for sake of readability (me missing them is a proof, FWIW)
In the yun case, I'd let the error crash the script during development (some one call this approach "crash early"), and only after thorough testing I'd add the needed "try"s

thank you.

any other general advice for python code? like something to avoid? comprehension? the 'as' constructor? function curry? decorators?

Nothing more than a good book and lots of practice (and related mistakes)
I'm not python expert, but I have this in my "to-read" bookshelf Learn Python the Hard Way