Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Code Generator written in python

Reply
Thread Tools

Code Generator written in python

 
 
nyoka
Guest
Posts: n/a
 
      01-13-2010
Can someone help me with sample python code for a code generator
 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      01-13-2010
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-13-2010
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
 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      01-13-2010
nyoka <(E-Mail Removed)> 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+'"''""'))("""(lambda x%%('"''""'+x+'"''""'))(%s)""")

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

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

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

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



--
Arnaud
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      01-13-2010
En Wed, 13 Jan 2010 13:09:38 -0300, Arnaud Delobelle
<(E-Mail Removed)> escribiķ:

> nyoka <(E-Mail Removed)> 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+'"''""'))("""(lambda
> x%%('"''""'+x+'"''""'))(%s)""")
>
> v = (lambda x%('r\"'+x+'\"'))(r"(lambda x%%('r\"'+x+'\"'))(%s)")
>
> v = (lambda x%`x`)('(lambda 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

 
Reply With Quote
 
Steve Ferg
Guest
Posts: n/a
 
      01-13-2010
http://nedbatchelder.com/code/cog/

 
Reply With Quote
 
trzewiczek
Guest
Posts: n/a
 
      01-14-2010
On 01/13/2010 05:09 PM, Arnaud Delobelle wrote:
> nyoka<(E-Mail Removed)> 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+'"''""'))("""(lambda x%%('"''""'+x+'"''""'))(%s)""")
>
> v = (lambda x%('r\"'+x+'\"'))(r"(lambda x%%('r\"'+x+'\"'))(%s)")
>
> v = (lambda x%`x`)('(lambda 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...
 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      01-14-2010
trzewiczek <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generally, are the programs written by C++ slower than written by C10% ? KaiWen C++ 102 09-14-2011 11:12 PM
Re: Recommendation for Pseudo-Random Number Generator Written in orCallable from C? David Schwartz C Programming 18 10-11-2010 03:06 AM
I wrote a C++ code generator in Python, would anyone please help me to review the code? :) Kevin Wan Python 5 01-17-2007 09:33 AM
UART with fractional baudrate generator ? Or fractional baudrate generator alone Martin Maurer VHDL 3 04-19-2006 01:26 PM
Re: Can a usercontrol written in C# be used in Web Forms that is written in VB.Net? Steve C. Orr, MCSD ASP .Net 1 08-24-2003 12:06 AM



Advertisments