List comprehesnion in variable doesnt work

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

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

Eric B. <eb 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. :slight_smile:
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. <josh 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

Josh B. <josh 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

the last point doesnt work. I dont know why.
If i insert the close() function manually it works. :slight_smile:
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