String handling and the percent operator

Brett g Porter bgporter at acm.org
Thu Jul 13 21:14:42 EDT 2006


Justin Azoff wrote:
> Tom Plunket wrote:
>> 	boilerplate = \
>> """
> [big string]
>> """
>>
>> 	return boilerplate % ((module,) * 3)
>>
[deletia...]

> Of course..
> 
>>>> stuff = {'lang': 'python', 'page': 'typesseq-strings.html'}
>>>> print """I should read the %(lang)s documentation at
> ... http://docs.%(lang)s.org/lib/%(page)s""" % stuff
> I should read the python documentation at
> http://docs.python.org/lib/typesseq-strings.html

...or perhaps cooler:
http://docs.python.org/lib/node109.html

something like:

from string import Template

def GetPyContents(module):
	boilerplate = Template( \
"""
class $moduleName:
	pass
	
if __name__ == '__main__':
	import unittest
	unittest.main('${moduleName}_t')
""")
	
	return boilerplate.substitute(moduleName=module)



More information about the Python-list mailing list