Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed0.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'perl,': 0.05; 'sys': 0.05; '"""': 0.07; 'skip:{ 20': 0.07; 'python': 0.08; "'''": 0.09; '__name__': 0.09; 'filename': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:python': 0.10; 'url:moin': 0.12; '"w")': 0.16; "'__main__':": 0.16; '2.7.2': 0.16; 'ast,': 0.16; 'overview:': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; 'template': 0.19; 'later': 0.19; 'seems': 0.20; 'programming': 0.20; "haven't": 0.20; 'trying': 0.21; "doesn't": 0.22; 'from:addr:web.de': 0.23; 'string': 0.24; "python's": 0.24; 'suspect': 0.24; 'code.': 0.26; 'import': 0.27; "i'm": 0.28; 'script': 0.28; 'pass': 0.29; 'class': 0.29; 'example': 0.29; 'url:wiki': 0.29; 'logic': 0.30; 'rarely': 0.30; 'subject:source': 0.30; 'app': 0.31; 'actually': 0.31; 'file.': 0.31; 'there': 0.33; 'too': 0.33; 'file': 0.34; 'header:X-Complaints-To:1': 0.34; 'realize': 0.34; 'skip:# 10': 0.34; 'rather': 0.34; 'to:addr:python-list': 0.35; 'url:python': 0.35; 'received:org': 0.36; 'but': 0.37; 'using': 0.37; 'options': 0.37; 'subject:with': 0.37; 'could': 0.38; 'data': 0.38; 'url:org': 0.39; 'that.': 0.39; 'to:addr:python.org': 0.40; 'due': 0.66; 'fit.': 0.67; 'otten': 0.84; 'subject:write': 0.84; 'template,': 0.84; 'few.': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Need to write python source with python Date: Tue, 28 Feb 2012 20:25:33 +0100 Organization: None References: <23135461.7.1330450584442.JavaMail.geo-discussion-forums@pbcrt4> <6137475.17.1330453824069.JavaMail.geo-discussion-forums@pbjl1> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849a89.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330457084 news.xs4all.nl 6862 [2001:888:2000:d::a6]:33369 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:21000 crstop@gmail.com wrote: > On Tuesday, February 28, 2012 9:56:43 AM UTC-8, Peter Otten wrote: >> crstop@gmail.com 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.