concise code (beginner)

bambam david at asdf.asdf
Thu Sep 6 07:44:57 CEST 2007

First, thank you.

All of the suggestions match what we want to do much better
than what we are doing. We have a script, written in python,
which is doing testing. But the python script doesn't look anything
like the test script, because the python script is written in python,
and the test script is a series of instrument command macros.

By putting the script sequence into a collection that is separate
from the python code, we will get script list that general engineering
will find much easier to understand:

def script(self)
    def a0010(): global self; self.power_on([]);
    def a0020(): global self;[, ' started']);
    def a0030(): global self; self.resetMinuteReg([]);
    def a0040(): global self; self.disablePLmessages([]);
    def a0050(): global self;<<8));

Most of these won't generate exceptions: exceptions are expected
only on the calculations following the reads, but of course the
problem is that the exceptions are unexpected... The semi-colons
were already there, I've just stripped out the loops and exception
handlers. The original code is a mixture of functions with internal
and external [dev] loops.

Because of the sequence names, I have a choice of using generated
call names (James), or a static list of some sort.

Other possibilities might be
1) Using dir(script) to get a list of line functions
2) Using frame.f_lineno instead of line functions
3) Use an expression list instead of line functions
4) Multiple script objects with yield on each line.

The devices are in a list, and are removed by using pop(i). This
messes up the loop iteration, so it is actually done by setting a
flag on each device in the exception handler, with ANOTHER
loop after each write/read/calculate sequence. I left that out also
because I wanted to show the intended logic.

I'm not wedded to the idea of using sequence numbers for
line functions. Comments are welcome. Sorry I didn't include
this level of detail in the original, but I am interested in the
question at all levels.

Note that this achieves synchronous parallel processing --
another area I know nothing about -- but I'm just starting
with the code as I got it, and coding so far was focused
on hardware integration.


"bambam" <david at asdf.asdf> wrote in message
news:13dsvqrqivtf092 at
>I have about 30 pages (10 * 3 pages each) of code like this
> (following). Can anyone suggest a more compact way to
> code the exception handling? If there is an exception, I need
> to continue the loop, and continue the list.
> Steve.
> -----------------------------------
> for dev in devs
>    try:
>        dev.read1()
>    except
>        print exception
>        remove dev from devs
> for dev in devs
>    try:
>        dev.read2()
>    except
>        print exception
>        remove dev from devs
> for dev in devs
>    try:
>        dev.read3()
>    except
>        print exception
>        remove dev from devs
> etc.

More information about the Python-list mailing list