Code Generator written in python

Discussion in 'Python' started by nyoka, Jan 13, 2010.

  1. nyoka

    nyoka Guest

    Can someone help me with sample python code for a code generator
    nyoka, Jan 13, 2010
    #1
    1. Advertising

  2. nyoka, 13.01.2010 16:48:
    > Can someone help me with sample python code for a code generator


    Such as Cheetah?

    http://www.cheetahtemplate.org/

    BTW, you might want to be more specific about your problem at hand. Code
    generation is a rarely used technique in Python. Most of the time, it's
    more maintainable (and not necessarily harder or more work) to actually
    write the code by hand.

    Stefan
    Stefan Behnel, Jan 13, 2010
    #2
    1. Advertising

  3. nyoka

    Peter Otten Guest

    nyoka wrote:

    > Can someone help me with sample python code for a code generator


    >>> print "print"

    print

    Seriously, you have to provide more information if you want a meaningful
    answer. If the generated code is Python, too, then the advice is most likely
    that you don't need to generate any code at all.

    Peter
    Peter Otten, Jan 13, 2010
    #3
  4. nyoka <> writes:

    > Can someone help me with sample python code for a code generator


    Sure, here are some example of self-evaluating python objects, i.e. for each v
    below,

    v == eval(v)

    I'm quite proud of the last one.

    v = (lambda x:x%('"''""'+x+'"''""'))("""(lambda x:x%%('"''""'+x+'"''""'))(%s)""")

    v = (lambda x:x%('r\"'+x+'\"'))(r"(lambda x:x%%('r\"'+x+'\"'))(%s)")

    v = (lambda x:x%`x`)('(lambda x:x%%`x`)(%s)')

    v = (lambda x: x+"("+`x`+")")('(lambda x: x+"("+`x`+")")')

    v = "\"%s\" %% ((r\"%s\",)*2)" % ((r"\"%s\" %% ((r\"%s\",)*2)",)*2)

    :)

    --
    Arnaud
    Arnaud Delobelle, Jan 13, 2010
    #4
  5. En Wed, 13 Jan 2010 13:09:38 -0300, Arnaud Delobelle
    <> escribió:

    > nyoka <> writes:
    >
    >> Can someone help me with sample python code for a code generator

    >
    > Sure, here are some example of self-evaluating python objects, i.e. for
    > each v
    > below,
    >
    > v == eval(v)
    >
    > I'm quite proud of the last one.


    And I'm still trying to disembowel it! :)

    > v = (lambda x:x%('"''""'+x+'"''""'))("""(lambda
    > x:x%%('"''""'+x+'"''""'))(%s)""")
    >
    > v = (lambda x:x%('r\"'+x+'\"'))(r"(lambda x:x%%('r\"'+x+'\"'))(%s)")
    >
    > v = (lambda x:x%`x`)('(lambda x:x%%`x`)(%s)')
    >
    > v = (lambda x: x+"("+`x`+")")('(lambda x: x+"("+`x`+")")')
    >
    > v = "\"%s\" %% ((r\"%s\",)*2)" % ((r"\"%s\" %% ((r\"%s\",)*2)",)*2)


    --
    Gabriel Genellina
    Gabriel Genellina, Jan 13, 2010
    #5
  6. nyoka

    Steve Ferg Guest

    Steve Ferg, Jan 13, 2010
    #6
  7. nyoka

    trzewiczek Guest

    On 01/13/2010 05:09 PM, Arnaud Delobelle wrote:
    > nyoka<> writes:
    >
    >
    >> Can someone help me with sample python code for a code generator
    >>

    > Sure, here are some example of self-evaluating python objects, i.e. for each v
    > below,
    >
    > v == eval(v)
    >
    > I'm quite proud of the last one.
    >
    > v = (lambda x:x%('"''""'+x+'"''""'))("""(lambda x:x%%('"''""'+x+'"''""'))(%s)""")
    >
    > v = (lambda x:x%('r\"'+x+'\"'))(r"(lambda x:x%%('r\"'+x+'\"'))(%s)")
    >
    > v = (lambda x:x%`x`)('(lambda x:x%%`x`)(%s)')
    >
    > v = (lambda x: x+"("+`x`+")")('(lambda x: x+"("+`x`+")")')
    >
    > v = "\"%s\" %% ((r\"%s\",)*2)" % ((r"\"%s\" %% ((r\"%s\",)*2)",)*2)
    >
    > :)
    >
    >

    If you're proud of the last one, consider moving to Perl. Python is - as
    I heard - about elegance and readability of code...
    trzewiczek, Jan 14, 2010
    #7
  8. trzewiczek <> writes:

    > On 01/13/2010 05:09 PM, Arnaud Delobelle wrote:

    [...]
    >> Sure, here are some example of self-evaluating python objects,
    >> i.e. for each v below,
    >>
    >> v == eval(v)
    >>
    >> I'm quite proud of the last one.

    [...]
    >> v = "\"%s\" %% ((r\"%s\",)*2)" % ((r"\"%s\" %% ((r\"%s\",)*2)",)*2)
    >>
    >> :)
    >>
    >>

    > If you're proud of the last one, consider moving to Perl. Python is -
    > as I heard - about elegance and readability of code...


    No thanks. It's far too easy to write unreadable code in Perl and I
    like a challenge :)

    Seriously - this is a self-evaluating string, not code for landing a
    $1bn rocket on Mars. Do you expect it to be readable? I don't.
    However, I definitely find a certain elegance to it.

    --
    Arnaud
    Arnaud Delobelle, Jan 14, 2010
    #8
    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. Steve C. Orr, MCSD
    Replies:
    1
    Views:
    556
    reaway lee
    Aug 24, 2003
  2. Martin Maurer
    Replies:
    3
    Views:
    4,823
    Peter
    Apr 19, 2006
  3. TheDustbustr
    Replies:
    1
    Views:
    447
    Sami Hangaslammi
    Jul 25, 2003
  4. David Schwartz
    Replies:
    18
    Views:
    1,215
    David Thompson
    Oct 11, 2010
  5. KaiWen
    Replies:
    102
    Views:
    2,708
    Jorgen Grahn
    Sep 15, 2011
Loading...

Share This Page