473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using Python to generate code?

Dear all:

I need your advice on this matter. I am working on a program which
takes some pieces of System-C code in and generate some other System-C
code. (System-C code is just C++ with some special libraries, hence
you can consider it as C++).

Right now, the generator program is written in C++. And I feel not so
comfortable with C++. I feel C++ is an overkill. Because, I need to
generate some code, hence in the program there are a lot of something
like this:

printf(out, "for (%s = 1, %s < %s, %s < %s )", varName, varName,
varName1, varname, varName2);

It is just too messy if I have more than 20 lines like this.

So my question is:
1. Can Python help to solve this issue?
2. Does Python has a parser? It seems to me that there is no-standard
Parser for Python right now. If it is the case, can I interface Python
with existing cup and lex?

Many thanks!
Tuan Anh
P/S: Also because it is NO big code generator, it only needs to
generate some simple codes. Hence, using some abstract-syntax-tree
library is also too an overkill.
Jul 18 '05 #1
16 2443
On Tue, 07 Sep 2004 23:51:38 -0700, Tran Tuan Anh wrote:
printf(out, "for (%s = 1, %s < %s, %s < %s )", varName, varName,
varName1, varname, varName2);

It is just too messy if I have more than 20 lines like this.
It looks like you want a templater. I don't know what such programs exist
for C++, if any, or how good they are.

You may get a lot of milage out of just adding some utility functions, to
turn that to something like

forFromOne(out, var, max, min);

Changing languages is pretty drastic if you have working, trusted code.
(If those words don't describe your code, then that assessment may change.)
So my question is:
1. Can Python help to solve this issue?
Maybe a little, but if the above code snippet is representative, the
Python equivalent of that isn't much shorter. Python would start to shine
if you want to start doing smarter things with the data, but without
knowing about the data it is hard to be specific. Basically, the moment
you aren't just assembling strings, Python will *quite likely* have an
advantage.

(I find it very likely that a Pythonic outlook on the problem would allow
significant simplification at acceptable effort levels, but I can't
*promise* that; if all you really, honestly need is printf, which I think
would be rare but certainly conceivable, then you may get no advantage.)
2. Does Python has a parser? It seems to me that there is no-standard
Parser for Python right now. If it is the case, can I interface Python
with existing cup and lex?


Why are you asking about a parser when you are talking about *outputting*
code? Is there something left out of the problem description?

There are a lot of parsers for Python, covering a wide range of needs.

Jul 18 '05 #2
Tran Tuan Anh <anhtt <at> hotmail.com> writes:
1. Can Python help to solve this issue?
I have used Python for quite simple code generation/templating - not in C++, but
that makes no difference. There are even special and not that complex tools for
code generation, e.g. Cog (http://www.nedbatchelder.com/code/cog/index.html).
2. Does Python has a parser? It seems to me that there is no-standard


There are all kinds of parsers, but I've never used any of them. Google gives
tons of links when searching for "python parser", so a more specific search
would be wise.

Yours,

Andrei

Jul 18 '05 #3
I use Python to generate quite a bit of HTML and
JavaScript code and it works very well. If you
can use Python's classes to abstract individual
elements and give each of them a __str__ method
you can build very complex code generator classes
that know how to render their own output.

Larry Bates
Syscon, Inc.

"Tran Tuan Anh" <an***@hotmail.com> wrote in message
news:ed**************************@posting.google.c om...
Dear all:

I need your advice on this matter. I am working on a program which
takes some pieces of System-C code in and generate some other System-C
code. (System-C code is just C++ with some special libraries, hence
you can consider it as C++).

Right now, the generator program is written in C++. And I feel not so
comfortable with C++. I feel C++ is an overkill. Because, I need to
generate some code, hence in the program there are a lot of something
like this:

printf(out, "for (%s = 1, %s < %s, %s < %s )", varName, varName,
varName1, varname, varName2);

It is just too messy if I have more than 20 lines like this.

So my question is:
1. Can Python help to solve this issue?
2. Does Python has a parser? It seems to me that there is no-standard
Parser for Python right now. If it is the case, can I interface Python
with existing cup and lex?

Many thanks!
Tuan Anh
P/S: Also because it is NO big code generator, it only needs to
generate some simple codes. Hence, using some abstract-syntax-tree
library is also too an overkill.

Jul 18 '05 #4
On 7 Sep 2004 23:51:38 -0700, Tran Tuan Anh <an***@hotmail.com> wrote:
Right now, the generator program is written in C++. And I feel not so
comfortable with C++. I feel C++ is an overkill. Because, I need to
generate some code, hence in the program there are a lot of something
like this:

printf(out, "for (%s = 1, %s < %s, %s < %s )", varName, varName,
varName1, varname, varName2);

It is just too messy if I have more than 20 lines like this.
Agreed, reached that same conclusion.
Therefore, I switched to something like

output="""for ($VAR = 1, $VAR < $START, $VAR < $END)"""

output=string.replace(output,"$VAR",varName)
output=string.replace(output,"$START",varName1)
output=string.replace(output,"$END",varName2)

Obviously, this can be enhanced.
(use other conventions than $identifier, and performing the substitution
in a loop).
BTW: I am not sure string.replace works OK as shown here.

In a sense, I am using the string replacement to substitute variables
names thus making my own text templating system.
1. Can Python help to solve this issue?
yep.
2. Does Python has a parser? It seems to me that there is no-standard
yes, several.
I have good experiences with spark. you have to be careful what you
enter, and it is not fast, but it can handle just about any
grammar you can think of.
P/S: Also because it is NO big code generator, it only needs to
generate some simple codes. Hence, using some abstract-syntax-tree
library is also too an overkill.


Anything small, simple, and beautiful grows ......
:-)

Albert
--
Unlike popular belief, the .doc format is not an open publically available format.
Jul 18 '05 #5
Jeremy Bowers <je**@jerf.org> wrote:
...
2. Does Python has a parser? It seems to me that there is no-standard
Parser for Python right now. If it is the case, can I interface Python
with existing cup and lex?
Why are you asking about a parser when you are talking about *outputting*
code? Is there something left out of the problem description?


the original poster started his original post with:
I need your advice on this matter. I am working on a program which
takes some pieces of System-C code in and generate some other System-C


That "takes some pieces of ... code" sounded to me like an indication
that a parser is needed -- nothing left out of the problem description,
IMHO.
Alex
Jul 18 '05 #6
Albert Hofkamp <ha*@se-126.se.wtb.tue.nl> wrote:
On 7 Sep 2004 23:51:38 -0700, Tran Tuan Anh <an***@hotmail.com> wrote:
Right now, the generator program is written in C++. And I feel not so
comfortable with C++. I feel C++ is an overkill. Because, I need to
generate some code, hence in the program there are a lot of something
like this:

printf(out, "for (%s = 1, %s < %s, %s < %s )", varName, varName,
varName1, varname, varName2);

It is just too messy if I have more than 20 lines like this.
Agreed, reached that same conclusion.
Therefore, I switched to something like

output="""for ($VAR = 1, $VAR < $START, $VAR < $END)"""


Uh, OT, but, doesn't this work better with semicolons than commas...?

output=string.replace(output,"$VAR",varName)
output=string.replace(output,"$START",varName1)
output=string.replace(output,"$END",varName2)

Obviously, this can be enhanced.
(use other conventions than $identifier, and performing the substitution
in a loop).
BTW: I am not sure string.replace works OK as shown here.


Yep, though output.replace(...) would be neater. But the best
replacement is already in the Python 2.4 standard library: it uses
exactly the $identifier convention, and takes a dictionary of mapping of
identifier to string...:

In [4]: tpl=string.Template('for ($VAR = 1, $VAR < $START, $VAR <
$END)')

In [5]: tpl % dict(VAR='foo', START='bar', END='baz')
Out[5]: u'for (foo = 1, foo < bar, foo < baz)'

In [6]: tpl % dict(VAR='fee', START='fie', END='fofum')
Out[6]: u'for (fee = 1, fee < fie, fee < fofum)'

Alex
Jul 18 '05 #7
Alex Martelli wrote:
output=string.replace(output,"$VAR",varName)
output=string.replace(output,"$START",varName1 )
output=string.replace(output,"$END",varName2)

Obviously, this can be enhanced.
(use other conventions than $identifier, and performing the substitution
in a loop).
BTW: I am not sure string.replace works OK as shown here.

Yep, though output.replace(...) would be neater. But the best
replacement is already in the Python 2.4 standard library: it uses
exactly the $identifier convention, and takes a dictionary of mapping of
identifier to string...:

In [4]: tpl=string.Template('for ($VAR = 1, $VAR < $START, $VAR <
$END)')

In [5]: tpl % dict(VAR='foo', START='bar', END='baz')
Out[5]: u'for (foo = 1, foo < bar, foo < baz)'

In [6]: tpl % dict(VAR='fee', START='fie', END='fofum')
Out[6]: u'for (fee = 1, fee < fie, fee < fofum)'


If you are not contrained to using $ identifiers:

d={'var':'foo', 'start':1, 'end': 100}
'for (%(var)s = %(start)i; %(var)s < %(end)i; %(var)s++)' % d

-> 'for (foo = 1; foo < 100; foo++)'

Regards
--
////
(@ @)
----------------------------oOO----(_)----OOo--------------------------
<> Ojo por ojo y el mundo acabara ciego
/\ Alexis Roda - Universitat Rovira i Virgili - Reus, Tarragona (Spain)
-----------------------------------------------------------------------

Jul 18 '05 #8
Alexis Roda <al*********@urv.es> wrote:
...
Yep, though output.replace(...) would be neater. But the best
replacement is already in the Python 2.4 standard library: it uses
exactly the $identifier convention, and takes a dictionary of mapping of
identifier to string...: ... In [6]: tpl % dict(VAR='fee', START='fie', END='fofum')
Out[6]: u'for (fee = 1, fee < fie, fee < fofum)'


If you are not contrained to using $ identifiers:

d={'var':'foo', 'start':1, 'end': 100}
'for (%(var)s = %(start)i; %(var)s < %(end)i; %(var)s++)' % d


yeah, but the template string for this long-standing Python feature is
hard to read and hard to write, which is why 2.4 finally grew a simpler
approach -- the string.Template class.
Alex

Jul 18 '05 #9
Alex Martelli wrote:
Alexis Roda <al*********@urv.es> wrote:
...
Yep, though output.replace(...) would be neater. But the best
replacement is already in the Python 2.4 standard library: it uses
exactly the $identifier convention, and takes a dictionary of mapping of
identifier to string...:
...
In [6]: tpl % dict(VAR='fee', START='fie', END='fofum')
Out[6]: u'for (fee = 1, fee < fie, fee < fofum)'


If you are not contrained to using $ identifiers:

d={'var':'foo', 'start':1, 'end': 100}
'for (%(var)s = %(start)i; %(var)s < %(end)i; %(var)s++)' % d

yeah, but the template string for this long-standing Python feature is
hard to read and hard to write, which is why 2.4 finally grew a simpler
approach -- the string.Template class.
Alex


If you come from Perl its not so hard :-). Anyway, I agree, templates
are nicer, but not everyone is running python 2.4 ...
Alex
--
////
(@ @)
----------------------------oOO----(_)----OOo--------------------------
<> Ojo por ojo y el mundo acabara ciego
/\ Alexis Roda - Universitat Rovira i Virgili - Reus, Tarragona (Spain)
-----------------------------------------------------------------------

Jul 18 '05 #10
Alex Martelli <aleaxit <at> yahoo.com> writes:
yeah, but the template string for this long-standing Python feature is
hard to read and hard to write, which is why 2.4 finally grew a simpler
approach -- the string.Template class.


Just to make the comparison as fair as possible:

2.4 Template solution
tpl = string.Template('for ($VAR = 1, $VAR < $START, $VAR < $END)')
tpl % dict(VAR='fee', START='fie', END='fofum')

<2.4 format string solution
tpl = 'for (%(VAR)s = 1, %(VAR)s < %(START)s, %(VAR)s < %(END)s)'
tpl % dict(VAR='fee', START='fie', END='fofum')

So, yes, the Template solution is cleaner, but not drastically so. It saves
you 3 characters per variable -- '(', ')' and 's'. Of course the more
variables you have to write, the bigger the deal it is, so I suspect the OP
would much prefer the Template solution.

Steve

Jul 18 '05 #11
On Wed, 8 Sep 2004 12:59:57 -0500, Greg Lindstrom
<gr************@novasyshealth.com> wrote:
How can I get the name (or ip) of the machine where my python script is
running? I'd like to add it to my log entry.

import socket

print socket.gethostname()
print socket.getfqdn()

this_host = socket_gethostname()
print socket.gethostbyname(this_host)

# the following will give several IP addresses if you are
#multi-homed

print socket.gethostbyname_ex(this_host)

Jul 18 '05 #12
Steven Bethard wrote:
So, yes, the Template solution is cleaner, but not drastically so.**It
saves you 3 characters per variable -- '(', ')' and 's'.**Of*course*the
more variables you have to write, the bigger the deal it is


True, and calling string.Template() is an extra 17 characters, so there
would have to be 6 variables in your string before you got a typing
payoff. :-) Of course, some people might prefer the look of the Template.

Jeffrey
Jul 18 '05 #13
Many thanks for your comments and advice!

Another concern is that how I can interface Python with Cup/Lex.
Writing parser is a no-joke and tedious to me, hence I would like to
re-use
the existing Cup/Lex scanner and parser.
Could anyone confirm that it is possible to do so? (I mean naturally
:)

In general, the program I am dealing with needs to manipulate strings
quite a lot. Eventhough we can certainly do with C++, it does not
appeal naturally to me.
Jul 18 '05 #14
- I'm familar with Java, C, C++, and Pascal, some experiences with
SML. Could you elaborate about some Python's string-manipulation
features?

- So now imagine if I have to convince my supervisor, an
theoretic-non-progammer, about switching to Python for this code
generator program, what kind of advantages I can get from doing so?

Thanks!
Tuan Anh
Jul 18 '05 #15
On 9 Sep 2004 07:09:10 -0700, Tran Tuan Anh <an***@hotmail.com> wrote:
- I'm familar with Java, C, C++, and Pascal, some experiences with
SML. Could you elaborate about some Python's string-manipulation
features?
I can never beat the online documentation:

http://www.python.org/doc, click 'Module index', click 'strings'.

I don't know where the string template thingies are described, but it
has to be there somewhere.
- So now imagine if I have to convince my supervisor, an
theoretic-non-progammer, about switching to Python for this code
generator program, what kind of advantages I can get from doing so?


Same:

http://www.python.org/ look in the 'Documentation links', in particular
the 'introductions to python'. There is a section of links to convince
those that control what we do (they think :-) ).
[ python documentation is awesome, very complete and very good. Browse
the website for answers to just about any question.
]
Albert
--
Unlike popular belief, the .doc format is not an open publically available format.
Jul 18 '05 #16
On Wed, 8 Sep 2004 17:47:16 +0200, Alex Martelli <al*****@yahoo.com> wrote:
Jeremy Bowers <je**@jerf.org> wrote:
...
> 2. Does Python has a parser? It seems to me that there is no-standard
> Parser for Python right now. If it is the case, can I interface Python
> with existing cup and lex?


Why are you asking about a parser when you are talking about *outputting*
code? Is there something left out of the problem description?


the original poster started his original post with:
I need your advice on this matter. I am working on a program which
takes some pieces of System-C code in and generate some other System-C


That "takes some pieces of ... code" sounded to me like an indication
that a parser is needed -- nothing left out of the problem description,
IMHO.


For the original poster:

I just discovered that there is a Topic guide about scanners/parsers at
python.org !!
It has quite a list of module links.
Albert
--
Unlike popular belief, the .doc format is not an open publically available format.
Jul 18 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Joe Cheng | last post by:
I'm curious about something... many Artima.com members who have a Java background and learned Python have come to the conclusion that Java and Python are highly complimentary languages. They would...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
4
by: Doug R | last post by:
I'm trying to create an anti-automation feature to prevent scripts from running reports on our website by requiring users to enter a code from an image (like Yahoo e-mail sign-up, or Network...
29
by: Maurice LING | last post by:
Hi, I remembered reading a MSc thesis about compiling Perl to Java bytecodes (as in java class files). At least, it seems that someone had compiled scheme to java class files quite successfully....
14
by: Philippe C. Martin | last post by:
Hi, I wish to use an easy way to generate reports from wxPython and feel wxHtmlEasyPrinting could be a good solution. I now need to generate the HTML wxHtmlEasyPrinting can print: I need to...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
5
by: Paul Brauner | last post by:
Hi, I'm working on a project that outputs several languages including (hopefully) python. My problem is that the generic backend architecture has not been designed to output correctly indented...
53
by: Vicent Giner | last post by:
Hello. I am new to Python. It seems a very interesting language to me. Its simplicity is very attractive. However, it is usually said that Python is not a compiled but interpreted programming...
3
by: eliben | last post by:
Hello, I'm building a parser in Python, and while pondering on the design of my ASTs had the idea to see what Python uses. I quickly got to the compiler.ast module, and understood it's...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.