Need to write python source with python

Discussion in 'Python' started by crstop@gmail.com, Feb 28, 2012.

  1. Guest

    Hi All,

    I'm new to Python but have experience with a few other programming languages(Java, Perl, JavaScript).

    I'm using Python 2.7.2 and I'm trying to create and write to a file (.py) apython class and functions from python. I will also need to later read andedit the file. I realize I could just write strings using the available string and file writing methods but suspect there is a better way than that.

    I have read about pickle, ast, and Django; searched this group and the web but haven't found a solution that seems to fit. Any suggestion?
    , Feb 28, 2012
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    > I'm new to Python but have experience with a few other programming
    > languages(Java, Perl, JavaScript).
    >
    > I'm using Python 2.7.2 and I'm trying to create and write to a file (.py)
    > a python class and functions from python. I will also need to later read
    > and edit the file. I realize I could just write strings using the
    > available string and file writing methods but suspect there is a better
    > way than that.
    >
    > I have read about pickle, ast, and Django; searched this group and the web
    > but haven't found a solution that seems to fit. Any suggestion?


    Due to Python's dynamic nature it is rarely necessary to generate Python
    code. What are you actually trying to achieve?
    Peter Otten, Feb 28, 2012
    #2
    1. Advertising

  3. 在 2012å¹´2月29日星期三UTC+8上åˆ1æ—¶56分43秒,Peter Otten写é“:
    > wrote:
    >
    > > I'm new to Python but have experience with a few other programming
    > > languages(Java, Perl, JavaScript).
    > >
    > > I'm using Python 2.7.2 and I'm trying to create and write to a file (.py)
    > > a python class and functions from python. I will also need to later read
    > > and edit the file. I realize I could just write strings using the
    > > available string and file writing methods but suspect there is a better
    > > way than that.
    > >
    > > I have read about pickle, ast, and Django; searched this group and the web
    > > but haven't found a solution that seems to fit. Any suggestion?

    >
    > Due to Python's dynamic nature it is rarely necessary to generate Python
    > code. What are you actually trying to achieve?


    Check myHDL, and BOA and pythoncard that can translate user messages
    to pyhton code as delphie.
    88888 Dihedral, Feb 28, 2012
    #3
  4. Guest

    On Tuesday, February 28, 2012 9:56:43 AM UTC-8, Peter Otten wrote:
    > wrote:
    >
    > > I'm new to Python but have experience with a few other programming
    > > languages(Java, Perl, JavaScript).
    > >
    > > I'm using Python 2.7.2 and I'm trying to create and write to a file (.py)
    > > a python class and functions from python. I will also need to later read
    > > and edit the file. I realize I could just write strings using the
    > > available string and file writing methods but suspect there is a better
    > > way than that.
    > >
    > > I have read about pickle, ast, and Django; searched this group and the web
    > > but haven't found a solution that seems to fit. Any suggestion?

    >
    > Due to Python's dynamic nature it is rarely necessary to generate Python
    > code. What are you actually trying to achieve?


    I'm trying to generate the script file that will launch a PythonCard resource file.

    very basic example from the documentation.

    #!/usr/bin/python
    """
    __version__ = "$Revision: 1.10 $"
    __date__ = "$Date: 2004/04/24 22:13:31 $"
    """

    from PythonCard import model

    class Minimal(model.Background):
    pass

    if __name__ == '__main__':
    app = model.Application(Minimal)
    app.MainLoop()
    , Feb 28, 2012
    #4
  5. Guest

    On Tuesday, February 28, 2012 9:56:43 AM UTC-8, Peter Otten wrote:
    > wrote:
    >
    > > I'm new to Python but have experience with a few other programming
    > > languages(Java, Perl, JavaScript).
    > >
    > > I'm using Python 2.7.2 and I'm trying to create and write to a file (.py)
    > > a python class and functions from python. I will also need to later read
    > > and edit the file. I realize I could just write strings using the
    > > available string and file writing methods but suspect there is a better
    > > way than that.
    > >
    > > I have read about pickle, ast, and Django; searched this group and the web
    > > but haven't found a solution that seems to fit. Any suggestion?

    >
    > Due to Python's dynamic nature it is rarely necessary to generate Python
    > code. What are you actually trying to achieve?


    I'm trying to generate the script file that will launch a PythonCard resource file.

    very basic example from the documentation.

    #!/usr/bin/python
    """
    __version__ = "$Revision: 1.10 $"
    __date__ = "$Date: 2004/04/24 22:13:31 $"
    """

    from PythonCard import model

    class Minimal(model.Background):
    pass

    if __name__ == '__main__':
    app = model.Application(Minimal)
    app.MainLoop()
    , Feb 28, 2012
    #5
  6. Peter Otten Guest

    wrote:

    > On Tuesday, February 28, 2012 9:56:43 AM UTC-8, Peter Otten wrote:
    >> wrote:
    >>
    >> > I'm new to Python but have experience with a few other programming
    >> > languages(Java, Perl, JavaScript).
    >> >
    >> > I'm using Python 2.7.2 and I'm trying to create and write to a file
    >> > (.py) a python class and functions from python. I will also need to
    >> > later read and edit the file. I realize I could just write strings
    >> > using the available string and file writing methods but suspect there
    >> > is a better way than that.
    >> >
    >> > I have read about pickle, ast, and Django; searched this group and the
    >> > web but haven't found a solution that seems to fit. Any suggestion?

    >>
    >> Due to Python's dynamic nature it is rarely necessary to generate Python
    >> code. What are you actually trying to achieve?

    >
    > I'm trying to generate the script file that will launch a PythonCard
    > resource file.
    >
    > very basic example from the documentation.
    >
    > #!/usr/bin/python
    > """
    > __version__ = "$Revision: 1.10 $"
    > __date__ = "$Date: 2004/04/24 22:13:31 $"
    > """
    >
    > from PythonCard import model
    >
    > class Minimal(model.Background):
    > pass
    >
    > if __name__ == '__main__':
    > app = model.Application(Minimal)
    > app.MainLoop()


    If it doesn't get too complex you could start with Python's built-in string
    formatting:

    import sys

    template = '''\
    #!/usr/bin/python
    from PythonCard import model

    class {Class}(model.Background):
    pass

    if __name__ == '__main__':
    app = model.Application({Class})
    app.MainLoop()
    '''

    resourcename, filename = sys.argv[1:]

    with open(resourcename, "U") as f:
    data = eval(f.read())

    with open(filename, "w") as f:
    f.write(template.format(Class=data["application"]["name"]))

    If you need logic inside the template, here's on overview:

    http://wiki.python.org/moin/Templating

    So there are rather too many options than too few.
    Peter Otten, Feb 28, 2012
    #6
  7. Guest

    On Tuesday, February 28, 2012 11:25:33 AM UTC-8, Peter Otten wrote:
    > wrote:
    >
    > > On Tuesday, February 28, 2012 9:56:43 AM UTC-8, Peter Otten wrote:
    > >> wrote:
    > >>
    > >> > I'm new to Python but have experience with a few other programming
    > >> > languages(Java, Perl, JavaScript).
    > >> >
    > >> > I'm using Python 2.7.2 and I'm trying to create and write to a file
    > >> > (.py) a python class and functions from python. I will also need to
    > >> > later read and edit the file. I realize I could just write strings
    > >> > using the available string and file writing methods but suspect there
    > >> > is a better way than that.
    > >> >
    > >> > I have read about pickle, ast, and Django; searched this group and the
    > >> > web but haven't found a solution that seems to fit. Any suggestion?
    > >>
    > >> Due to Python's dynamic nature it is rarely necessary to generate Python
    > >> code. What are you actually trying to achieve?

    > >
    > > I'm trying to generate the script file that will launch a PythonCard
    > > resource file.
    > >
    > > very basic example from the documentation.
    > >
    > > #!/usr/bin/python
    > > """
    > > __version__ = "$Revision: 1.10 $"
    > > __date__ = "$Date: 2004/04/24 22:13:31 $"
    > > """
    > >
    > > from PythonCard import model
    > >
    > > class Minimal(model.Background):
    > > pass
    > >
    > > if __name__ == '__main__':
    > > app = model.Application(Minimal)
    > > app.MainLoop()

    >
    > If it doesn't get too complex you could start with Python's built-in string
    > formatting:
    >
    > import sys
    >
    > template = '''\
    > #!/usr/bin/python
    > from PythonCard import model
    >
    > class {Class}(model.Background):
    > pass
    >
    > if __name__ == '__main__':
    > app = model.Application({Class})
    > app.MainLoop()
    > '''
    >
    > resourcename, filename = sys.argv[1:]
    >
    > with open(resourcename, "U") as f:
    > data = eval(f.read())
    >
    > with open(filename, "w") as f:
    > f.write(template.format(Class=data["application"]["name"]))
    >
    > If you need logic inside the template, here's on overview:
    >
    > http://wiki.python.org/moin/Templating
    >
    > So there are rather too many options than too few.


    It shouldn't get very complicated so I look through those options.

    Thanks to all posters
    , Feb 28, 2012
    #7
  8. Guest

    On Tuesday, February 28, 2012 11:25:33 AM UTC-8, Peter Otten wrote:
    > wrote:
    >
    > > On Tuesday, February 28, 2012 9:56:43 AM UTC-8, Peter Otten wrote:
    > >> wrote:
    > >>
    > >> > I'm new to Python but have experience with a few other programming
    > >> > languages(Java, Perl, JavaScript).
    > >> >
    > >> > I'm using Python 2.7.2 and I'm trying to create and write to a file
    > >> > (.py) a python class and functions from python. I will also need to
    > >> > later read and edit the file. I realize I could just write strings
    > >> > using the available string and file writing methods but suspect there
    > >> > is a better way than that.
    > >> >
    > >> > I have read about pickle, ast, and Django; searched this group and the
    > >> > web but haven't found a solution that seems to fit. Any suggestion?
    > >>
    > >> Due to Python's dynamic nature it is rarely necessary to generate Python
    > >> code. What are you actually trying to achieve?

    > >
    > > I'm trying to generate the script file that will launch a PythonCard
    > > resource file.
    > >
    > > very basic example from the documentation.
    > >
    > > #!/usr/bin/python
    > > """
    > > __version__ = "$Revision: 1.10 $"
    > > __date__ = "$Date: 2004/04/24 22:13:31 $"
    > > """
    > >
    > > from PythonCard import model
    > >
    > > class Minimal(model.Background):
    > > pass
    > >
    > > if __name__ == '__main__':
    > > app = model.Application(Minimal)
    > > app.MainLoop()

    >
    > If it doesn't get too complex you could start with Python's built-in string
    > formatting:
    >
    > import sys
    >
    > template = '''\
    > #!/usr/bin/python
    > from PythonCard import model
    >
    > class {Class}(model.Background):
    > pass
    >
    > if __name__ == '__main__':
    > app = model.Application({Class})
    > app.MainLoop()
    > '''
    >
    > resourcename, filename = sys.argv[1:]
    >
    > with open(resourcename, "U") as f:
    > data = eval(f.read())
    >
    > with open(filename, "w") as f:
    > f.write(template.format(Class=data["application"]["name"]))
    >
    > If you need logic inside the template, here's on overview:
    >
    > http://wiki.python.org/moin/Templating
    >
    > So there are rather too many options than too few.


    It shouldn't get very complicated so I look through those options.

    Thanks to all posters
    , Feb 28, 2012
    #8
    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. =?Utf-8?B?QWJlbCBDaGFu?=
    Replies:
    6
    Views:
    9,252
    Rahul
    May 3, 2006
  2. Peter Hansen
    Replies:
    1
    Views:
    364
    Joona I Palaste
    Aug 24, 2003
  3. JasBascom
    Replies:
    1
    Views:
    354
    John Harrison
    Feb 24, 2004
  4. Peter Hansen
    Replies:
    1
    Views:
    407
    Joona I Palaste
    Aug 24, 2003
  5. sahm
    Replies:
    4
    Views:
    663
Loading...

Share This Page