how to iterate over sequence and non-sequence ?

stef s.mientki at id.umcn.nl
Fri Oct 19 10:19:32 EDT 2007


Paul Hankin wrote:
> On Oct 19, 12:24 am, stef mientki <stef.mien... at gmail.com> wrote:
>   
>> I generate dynamically a sequence of values,
>> but this "sequence" could also have length 1 or even length 0.
>>
>> So I get some line in the form of:
>>       line = '(2,3,4)'
>>       line = ''
>>       line = '(2)'
>> (in fact these are not constant numbers, but all kind of integer
>> variables, coming from all over the program, selected from a tree, that
>> shows all "reachable" variables)
>>
>> So in fact I get the value from an exec statement, like this
>>       exec 'signals = ' + line
>>
>> Now I want to iterate over "signals", which works perfect if there are 2
>> or more signals,
>> but it fails when I have none or just 1 signal.
>>     for value in signals :
>>         do something
>>
>> As this meant for real-time signals, I want it fast, so (I think) I
>> can't afford extensive testing.
>>
>> Any smart solution there ?
>>     
>
> First: don't collect data into strings - python has many container
> types which you can use.
>   
Well I'm not collecting data, I'm collecting pointers to data.
This program simulates a user written program in JAL.
As Python doesn't support pointers, instead I collect names.
The names are derived from an analysis of the user program under test,
so the danger some of you are referring to, is not there,
or at least is not that simple.
Besides it's a local application where the goal is to let a user test 
his program (and hardware),
so if the user want to hack, he can better type directly "format c:\".

> Next, your strings look like they're supposed to contain tuples. In
> fact, tuples are a bit awkward sometimes because you have to use
> '(a,') for a tuple with one element - (2) isn't a tuple of length one,
> it's the same as 2. Either cope with this special case, or use lists.
> Either way, you'll have to use () or [] for an empty sequence.
>   
Of course, thanks Paul,
if I change tuple to list, everything works ok, even with empty lists.

cheers,
Stef Mientki
> --
> Paul Hankin
>
>   



More information about the Python-list mailing list