Forum: GNU Radio list comprehesnion in variable doesnt work

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
feldmaus (Guest)
on 2009-05-30 14:57
(Received via mailing list)
Hi All again,

list comprehension doesnt work in variables, for  example:
[print(i) for i in range(3)]
or
variable_2 = file('../../../tabs.dat','w')
([variable_2.write(str(i)) for i in [1,2]] is True) &\
                  (variable_2.close() is True)
doesnt work.

Regards Markus
Eric B. (Guest)
on 2009-05-30 23:40
(Received via mailing list)
On Sat, May 30, 2009 at 10:55:50AM +0000, feldmaus wrote:
> Hi All again,
>
> list comprehension doesnt work in variables, for  example:
> [print(i) for i in range(3)]

"print" is a statement, not a function.


I'd probably use:

  for i in range(3): print i


> or
> variable_2 = file('../../../tabs.dat','w')
> ([variable_2.write(str(i)) for i in [1,2]] is True) &\
>                   (variable_2.close() is True)
> doesnt work.

([v2.write(str(i)) for i in [1,2]] is True) and v2.close() is True


will of course always be False, since [None, None] is not True


It looks like you are trying to abuse list comprehensions for some kind
of control flow short cut.  I think of them as an equivalent to the
"map" builtin, a higher-order function.

You may want to consider "map" and "reduce" in combination.


Eric
feldmaus (Guest)
on 2009-05-31 01:00
(Received via mailing list)
Eric B. <eb <at> comsec.com> writes:

>
> will of course always be False, since [None, None] is not True
>
> It looks like you are trying to abuse list comprehensions for some kind
> of control flow short cut.  I think of them as an equivalent to the
> "map" builtin, a higher-order function.
>
> You may want to consider "map" and "reduce" in combination.
>
Thanks for your answer,

i tried to save some infos, because i can not send a string to the file
sink.
However i tried to save to a file AND close this in the next step.
But this doesn't work. I still tried the map function, but doesnt got it
to work. And at least i tried to set up 3 variables in grc to
1.)define a file descriptor
2.)write to the file something
3.)and close the file

the last point doesnt work. I dont know why.
If i insert the close() function manually it works. :-)
But not with grc.
I think there is a problem with the sequence grc set up the variables.
However it doesnt work.

regards markus
Josh B. (Guest)
on 2009-05-31 02:56
(Received via mailing list)
>
> the last point doesnt work. I dont know why.
> If i insert the close() function manually it works. :-)
> But not with grc.
> I think there is a problem with the sequence grc set up the variables.


I bet it works very well actually.

variable1 = open(file)
variable2 = variable1.write(xxx)
variable3 = variable1.close()

GRC knows that variable2 and variable3 depend on variable1, but
variable2 and variable3 are not dependent upon other, so their order
does not matter.

In reality, this is a hack, and you are abusing what variables are and
what they represent. What you would really want to see is:

variable1 = open(file)
variable1.write(xxx)
variable1.close()

So, take my advice from my previous email, and encapsulate this code in
a custom grc block wrapper, and you will get exactly what you are
looking for.

Auiafgkjsagfkagh
-Josh
feldmaus (Guest)
on 2009-05-31 11:59
(Received via mailing list)
Josh B. <josh <at> joshknows.com> writes:

> I bet it works very well actually.
>
> variable1 = open(file)
> variable2 = variable1.write(xxx)
> variable3 = variable1.close()
>
> GRC knows that variable2 and variable3 depend on variable1, but
> variable2 and variable3 are not dependent upon other, so their order
> does not matter.
Thanks i got this to work.

regards Markus
feldmaus (Guest)
on 2009-05-31 16:42
(Received via mailing list)
Josh B. <josh <at> joshknows.com> writes:

>
> So, take my advice from my previous email, and encapsulate this code in
> a custom grc block wrapper, and you will get exactly what you are
> looking for.
>
I forgot to wrote, that there is still a bug, allthough I got it to
work.

regards markus
This topic is locked and can not be replied to.