473,466 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Do you know of a much simpler way of writing a program that writes aprogram?

Here is asample program that writes a program and then executes it.
Do you knowof a much simpler way of writing a program that writes a program?

"""
-----------------------------------------------------------------------------
Name: _writePythonCode.py
Purpose: This script writes Python code and thentransfers control to it.

Author: MCSEJUNG

Created: 2008/05/01
RCS-ID: $Id: _writePythonCode.py $
Copyright: (c) 2008
Licence: GPL
This program is free software; you canredistribute it and/or modify
it under the terms of the GNU General PublicLicense as published by
the Free Software Foundation; either version2 of the License
(at your option) any later version.

This program is distributed in the hope thatit will be useful,
but WITHOUT ANY WARRANTY; without even theimplied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULARPURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNUGeneral Public License
along with this program; if not, write tothe Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Maintenance:MCSEJUNG 2008/05/01 This script writes Python code and then transfers controlto it.
-----------------------------------------------------------------------------
"""
__version__= "$Revision: 80101 $"
# $Source$

# Rememberto document each paragraph with a purposeful short description

#if__name__ == "__main__":

#Print thestart time of this process
import time
start =time.localtime(time.time())
year,month, day, hour, minute, second, weekday, yearday, daylight = start
print"Process Started", "%02d:%02d:%02d" % (hour, minute,second)

header1 ="""
-----------------------------------------------------------------------------
Name: _generatedPython.py
Purpose: This script was built by_writePythonCode.py

Author: MCSEJUNG

Created: 2008/05/01
RCS-ID: $Id: _generatedPython.py $
Copyright: (c) 2008
Licence: GPL
This program is free software; you canredistribute it and/or modify
it under the terms of the GNU General PublicLicense as published by
the Free Software Foundation; either version2 of the License
(at your option) any later version.

This program is distributed in the hope thatit will be useful,
but WITHOUT ANY WARRANTY; without even theimplied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULARPURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNUGeneral Public License
along with this program; if not, write tothe Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Maintenance:MCSEJUNG 2008/05/01 This script was built by _writePythonCode.py
-----------------------------------------------------------------------------
"""

header2 ="""
# $Source$

# Rememberto document each paragraph with a purposeful short description

#if__name__ == "__main__":

#Print thestart time of this process
"""

filePathName= 'C:\\Python25\\Lib\\_myStuff'
fileInName= '_generatedPython.py'
filegTName= filePathName + '\\' +fileInName
gT =open(filegTName, 'w')

gT.write('"""'+ header1 + '"""' + '\n')
gT.write('__version__= "$Revision: 80101 $"'+ '\n')
gT.write('"""'+ header2 + '"""' + '\n')
gT.write('importtime'+'\n')
gT.write('start= time.localtime(time.time())'+'\n')
gT.write('year,month, day, hour, minute, second, weekday, yearday, daylight = start'+'\n')
gT.write('print"Generated Process Started", "%02d:%02d:%02d" % (hour,minute, second)'+'\n')
#-----------
gT.write('print"start"'+'\n')
gT.write('fori in [0,4,8,12,16,12,8,12,8,12,16,12,8,4,8,12,16,12,8,4, 0]:'+'\n')
gT.write(' print "-"*i'+'\n')
gT.write('print"end"'+'\n')
#-----------
gT.write('#Calculatethe processes duration; format and print the start time, stop time, andduration'+'\n')
gT.write('stop= time.localtime(time.time())'+'\n')
gT.write('syear,smonth, sday, shour, sminute, ssecond, sweekday, syearday, sdaylight =stop'+'\n')
gT.write(''+'\n')
gT.write('thour = shour'+'\n')
gT.write('tminute = sminute'+'\n')
gT.write('tsecond= ssecond'+'\n')
gT.write(''+'\n')
gT.write('iftsecond < second:'+'\n')
gT.write(' tminute = tminute - 1'+'\n')
gT.write(' dursecond = 60 + tsecond - second'+'\n')
gT.write('else:'+'\n')
gT.write(' dursecond = tsecond - second'+'\n')
gT.write(''+'\n')
gT.write('iftminute < minute:'+'\n')
gT.write(' thour = thour - 1'+'\n')
gT.write('
durminute = 60 + tminute - minute'+'\n')
gT.write('else:'+'\n')
gT.write('
durminute = tminute - minute'+'\n')
gT.write(''+'\n')
gT.write('ifthour < hour:'+'\n')
gT.write(' xday = 1'+'\n')
gT.write(' durhour = hour - thour'+'\n')
gT.write('else:'+'\n')
gT.write(' xday = 0'+'\n')
gT.write(' durhour = thour - hour'+'\n')
gT.write(''+'\n')
gT.write('ifxday == 0:'+'\n')
gT.write(' print "Generated Process Completed--", "start:", "%02d:%02d:%02d" % (hour, minute,second), "stop:", "%02d:%02d:%02d" % (shour, sminute,ssecond), "duration:", "%02d:%02d:%02d" % (durhour,durminute, dursecond);'+'\n')
gT.write('else:'+'\n')
gT.write(' print "Generated Process Completed--", "start:", "%02d:%02d:%02d" % (hour, minute,second), "stop:", "%02d:%02d:%02d" % (shour, sminute,ssecond), "duration:", "%02d:%02d:%02d" % (durhour,durminute, dursecond), "and one day";'+'\n')

gT.flush()
gT.close()

execfile("_generatedPython.py")

#Calculatethe processes duration; format and print the start time, stop time, andduration
stop =time.localtime(time.time())
syear,smonth, sday, shour, sminute, ssecond, sweekday, syearday, sdaylight = stop

thour = shour
tminute =sminute
tsecond =ssecond

if tsecond< second:
tminute = tminute - 1
dursecond = 60 + tsecond - second
else:
dursecond = tsecond - second

if tminute< minute:
thour = thour - 1
durminute = 60 + tminute - minute
else:
durminute =tminute - minute

if thour< hour:
xday = 1
durhour = hour - thour
else:
xday = 0
durhour = thour - hour

if xday ==0:
print "Process Completed --",'start:', "%02d:%02d:%02d" % (hour, minute, second), 'stop:',"%02d:%02d:%02d" % (shour, sminute, ssecond), 'duration:',"%02d:%02d:%02d" % (durhour, durminute, dursecond);
else:
print "Process Completed --",'start:', "%02d:%02d:%02d" % (hour, minute, second), 'stop:',"%02d:%02d:%02d" % (shour, sminute, ssecond), 'duration:',"%02d:%02d:%02d" % (durhour, durminute, dursecond), 'and one day';


__________________________________________________ __________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ
Jun 27 '08 #1
6 1237
On May 2, 3:50 pm, mcse jung <mcsej...@yahoo.comwrote:
Here is asample program that writes a program and then executes it.
Do you knowof a much simpler way of writing a program that writes a program?
Use a templating engine, such as Cheetah: http://www.cheetahtemplate.org/

Cheers,
Nicola Musatti
Jun 27 '08 #2
mcse jung wrote:
Here is asample program that writes a program and then executes it.
Do you knowof a much simpler way of writing a program that writes a program?
I'm not quite sure what you are trying to achieve here, but I bet there is a
simpler way to do it than by generating a script. You might want to look into
functions.

http://docs.python.org/tut/node6.htm...00000000000000

Stefan
Jun 27 '08 #3
mcse jung wrote:
Here is asample program that writes a program and then executes it.
Do you knowof a much simpler way of writing a program that writes a program?
It looks like you have a program which is more printed text than program
logic. In which case, the usual solution is to use a templating system,
where most of the text is unmodified and you only set out the specific
areas where you want to insert logic.

EmPy is one:

http://www.alcyone.com/software/empy/

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
Jun 27 '08 #4
On May 2, 10:40 am, Stefan Behnel <stefan...@behnel.dewrote:
mcse jung wrote:
Here is asample program that writes a program and then executes it.
Do you knowof a much simpler way of writing a program that writes a program?

I'm not quite sure what you are trying to achieve here, but I bet there is a
simpler way to do it than by generating a script. You might want to look into
functions.

http://docs.python.org/tut/node6.htm...00000000000000
Seriously, this looks like a DailyWTF entry.

George
Jun 27 '08 #5
Use lisp?
Jun 27 '08 #6
In article <28**********************************@w74g2000hsh. googlegroups.com>,
Jeff <je******@gmail.comwrote:
>
Use lisp?
:-)))))))
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

Help a hearing-impaired person: http://rule6.info/hearing.html
Jun 27 '08 #7

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

Similar topics

8
by: Lu | last post by:
Hi there, I got a program to write data to a randomly accessed file (the program moves file pointer to a certain position of the file according the current "keyword" and then writes data). It...
12
by: Colm | last post by:
Hi I'm having a wierd problem and I was wondering if anyone else had encountered something similar... I have a program which reads in a 5 digit number from a file in decimal format, converts...
3
by: Cliff | last post by:
Hi all. I'm working on a program that writes a line of text to a file during each iteration of a for loop. This for loop may run for quite a while and sometimes I would like to stop it ("Ctrl-C")...
29
by: Glen | last post by:
Is it possible to write a structure to a file in c...as in c++...?? is it using fwrite?? thanx glen
72
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
0
by: Bernard Lebel | last post by:
Hello, I have this strange problem. I have written a program that writes a log to a log file. To start the program (I'm on Linux Fedora Core 3), I used a service. This service runs a bash...
8
by: maxvalery | last post by:
Hi, Here's a worthy challenge for ya'll. I'm trying to build a questionnaire in php and need help understanding the logic behind the process. Here's an example that explains everything: 1)...
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
5
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.