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

Discussion in 'Python' started by mcse jung, May 2, 2008.

  1. mcse jung

    mcse jung Guest

    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=Ahu06i62sR8HDtDypao8Wcj9tAcJ
    mcse jung, May 2, 2008
    #1
    1. Advertising

  2. Re: Do you know of a much simpler way of writing a program thatwrites a program?

    On May 2, 3:50 pm, 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?


    Use a templating engine, such as Cheetah: http://www.cheetahtemplate.org/

    Cheers,
    Nicola Musatti
    Nicola Musatti, May 2, 2008
    #2
    1. Advertising

  3. Re: Do you know of a much simpler way of writing a program that writesa program?

    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.html#SECTION006600000000000000000

    Stefan
    Stefan Behnel, May 2, 2008
    #3
  4. Re: Do you know of a much simpler way of writing a program thatwrites a program?

    On May 2, 10:40 am, Stefan Behnel <> wrote:
    > 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.html#SECTION006600000000000000000


    Seriously, this looks like a DailyWTF entry.

    George
    George Sakkis, May 2, 2008
    #4
  5. mcse jung

    Jeff Guest

    Re: Do you know of a much simpler way of writing a program thatwrites a program?

    Use lisp?
    Jeff, May 2, 2008
    #5
  6. mcse jung

    Aahz Guest

    Aahz, May 3, 2008
    #6
    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. Tom Widmer
    Replies:
    11
    Views:
    488
    Erik Arner
    Nov 24, 2004
  2. Erik Arner
    Replies:
    5
    Views:
    423
    Rob Williscroft
    Nov 23, 2004
  3. Replies:
    0
    Views:
    558
  4. Replies:
    2
    Views:
    114
  5. Benoit Fries
    Replies:
    1
    Views:
    76
    Thomas 'PointedEars' Lahn
    Nov 14, 2003
Loading...

Share This Page