本文整理汇总了Python中string.Template类的典型用法代码示例。如果您正苦于以下问题:Python Template类的具体用法?Python Template怎么用?Python Template使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Template类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _create_report
def _create_report(self):
context = aq_inner(self.context)
date = datetime.datetime.now()
token = django_random.get_random_string(length=24)
item = api.content.create(
type='xpose.seodash.report',
id=token,
title='Report {0} {1}'.format(date.strftime("%B"),
date.strftime("%Y")),
container=context,
safe_id=True
)
uuid = api.content.get_uuid(obj=item)
template_file = os.path.join(os.path.dirname(__file__),
'report.json')
template = Template(open(template_file).read())
template_vars = {
'id': uuid_tool.uuid4(),
'uid': uuid,
'timestamp': int(time.time()),
'created': datetime.datetime.now(),
'dashboard': api.content.get_uuid(obj=context),
}
report = template.substitute(template_vars)
setattr(item, 'report', report)
modified(item)
item.reindexObject(idxs='modified')
return uuid
示例2: writeMembers
def writeMembers(c):
members = members_begin
for m in c["members"]:
members += member.safe_substitute(membername=m["name"], membertype=m["type"], comment=m["comment"])
members += members_end
members = Template(members)
print(members.substitute(classname=c["name"]))
示例3: pattern_to_file
def pattern_to_file(input_l):
# 入力List[test_so,test_si,pi,pi,po]をテストパターンのリストに置き換える
# 入力List[{test_so='aaaa',..},{..}]
output = []
template = Template(' Ann {* fast_sequential *}\n' + \
' "pattern ${num}": Call "load_unload" { \n' + \
' "test_so"=${test_so}; ' + \
'"test_si"=${test_si}; }\n' + \
' Call "${launch}" { \n' + \
' "_pi"=${pi_1}; }\n' + \
' Call "${capture}" { \n' + \
' "_pi"=${pi_2}; "_po"=${po}; }\n')
template_first = Template(' Ann {* fast_sequential *}\n' + \
' "pattern ${num}": Call "load_unload" { \n' + \
' "test_si"=${test_si}; }\n' + \
' Call "${launch}" { \n' + \
' "_pi"=${pi_1}; }\n' + \
' Call "${capture}" { \n' + \
' "_pi"=${pi_2}; "_po"=${po}; }\n')
for i, l in enumerate(input_l):
l['num'] = i
if i == 0:
line = template_first.substitute(l)
output.append(line)
else:
#l['num'] = i + 1
line = template.substitute(l)
output.append(line)
return(output)
示例4: filter
def filter(self, ):
if (self.filters and len(self.filters) > 0):
self.filter_params = self.stage_dir + 'filters.txt'
try:
with open(self.filter_params, 'w') as f:
f.write('--keep="{0}"\n'.format(' or '.join(self.filters)))
except IOError as e:
logger.error('Error saving filter params file', e)
# can't filter so return the raw data
shutil.copy(self.raw_osm, self.filtered_osm)
return self.filtered_osm
# convert to om5 for faster processing
om5 = self._convert_om5()
# filter om5 data
filter_tmpl = Template(
'osmfilter $om5 --parameter-file=$params --out-osm >$filtered_osm'
)
filter_cmd = filter_tmpl.safe_substitute({'om5': om5,
'params': self.filter_params,
'filtered_osm': self.filtered_osm})
proc = subprocess.Popen(filter_cmd, shell=True, executable='/bin/bash',
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
returncode = proc.wait()
if (returncode != 0):
logger.error('%s', stderr)
raise Exception, "osmfilter process failed with returncode {0}".format(returncode)
return self.filtered_osm
else:
logger.error('No filters found. Returning raw osm data.')
shutil.copy(self.raw_osm, self.filtered_osm)
return self.filtered_osm
示例5: renderTemplates
def renderTemplates(inAndOutFilePaths, options, relPathToSdk, absPathToSdk, renderTarget, jobsAndDescs=[]):
for inFile, outFile in inAndOutFilePaths:
console.log("Patching file '%s'" % outFile)
#config = MyTemplate(open(inFile).read())
config = Template(open(inFile).read())
out = open(outFile, "w")
context = {
"Name": options.name,
"Namespace": options.namespace,
"NamespacePath" : (options.namespace).replace('.', '/'),
"REL_QOOXDOO_PATH": relPathToSdk,
"ABS_QOOXDOO_PATH": absPathToSdk,
"QOOXDOO_VERSION": QOOXDOO_VERSION,
"Cache" : options.cache,
}
if renderTarget == TARGET.GRUNT:
context["JOBS_AND_DESCS"] = jobsAndDescs
for k, v in context.iteritems():
if isinstance(v, (str, unicode)):
context[k] = gruntifyMacros(v);
out.write(config.substitute(context).encode('utf-8'))
out.close()
os.remove(inFile)
示例6: GenerateBridgeWrapperMethod
def GenerateBridgeWrapperMethod(self):
no_return_value = self._method_return == 'void'
return_is_internal = self.IsInternalClass(self._method_return)
if return_is_internal:
return_type_java_data = self.LoadJavaClass(self._method_return)
template = Template(
' public ${RETURN_TYPE} ${NAME}(${PARAMS}) {\n' +
' ${RETURN}ReflectionHelper.invokeMethod(\n' +
' ${METHOD_DECLARE_NAME}, wrapper${PARAMS_PASSING});\n' +
' }\n\n')
if no_return_value:
return_statement = ''
elif return_is_internal:
return_statement = 'return (%s)' % return_type_java_data.bridge_name
else:
return_statement = 'return (%s)' % (
ConvertPrimitiveTypeToObject(self.method_return))
value = {'RETURN_TYPE': self.method_return,
'NAME': self.method_name,
'METHOD_DECLARE_NAME': self._method_declare_name,
'PARAMS': self._bridge_params_declare,
'RETURN': return_statement,
'PARAMS_PASSING': self._bridge_params_pass_to_wrapper}
return template.substitute(value)
示例7: GenerateWrapperStaticMethod
def GenerateWrapperStaticMethod(self):
no_return_value = self._method_return == 'void'
template = Template(
' public static ${RETURN_TYPE} ${NAME}(${PARAMS}) {\n' +
' Class<?> clazz = ReflectionHelper.loadClass(' +
'\"${FULL_BRIDGE_NAME}\");\n' +
' Method method = ReflectionHelper.loadMethod(clazz, ' +
'\"${NAME}\"${PARAMS_DECLARE_FOR_BRIDGE});\n' +
' ${RETURN}ReflectionHelper.invokeMethod(method, null' +
'${PARAMS_PASSING});\n' +
' }\n\n')
if no_return_value:
return_state = ''
else:
return_state = 'return (%s)' % ConvertPrimitiveTypeToObject(
self.method_return)
value = {'RETURN_TYPE': self.method_return,
'NAME': self.method_name,
'FULL_BRIDGE_NAME': self._class_java_data.GetFullBridgeName(),
'PARAMS_DECLARE_FOR_BRIDGE':
self._wrapper_params_declare_for_bridge,
'PARAMS_PASSING': self._wrapper_params_pass_to_bridge,
'PARAMS': self._wrapper_params_declare,
'RETURN': return_state}
return template.substitute(value)
示例8: _download_and_format_issues
def _download_and_format_issues():
try:
from robot.utils import HtmlWriter, html_format
except ImportError:
sys.exit('creating release requires Robot Framework to be installed.')
URL = Template('http://code.google.com/p/robotframework-ride/issues/csv?'
'sort=priority+type&colspec=ID%20Type%20Priority%20Summary'
'&q=target%3A${version}&can=1')
reader = csv.reader(urlopen(URL.substitute({'version': VERSION})))
total_issues = 0
writer = HtmlWriter(StringIO())
writer.element('h2', 'Release notes for %s' % VERSION)
writer.start('table', attrs={'border': '1'})
for row in reader:
if not row or row[1] == 'Task':
continue
row = row[:4]
writer.start('tr')
if reader.line_num == 1:
row = [ '*%s*' % cell for cell in row ]
else:
row[0] = '<a href="http://code.google.com/p/robotframework-ride/'\
'issues/detail?id=%s">Issue %s</a>' % (row[0], row[0])
total_issues += 1
for cell in row:
if reader.line_num == 1:
cell = html_format(cell)
writer.element('td', cell, escape=False)
writer.end('tr')
writer.end('table')
writer.element('p', 'Altogether %d issues.' % total_issues)
return writer.output.getvalue()
示例9: send_mail
def send_mail(self, correlations, time, script):
"""Simple convenience function which sends an email \
from the configured sender to receivers.
:param correlations: number representing the combined \
number of threats to be reported.
:type correlations: :mod:`int`
:param time: the time it took for the calling program \
to execute and finish successfully.
:type time: :mod:`string`
:param script: the script which was invoked such that a \
detailed job description can be provided to correlation notifications.
:type time: :mod:`string`
"""
description = self.get_description(script)
message = Template("""
<br><img src="cid:image1" width="200"><br>
<p>You are receiving this email because you are subscribed to <b>Assurant's Threat Intelligence notification service</b>.</p>
<p><b>$corr threat correlation(s) have been identified</b> whilst running one of our threat correlation scripts.</p>
<p>Identified correlations relate to: <b>$desc</b>.</p>
<p>To view correlation(s) please visit the Kibana dashboard.</p>
<p>Time taken to identify correlations was <b>$dur seconds</b>.</p>
<p><i>To unsubscribe from this service please contact $sender</i>.</p>
""")
fullMessage = message.substitute(corr=correlations, dur=time, sender=sender, desc=description)
# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Assurant Threatelligence Update'
msgRoot['From'] = sender
msgRoot['To'] = receivers
msgRoot.preamble = 'This is a multi-part message in MIME format.'
# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
# We reference the image in the IMG SRC attribute by the ID we give it below
#msgRoot = MIMEText()
msgText = MIMEText(fullMessage, 'html')
msgAlternative.attach(msgText)
# This example assumes the image is in the current directory
fp = open('assurant-logo.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(sender, '')
smtpObj.sendmail(sender, receivers, msgRoot.as_string())
smtpObj.quit()
示例10: call_realign_single
def call_realign_single(java, gatk, ref_seq, blocks, input_list, output_map, target_intervals, known_vcfs, mem="8g"):
known_str = " ".join(list("-known %s" % (a) for a in known_vcfs))
template = Template("""
${JAVA}
-Xmx${MEM} -XX:ParallelGCThreads=2 -jar ${GATK}
-T IndelRealigner
-R ${REF_SEQ}
-disable_auto_index_creation_and_locking_when_reading_rods
-I ${INPUT_LIST}
-targetIntervals ${TARGET_INTERVALS}
${KNOWN_STR}
-nWayOut ${OUTPUT_MAP}
""".replace("\n", " "))
#for i, block in enumerate(fai_chunk( ref_seq + ".fai", block_size ) ):
#for i, block in enumerate(blocks):
cmd = template.substitute(
dict(
JAVA=java,
REF_SEQ=ref_seq,
GATK=gatk,
#BLOCK_NUM=i,
#INTERVAL="%s:%s-%s" % (block[0], block[1], block[2]),
#INTERVAL="%s" % (block),
INPUT_LIST=input_list,
#OUTPUT_BASE=output_base,
OUTPUT_MAP=output_map,
KNOWN_STR=known_str,
TARGET_INTERVALS=target_intervals,
MEM=mem
)
)
return cmd
示例11: call_scan
def call_scan(java, gatk, ncpus, ref_seq, input_list, known_vcfs, intervals, mem="8g"):
known_str = " ".join(list("-known %s" % (a) for a in known_vcfs))
#out = "%s.intervals" % (output_base)
out = intervals
template = Template("""
${JAVA}
-Xmx${MEM} -XX:ParallelGCThreads=2 -jar ${GATK}
-T RealignerTargetCreator
-disable_auto_index_creation_and_locking_when_reading_rods
-nt ${NCPUS}
-R ${REF_SEQ}
-I ${INPUT_LIST}
${KNOWN_STR}
-o ${OUT}
""".replace("\n", " "))
#output_base
cmd = template.substitute(
dict(
JAVA=java,
REF_SEQ=ref_seq,
GATK=gatk,
INPUT_LIST=input_list,
# OUTPUT_BASE=output_base,
KNOWN_STR=known_str,
NCPUS=ncpus,
OUT=out,
MEM=mem
))
return cmd #, out
示例12: addressOrIntersection
def addressOrIntersection(cross_street0, cross_street1) :
place = None
street_address = Template("$street_0")
intersection = Template("$street_0 & $street_1")
if cross_street0 :
# Sometimes street addresses are written out, particularly
# 'One' and sometimes 'Two.' Right now we are only catching
# addresses that start with a sequence of numbers a space and
# something that is not a number or whitespace (ideally an
# letter.
if re.compile('\d+ [\D\W]').match(cross_street0) :
place = street_address.substitute(street_0 = cross_street0)
else :
if cross_street1 and cross_street1 != cross_street0 :
# Likely fertile place for improvement
xstreet_splitter = re.compile('(.+)( and |/)(.+)')
xstreet_split = xstreet_splitter.findall(cross_street1)
if xstreet_split :
xstreet0 = xstreet_split[0][0].strip()
xstreet1 = xstreet_split[0][2].strip()
else :
xstreet0 = cross_street0
xstreet1 = cross_street1
xstreet0 = xstreet0.title()
xstreet1 = xstreet1.title()
place = intersection.substitute(street_0 = xstreet0,
street_1 = xstreet1)
return place
示例13: gen_qsub_script
def gen_qsub_script(script, name='qsub_magic', nodes=1, ppn=1, walltime='01:00:00',
mailto='[email protected]', path=os.getcwd(), pre=[], post=[], logfile='',
isdonefile=''):
''' Generate a template to be runned using qsub.'''
tplate = Template('\n'.join(
['#!/bin/sh',
'#PBS -S /bin/sh',
'#PBS -N $name',
'#PBS -j oe',
'#PBS -l nodes=$nodes:ppn=$ppn,walltime=$walltime',
'#PBS -M $mailto',
'#PBS -m abe',
'cd $path',
'ulimit -s unlimited'] +
pre +
[ 'python $script > $logfile &&',
' echo 0 > $isdonefile ||',
' echo 1 > $isdonefile'] +
post +
['return 0']))
qsub_script = tplate.substitute(name=name, nodes=nodes, ppn=ppn,
walltime=walltime, mailto=mailto,
path=path, script=script, logfile=logfile,
isdonefile=isdonefile)
return qsub_script
示例14: replace_template
def replace_template(infile, vars, outfile):
with open(infile, "r") as template_file:
temp = Template(template_file.read())
result = temp.substitute(vars)
with open(outfile, "w") as out:
out.write(result)
示例15: GetMethodIDImpl
def GetMethodIDImpl(self, called_by_native):
"""Returns the implementation of GetMethodID."""
template = Template("""\
base::android::MethodID::LazyGet<
base::android::MethodID::TYPE_${STATIC}>(
env, ${JAVA_CLASS}_clazz(env),
"${JNI_NAME}",
${JNI_SIGNATURE},
&g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME});
""")
jni_name = called_by_native.name
jni_return_type = called_by_native.return_type
if called_by_native.is_constructor:
jni_name = '<init>'
jni_return_type = 'void'
if called_by_native.signature:
signature = called_by_native.signature
else:
signature = self.jni_params.Signature(called_by_native.params,
jni_return_type, True)
java_class = self.fully_qualified_class
if called_by_native.java_class_name:
java_class += '$' + called_by_native.java_class_name
values = {
'JAVA_CLASS': GetBinaryClassName(java_class),
'JNI_NAME': jni_name,
'METHOD_ID_VAR_NAME': called_by_native.method_id_var_name,
'STATIC': 'STATIC' if called_by_native.static else 'INSTANCE',
'JNI_SIGNATURE': signature,
}
return template.substitute(values)