Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'to:addr:comp.lang.python': 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; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'template': 0.19; 'later': 0.19; 'seems': 0.20; 'programming': 0.20; "haven't": 0.20; 'cc:no real name:2**0': 0.21; 'trying': 0.21; 'options.': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; "shouldn't": 0.23; 'string': 0.24; "python's": 0.24; 'suspect': 0.24; 'cc:2**0': 0.26; 'code.': 0.26; 'import': 0.27; "i'm": 0.28; 'script': 0.28; 'pass': 0.29; 'class': 0.29; 'example': 0.29; 'cc:addr:python.org': 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; 'thanks': 0.32; 'there': 0.33; 'header:User-Agent:1': 0.33; 'too': 0.33; 'file': 0.34; 'realize': 0.34; 'skip:# 10': 0.34; 'rather': 0.34; 'posters': 0.34; 'url:python': 0.35; 'received:209.85.160': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'options': 0.37; 'subject:with': 0.37; 'received:209.85': 0.38; 'could': 0.38; 'data': 0.38; 'url:org': 0.39; 'that.': 0.39; 'received:209': 0.39; 'from:no real name:2**0': 0.64; 'due': 0.66; 'fit.': 0.67; 'otten': 0.84; 'subject:write': 0.84; 'template,': 0.84; 'few.': 0.93 Received-SPF: pass (google.com: domain of crstop@gmail.com designates 10.68.228.197 as permitted sender) client-ip=10.68.228.197; Authentication-Results: mr.google.com; spf=pass (google.com: domain of crstop@gmail.com designates 10.68.228.197 as permitted sender) smtp.mail=crstop@gmail.com Newsgroups: comp.lang.python Date: Tue, 28 Feb 2012 11:36:00 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2001:480:10:160:5e26:aff:fe59:9b7d; posting-account=2FdWbgkAAACrRLNFffjv9vTu0_2N97nl References: <23135461.7.1330450584442.JavaMail.geo-discussion-forums@pbcrt4> <6137475.17.1330453824069.JavaMail.geo-discussion-forums@pbjl1> User-Agent: G2/1.0 X-Google-Web-Client: true MIME-Version: 1.0 Subject: Re: Need to write python source with python From: crstop@gmail.com To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: , Message-ID: Lines: 76 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330457764 news.xs4all.nl 6861 [2001:888:2000:d::a6]:39389 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:21002 On Tuesday, February 28, 2012 11:25:33 AM UTC-8, Peter Otten wrote: > 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. It shouldn't get very complicated so I look through those options. Thanks to all posters