在这里发表评论:如何定义一个新的字符串格式化程序,我尝试了子类化string.Formatter.这就是我所做的.不幸的是,我似乎在这个过程中打破了它
import string
from math import floor, log10
class CustFormatter(string.Formatter):
"Defines special formatting"
def __init__(self):
super(CustFormatter, self).__init__()
def powerise10(self, x):
if x == 0: return 0, 0
Neg = x < 0
if Neg: x = -x
a = 1.0 * x / 10**(floor(log10(x)))
b = int(floor(log10(x)))
if Neg: a = -a
return a, b
def eng(self, x):
a, b = self.powerise10(x)
if -3 < b < 3: return "%.4g" % x
a = a * …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
StringTemplate st = new StringTemplate("$msg$");
st.SetAttribute("msg", "Hello $usr$");
st.SetAttribute("usr", "Jakub");
Console.WriteLine(st);
// current output: "Hello $usr$"
// expected output: "Hello Jakub"
Run Code Online (Sandbox Code Playgroud)
有人知道如何强制StringTemplate来评估$usr$属性吗?
这没有任何显示:
$ Articles:{$ if(i!= 1)$显示在文章之间$ endif $ $ it.Text
}
这显示为预期
$ Articles:{显示文章$ it.Text
}
关于如何使这个工作的任何想法?
我在StringTemplate的介绍中注意到以下内容:
StringTemplate通过在对象o中查找属性p来解释op.查找规则在语言端口之间略有不同,但通常它们遵循旧的JavaBeans命名约定.StringTemplate首先查找方法getP(),isP(),hasP().如果找不到其中一个方法,它会查找一个名为p的字段.
本文似乎没有这篇文章:http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf
这是否打开了违反模型/视图分离的大门,主要是允许模型通过调用方法来提取数据?一个糟糕的程序员可能会编写一个导致副作用的方法getP().ST如何"严格"执行此处的关注点分离?
在我当前的项目中,我试图链接用xtext编写的DSL规范和用StringTemplate编写的代码生成器.
例如,我的DSL规范的语法如下.我通过xText提供的很好的编辑器工具输入这些信息.
structs:
TempStruct
tempValue : double;
unitOfMeasurement : String;
abilities :
sensors:
TemperatureSensor
generate tempMeasurement : TempStruct;
attribute responseFormat : String;
Run Code Online (Sandbox Code Playgroud)
上述DSL规范的语法如下:
VocSpec:
'structs' ':'
(structs += Struct)+
'abilities' ':'
('sensors' ':' (sensors += Sensor)+ )+
;
Sensor:
name = ID
((attributes += Attributes ) |
(sources += Sources))*
;
Sources:
'generate' name=ID ':' type = Type ';'
;
Attributes:
'attribute' name=ID ':' type = Type ';'
;
Struct:
name = ID
(fields += Field)+
;
Field:
name=ID ':' …Run Code Online (Sandbox Code Playgroud) 我正在使用StringTemplate V4在我的项目中生成一些HTML代码。我需要有HTML格式在我的模板,所以使用默认的分隔符<,并>会非常尴尬。
因此,我正在创建一个将定界符作为参数传递的组(如本问题所建议),但它根本行不通。
这是我的测试代码:
public void testTemplate() {
char sep = '$';
STGroup stGroup = new STGroupString("temp",
"<html>hello, $name$!</html>", sep, sep);
System.out.println("Group created");
ST st = stGroup.getInstanceOf("temp");
if (st == null) {
System.out.println("Failed to get template!");
} else {
st.add("name", "Guest");
System.out.println("Template initialized correctly");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
temp 1:1: invalid character '<'
temp 1:5: invalid character '>'
temp 1:1: garbled template definition starting at 'html'
temp 1:6: garbled template definition starting at 'hello'
temp …Run Code Online (Sandbox Code Playgroud) 在StringTemplate 4中,迭代的默认行为是迭代keys而不是values版本3中的行为.
我找不到如何通过迭代的语法keys 和 values在同一时间为4版本.
有人可以发布一个语法示例吗?
我试图通过使用python字符串模板为mutate_model.py脚本(http://salilab.org/modeller/wiki/Mutate%20model)制作我自己的模板,其中我替换了这五个变量Model,resType,resPos,pdb的值,链以重新编写并使用值写入新文件,但我收到如下错误:
MyAttempt:
import os
import re
import sys
import itertools
from modeller import *
from docopt import docopt
from string import Template
from modeller.automodel import *
from os.path import join, getsize
from modeller.scripts import complete_pdb
Model="3o26"
resType="A"
resPos="275"
pdb="3o26.pdb"
chain="A"
dir=os.getcwd()
str = '''import sys
import os
from modeller import *
from modeller.optimizers import molecular_dynamics, conjugate_gradients
from modeller.automodel import autosched
def optimize(atmsel, sched):
for step in sched:
step.optimize(atmsel, max_iterations=200, min_atom_shift=0.001)
refine(atmsel)
cg = conjugate_gradients()
cg.optimize(atmsel, max_iterations=200, min_atom_shift=0.001)
def …Run Code Online (Sandbox Code Playgroud) 现在,我正在尝试用 Kotlin 重写我的 java 应用程序。然后,我遇到了日志语句,比如
log.info("do the print thing for {}", arg);
Run Code Online (Sandbox Code Playgroud)
所以我有两种方法可以在 Kotlin 中执行日志操作,例如log.info("do the print thing for {}", arg)和log.info("do the print thing for $arg")。第一个是 Slf4j 或 Log4j 等框架的委托格式;第二个是使用 Kotlin 字符串模板。
那么它们有什么区别,哪个性能更好呢?
对于Python中的标准库string template,是否有一个函数可以获取所有标识符的列表?
例如,使用以下 xml 文件:
<Text>Question ${PrimaryKey}:</Text>
<Text>Cheat: ${orientation}</Text>
Run Code Online (Sandbox Code Playgroud)
该函数将返回类似的内容PrimaryKey, orientation