小编sim*_*mon的帖子

字符串连接真的那么慢吗?

我目前正在研究String concat选项以及它们对整体性能的惩罚.我的测试用例创造了令我心烦意乱的结果,我不确定我是否会忽略某些东西.

这是交易:"something"+"somethingElse"在java中执行(在编译时)StringBuilder每次完成时都会创建一个新的.

对于我的测试用例,我正在从我的HDD加载一个包含1661行示例数据的文件(经典的"Lorem Ipsum").这个问题不是关于I/O性能,而是关于不同字符串concat方法的性能.

public class InefficientStringConcat {

    public static void main(String[] agrs) throws Exception{
        // Get a file with example data:

        System.out.println("Starting benchmark");
        // Read an measure:
        for (int i = 0; i < 10; i++){
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(new FileInputStream(new File("data.txt")))
            );

            long start = System.currentTimeMillis();
            // Un-comment method to test:
            //inefficientRead(in);
            //betterRead(in);
            long end = System.currentTimeMillis();
            System.out.println("Took "+(end-start)+"ms");

            in.close();
        }



    }

    public static String betterRead(BufferedReader …
Run Code Online (Sandbox Code Playgroud)

java string-concatenation performance-testing

19
推荐指数
2
解决办法
3726
查看次数

Django-templates:为什么{%if"string"| length> 10%}不起作用?

我正在使用Django 1.3.如果我将以下片段放入我的模板中:

{% if 'my string'|length > 10 %}{{ 'my string'|length }}{% endif %}
Run Code Online (Sandbox Code Playgroud)

渲染引擎打印'9'.我唯一能想到的是|length过滤器返回一个字符串,但这在极端情况下似乎很奇怪.谁能指出我正确的方向?

谢谢!

编辑:

我实际想要测试的长度来自flatpage.title提供django.contrib.flatpages.出于这个原因,我宁愿不破解视图来提供模板所需的信息.我希望我可以简单地使用Django文档中|length描述的过滤器.但是,正如已经指出的那样,这样做的唯一方法似乎也是使用过滤器,其行为在这方面没有明确定义.:(|get_digit

django django-templates

15
推荐指数
3
解决办法
2万
查看次数

Django自定义字段具有多重继承

我有两个自定义Django字段,a JSONField和a CompressedField,两者都运行良好.我想也有CompressedJSONField,我希望我能做到这一点:

class CompressedJSONField(JSONField, CompressedField):
    pass
Run Code Online (Sandbox Code Playgroud)

但在导入时,我得到:

RuntimeError: maximum recursion depth exceeded while calling a Python object
Run Code Online (Sandbox Code Playgroud)

我可以在Django中找到有关使用具有多重继承的模型的信息,但没有关于对字段执行相同操作的信息.这有可能吗?或者我应该在这个阶段放弃?

编辑:

为了清楚起见,我不认为这与我的代码的细节有任何关系,因为下面的代码有完全相同的问题:

class CustomField(models.TextField, models.CharField):
    pass
Run Code Online (Sandbox Code Playgroud)

编辑2:

我目前正在使用Python 2.6.6和Django 1.3.这是我的剥离右下测试示例的完整代码:

customfields.py

from django.db import models


class CompressedField(models.TextField):
    """ Standard TextField with automatic compression/decompression. """

    __metaclass__ = models.SubfieldBase
    description = 'Field which compresses stored data.'

    def to_python(self, value):
        return value

    def get_db_prep_value(self, value, **kwargs):
        return super(CompressedField, self)\
                        .get_db_prep_value(value, prepared=True)


class JSONField(models.TextField):
    """ JSONField with automatic serialization/deserialization. …
Run Code Online (Sandbox Code Playgroud)

python django multiple-inheritance custom-fields

5
推荐指数
1
解决办法
1315
查看次数

`pyparsing`:迭代`ParsedResults`

我刚开始使用pyparsing这个晚上,我已经构建了一个复杂的语法,它描述了我正在非常有效地工作的一些来源.它非常简单而且非常强大.但是,我在使用时遇到了一些麻烦ParsedResults.我需要能够按照它们被找到的顺序迭代嵌套的标记,并且我发现它有点令人沮丧.我已将问题抽象为一个简单的案例:

import pyparsing as pp

word = pp.Word(pp.alphas + ',.')('word*')
direct_speech = pp.Suppress('“') + pp.Group(pp.OneOrMore(word))('direct_speech*') + pp.Suppress('”')
sentence = pp.Group(pp.OneOrMore(word | direct_speech))('sentence')

test_string = 'Lorem ipsum “dolor sit” amet, consectetur.'

r = sentence.parseString(test_string)

print r.asXML('div')

print ''

for name, item in r.sentence.items():
    print name, item

print ''

for item in r.sentence:
    print item.getName(), item.asList()
Run Code Online (Sandbox Code Playgroud)

据我所见,这应该有用吗?这是输出:

<div>
  <sentence>
    <word>Lorem</word>
    <word>ipsum</word>
    <direct_speech>
      <word>dolor</word>
      <word>sit</word>
    </direct_speech>
    <word>amet,</word>
    <word>consectetur.</word>
  </sentence>
</div>

word ['Lorem', 'ipsum', 'amet,', 'consectetur.']
direct_speech [['dolor', 'sit']]

Traceback …
Run Code Online (Sandbox Code Playgroud)

python pyparsing

5
推荐指数
2
解决办法
486
查看次数

将重复(隐藏)文本图层添加到pdf以进行额外搜索

我的问题:

我有一个带有许多带有复杂变音符号的罗马字符的pdf(例如,ṣ,ś,ṝ,ǎ等).为了更容易在pdf中搜索,我想添加一个额外的层,就像使用hocr一样,其中相同的文本存在而没有变音符号.

当使用全文搜索引擎时,我可以在同一位置(向量)索引多个术语 - 我想在这里达到相同的效果.

我已经阅读了很多关于向扫描图像添加特定图层的内容,但我真的只想复制文本图层,将其传递给删除变音符号的脚本(直截了当),然后将其作为隐藏但可搜索的图层重新添加.

有人有什么建议吗?(涉及任何平台,语言,库或工具链的解决方案将非常有用!)

谢谢 :)

编辑:如果问题不清楚,请告诉我.

pdf search

4
推荐指数
1
解决办法
3153
查看次数

Gtk.TreeView 中的 Gtk.Entry (CellRenderer)

我想将一个Gtk.Entry(已Gtk.EntryCompletion连接)打包到Gtk.TreeView. 有谁知道如何做到这一点?(我只需要对表格视图中的文本条目进行输入完成。)

Do I perhaps need to subclass Gtk.CellRenderer or Gtk.CellRendererText, and override the start_editing method (or similar)? I can find examples of subclassing Gtk.CellRenderer, but not modifying the editable behaviour. I can't find the source-code for the Gtk.CellRendererText class, either.

I'm using Gobject Introspection (i.e. from gi.repository import Gio, Gtk, GLib, Gdk).

python gtk gobject pygobject gobject-introspection

2
推荐指数
1
解决办法
1830
查看次数

使用atexit进行织物清理操作

是否有关于如何在结构任务中清理(例如删除临时文件等)的智慧?如果我像平常一样使用atexit模块,那么我有困难,因为我不能使用@roles装饰器来装饰传递给它的函数atexit.register().或者我可以吗?其他面料用户如何应对此问题?

python atexit fabric

2
推荐指数
1
解决办法
483
查看次数