标签: string-interpolation

是否可以暂时禁用Python的字符串插值?

我使用python的日志记录模块设置了一个python记录器.我想使用ConfigParser模块将我正在使用的字符串与记录Formatter对象一起存储在配置文件中.

格式字符串存储在单独文件中的设置字典中,该文件处理配置文件的读取和写入.我遇到的问题是python仍然会尝试格式化文件,并在读取所有特定于日志模块的格式化标志时失败.

{
    "log_level":logging.debug,
    "log_name":"C:\\Temp\\logfile.log",
    "format_string":
        "%(asctime)s %(levelname)s: %(module)s, line %(lineno)d - %(message)s"
}
Run Code Online (Sandbox Code Playgroud)

我的问题很简单:如何在此处禁用格式化功能,同时将其保留在其他位置.我最初的反应是大量使用反斜杠来逃避各种百分比符号,但这当然会永久地打破格式化,即使我需要它也不会工作.

我还应该提一下,因为它在评论中被买了,所以ConfigParser做了一些导致跳闸的内部插值.这是我的追溯:

Traceback (most recent call last):
  File "initialconfig.py", line 52, in <module>
    "%(asctime)s %(levelname)s: %(module)s, line %(lineno)d - %(message)s"
  File "initialconfig.py", line 31, in add_settings
    self.set(section_name, setting_name, default_value)
  File "C:\Python26\lib\ConfigParser.py", line 668, in set
    "position %d" % (value, m.start()))
ValueError: invalid interpolation syntax in '%(asctime)s %(levelname)s: %(module
)s, line %(lineno)d - %(message)s' at position 10
Run Code Online (Sandbox Code Playgroud)

此外,关于良好的设置 - 文件实践的一般指针将是很好的.这是我第一次使用ConfigParser做任何重要事情(或者记录此事).

多谢,提前谢谢

python string-interpolation

12
推荐指数
4
解决办法
8113
查看次数

慢s3 url生成回形针

我们正在尝试优化我们的视图,并在使用以下代码加载40张图片的页面上:

= image_tag(product.pictures.first.data.url(:gallery))
Run Code Online (Sandbox Code Playgroud)

如果我们将其更改为以下代码,则加载时间为840毫秒:

= image_tag("http://bucketname.s3.amazonaws.com/products/#{product.pictures.first.id}/gallery.jpg?1325844462"
Run Code Online (Sandbox Code Playgroud)

我们成为220ms的加载时间.

这意味着s3_path_url的插值非常慢.有人还在期待同样的问题吗?目前我创建了一个生成我的网址的助手:

def picture_url(picture, style)
  "http://bucketname.s3.amazonaws.com/products/#{picture.id}/#{style}.jpg"
end
Run Code Online (Sandbox Code Playgroud)

我这里唯一的问题是缓存键不存在而且扩展名不存在.

performance paperclip string-interpolation ruby-on-rails-3

12
推荐指数
1
解决办法
735
查看次数

使用Format Scala字符串插值,如何更改语言环境?

在瑞典进行格式字符串插值时,在创建带十进制数的字符串时,我得到逗号而不是点:

scala> val a = 5.010
a: Double = 5.01

scala> val a = 5.0101
a: Double = 5.0101

scala> f"$a%.2f"
res0: String = 5,01
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何设置格式以便得到结果5.01?我希望能够仅为该String设置语言环境,即这样我就不会更改整个环境的语言环境.

干杯,

约翰

string scala string-interpolation

12
推荐指数
1
解决办法
2121
查看次数

用于字符串插值的Swift协议

我必须实现什么协议来控制Swift中字符串插值中对象的表示方式?

我不想用这样的东西来指定得到的东西:

struct A{

}

var a = A()
println("\(a)")
Run Code Online (Sandbox Code Playgroud)

protocols string-interpolation swift

12
推荐指数
1
解决办法
6248
查看次数

C#5中不提供"插值字符串"功能.请使用语言版本6或更高版本.

当我放入Razor View时,以下行无法编译.

var extPropLookupNameCompania = $"extension_{SettingsHelper.ClientId.Replace("-", "")}_{"Compania"}";
Run Code Online (Sandbox Code Playgroud)

但是在控制器中,同一条线路工作得很好.

为什么我不能在剃刀视图上进行用户字符串插值?或者我可能需要配置一些东西?

c# string-interpolation asp.net-mvc-5

12
推荐指数
1
解决办法
5155
查看次数

使用字符串插值重载的字符串方法

为什么字符串插值更喜欢方法的重载string而不是IFormattable

想象一下:

static class Log {
    static void Debug(string message);
    static void Debug(IFormattable message);
    static bool IsDebugEnabled { get; }
}
Run Code Online (Sandbox Code Playgroud)

我有非常昂贵的物品ToString().以前,我做过以下事情:

if (Log.IsDebugEnabled) Log.Debug(string.Format("Message {0}", expensiveObject));
Run Code Online (Sandbox Code Playgroud)

现在,我希望内部有IsDebugEnabled逻辑Debug(IFormattable),并且仅在必要时才在消息中的对象上调用ToString().

Log.Debug($"Message {expensiveObject}");
Run Code Online (Sandbox Code Playgroud)

然而,这称为Debug(string)过载.

c# string-interpolation c#-6.0

12
推荐指数
3
解决办法
767
查看次数

带三重引号和多行的字符串插值

"""使用字符串插值和换行符时,我得到三重引号字符串的结果:

val foo = "bar"

s"""$foo"""
Run Code Online (Sandbox Code Playgroud)

还行吧.

s"""
$foo
"""    
Run Code Online (Sandbox Code Playgroud)

这是错误的,我得到以下输出:

"
bar
"
Run Code Online (Sandbox Code Playgroud)

为什么有引号?

scala string-interpolation

11
推荐指数
1
解决办法
9648
查看次数

Angular 2 - 使用异步http请求进行插值和绑定

我是Angular 2的新手,我遇到了异步http请求和插值绑定的问题.

这是我的组件:

@Component({
  selector: 'info',
  template: `<h1>{{model.Name}}</h1>`
})
export class InfoComponent implements OnInit {

    model: any;

    constructor(
        private _service: BackendService
    ) { }

    ngOnInit() {
         if (this.model == null) {
            this._service.observableModel$.subscribe(m => this.model = m);
            this._service.get();
        }     
    }
}
Run Code Online (Sandbox Code Playgroud)

渲染模板时,我收到错误,因为尚未设置"模型".

我用这个非常丑陋的黑客解决了这个问题:

@Component({
    selector: 'info',
    template: `
  <template ngFor #model="$implicit" [ngForOf]="models | async">
  <h1>{{model.Name}}</h1>
  </template>
  `
})
export class NeadInfoComponent implements OnInit {

    models: Observable<any>;

    constructor(
        private _service: BackendService
    ) { }

    ngOnInit() {
         if (this.models == null) {
            this._service.observableModel$.subscribe(m …
Run Code Online (Sandbox Code Playgroud)

binding asynchronous httprequest string-interpolation angular

11
推荐指数
1
解决办法
7061
查看次数

C#字符串插值 - 转义双引号和花括号

伙计们,

我正在从插值字符串构建一个JSON对象,而不是获取转义的工作原理.我必须为API使用双引号.

这不会插入花括号之间的表达式:

@"{{
                        ""name"":""{taskName}"", 
                        ""products"": [    
                                    {""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]}, 
                                    {""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}
                                  ], 
                        ""recurring"":true,
                        ""query"":    {
                                        ""date_from"": ""{dateFromString}"",
                                        ""date_to"": ""{dateToString}"",
                                        ""aoi"": {polygon}
                                    },
                        ""aoi_coverage_percentage"":90
                        }}";
Run Code Online (Sandbox Code Playgroud)

这引发了一堆错误 - 显然,大括号没有被正确转义:

$"{{
                        ""name"":""{taskName}"", 
                        ""products"": [    
                                    {""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]}, 
                                    {""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}
                                  ], 
                        ""recurring"":true,
                        ""query"":    {
                                        ""date_from"": ""{dateFromString}"",
                                        ""date_to"": ""{dateToString}"",
                                        ""aoi"": {polygon}
                                    },
                        ""aoi_coverage_percentage"":90
                        }}";
Run Code Online (Sandbox Code Playgroud)

我应该如何格式化它以保留内部双引号和外括号,同时允许插入单个括号内的值?

c# string-interpolation

11
推荐指数
3
解决办法
1万
查看次数

SwiftUI - 显示双精度四舍五入到小数点后两位

我有一个美元(货币)金额,我试图在 Text 元素中使用字符串插值来显示该金额。目前,它显示如 9.9900000。我希望该值仅显示为 9.99。

struct ContentView: View {
var productPrice: Double = 9.99

    var body: some View {
       Text("\(productPrice)")
    }
}
Run Code Online (Sandbox Code Playgroud)

double string-interpolation swiftui

10
推荐指数
1
解决办法
4931
查看次数