Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Code Generator written in python

5 views
Skip to first unread message

nyoka

unread,
Jan 13, 2010, 10:48:26 AM1/13/10
to
Can someone help me with sample python code for a code generator

Stefan Behnel

unread,
Jan 13, 2010, 10:52:52 AM1/13/10
to
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

Peter Otten

unread,
Jan 13, 2010, 10:56:13 AM1/13/10
to
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

Arnaud Delobelle

unread,
Jan 13, 2010, 11:09:38 AM1/13/10
to
nyoka <dansh...@gmail.com> 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

Gabriel Genellina

unread,
Jan 13, 2010, 11:26:01 AM1/13/10
to pytho...@python.org
En Wed, 13 Jan 2010 13:09:38 -0300, Arnaud Delobelle
<arn...@googlemail.com> escribiᅵ:

> nyoka <dansh...@gmail.com> 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

Steve Ferg

unread,
Jan 13, 2010, 3:58:06 PM1/13/10
to

trzewiczek

unread,
Jan 14, 2010, 6:57:31 AM1/14/10
to pytho...@python.org
If you're proud of the last one, consider moving to Perl. Python is - as
I heard - about elegance and readability of code...

Arnaud Delobelle

unread,
Jan 14, 2010, 11:51:25 AM1/14/10
to
trzewiczek <trzew...@trzewiczek.info> 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

0 new messages