[exec cmd for cmd in cmds]

Discussion in 'Python' started by =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Mar 8, 2006.

  1. Hello all,

    >>> p = "z%i = complex(1-1e-%i, 1-1e-%i)"
    >>> lst = [p % (i,i,i) for i in range(10, 30)]
    >>> for item in lst:

    .... exec item
    ....
    >>>
    >>> p = "z%i = complex(1-1e-%i, 1-1e-%i)"
    >>> lst = [p % (i,i,i) for i in range(10, 30)]
    >>> [exec item for item in lst]

    File "<stdin>", line 1
    [exec item for item in lst]
    ^
    SyntaxError: invalid syntax
    >>>


    is this prohibited for some reasons or is this just happens to be
    disallowed?


    this is one more cool way
    >>> p = "z%i = complex(1-1e-%i, 1-1e-%i);"
    >>> c = reduce(lambda x,y: x+y, [p % (i,i,i) for i in range(20,30)])
    >>> exec c


    and one more :)
    >>> p = "z%i = complex(1-1e-%i, 1-1e-%i);"
    >>> c = "".join([ p % (i,i,i) for i in range(20,30) ])
    >>> exec c


    Regards, Daniel
     
    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Mar 8, 2006
    #1
    1. Advertising

  2. Schüle Daniel wrote:

    > Hello all,
    >
    > >>> p = "z%i = complex(1-1e-%i, 1-1e-%i)"
    > >>> lst = [p % (i,i,i) for i in range(10, 30)]
    > >>> for item in lst:

    > ... exec item
    > ...
    > >>>
    > >>> p = "z%i = complex(1-1e-%i, 1-1e-%i)"
    > >>> lst = [p % (i,i,i) for i in range(10, 30)]
    > >>> [exec item for item in lst]

    > File "<stdin>", line 1
    > [exec item for item in lst]
    > ^
    > SyntaxError: invalid syntax
    > >>>

    >
    > is this prohibited for some reasons or is this just happens to be
    > disallowed?


    exec is a statement. And statements aren' allowed in the _expression_ of a
    list-comprehension.

    > this is one more cool way
    > >>> p = "z%i = complex(1-1e-%i, 1-1e-%i);"
    > >>> c = reduce(lambda x,y: x+y, [p % (i,i,i) for i in range(20,30)])
    > >>> exec c

    >
    > and one more :)
    > >>> p = "z%i = complex(1-1e-%i, 1-1e-%i);"
    > >>> c = "".join([ p % (i,i,i) for i in range(20,30) ])
    > >>> exec c


    If you think so :) Ususally people go for dictionaries in such cases.

    Diez
     
    Diez B. Roggisch, Mar 8, 2006
    #2
    1. Advertising

  3. [...]

    > If you think so :) Ususally people go for dictionaries in such cases.


    you are right, I didn't think about dictionaries

    >>> p = "complex(1-1e-%i, 1-1e-%i)"
    >>> d={}
    >>> [d.update({i:eval(p % (i,i))}) for i in range(20,30)]

    [None, None, None, None, None, None, None, None, None, None]

    so now the work is complete :)

    Regards
     
    =?UTF-8?B?U2Now7xsZSBEYW5pZWw=?=, Mar 8, 2006
    #3
  4. Schüle Daniel wrote:
    > you are right, I didn't think about dictionaries
    > >>> p = "complex(1-1e-%i, 1-1e-%i)"
    > >>> d={}
    > >>> [d.update({i:eval(p % (i,i))}) for i in range(20,30)]

    > [None, None, None, None, None, None, None, None, None, None]
    >
    > so now the work is complete :)
    >
    > Regards
    >

    Really, isn't this clearer?:

    d = {}
    for i in range(20, 30):
    v = 1. - 10. ** -i
    d = complex(v, v)

    If you must repair the mess above, try:

    p = "complex(1-1e-%i, 1-1e-%i)"
    d = dict([(i, eval(p % (i, i))) for i in range(20, 30)])

    Strive to be clear first, terse second given the first is still
    achieved.

    --Scott David Daniels
     
    Scott David Daniels, Mar 8, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Profetas

    System cmds

    Profetas, Jun 12, 2004, in forum: C Programming
    Replies:
    6
    Views:
    582
    Keith Thompson
    Jun 12, 2004
  2. Achim Domma (Procoders)

    read input for cmd.Cmd from file

    Achim Domma (Procoders), Jun 3, 2005, in forum: Python
    Replies:
    2
    Views:
    8,130
    Peter Otten
    Jun 3, 2005
  3. Sarir Khamsi

    Interpreter-like help in cmd.Cmd

    Sarir Khamsi, Jun 9, 2005, in forum: Python
    Replies:
    4
    Views:
    397
    Bengt Richter
    Jun 26, 2005
  4. Diez B. Roggisch

    pydb remote debugging/cmd.Cmd over socket?

    Diez B. Roggisch, May 28, 2008, in forum: Python
    Replies:
    2
    Views:
    576
    Diez B. Roggisch
    May 29, 2008
  5. Diez B. Roggisch

    cmd.Cmd bug or at least docu-bug

    Diez B. Roggisch, May 29, 2008, in forum: Python
    Replies:
    1
    Views:
    355
    Michele Simionato
    May 29, 2008
Loading...

Share This Page