小编Nwa*_*ume的帖子

Django:将变量从pre_save传递到post_save信号

我使用pre_save和post_save信号将分析发送到Mixpanel.我更喜欢将它与我的模型的保存方法分开.

有没有办法在pre_save信号出现时保存实例的旧值,然后在post_save上检查新值?

我的代码看起来像这样:

@receiver(pre_save, sender=Activity)
def send_user_profile_analytics(sender, **kwargs):
    activity_completed_old_value = kwargs['instance'].is_completed
    # store this value somewhere?

@receiver(post_save, sender=Activity)
def send_user_profile_analytics(sender, **kwargs):
    if kwargs['instance'].is_completed != activity_completed_old_value:
        # send analytics
Run Code Online (Sandbox Code Playgroud)

对我来说,使用post_save发送分析而不是pre_save似乎更健壮,但在那时我无法看到模型实例中发生了什么变化.我想在我的模型的保存功能中阻止使用全局变量或实现某些东西.

django django-signals

6
推荐指数
1
解决办法
2671
查看次数

Python中使用Tesseract OCR的UnicodeDecodeError

我试图使用Python中的Tesseract OCR从图像文件中提取文本,但我面临一个错误,我可以弄清楚如何处理它.所有我的环境都很好,因为我在python中使用ocr测试了一些示例图像!

这是代码

from PIL import Image
import pytesseract
strs = pytesseract.image_to_string(Image.open('binarized_image.png'))

print (strs)
Run Code Online (Sandbox Code Playgroud)

以下是我从eclipse控制台获得的错误

strs = pytesseract.image_to_string(Image.open('binarized_body.png'))
  File "C:\Python35x64\lib\site-packages\pytesseract\pytesseract.py", line 167, in image_to_string
    return f.read().strip()
  File "C:\Python35x64\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 20: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)

我在Windows10上 使用python 3.5 x64

python tesseract python-tesseract

6
推荐指数
1
解决办法
5454
查看次数

在JAVA中遍历JSON数据

我是JSON新手..Am使用HTTPUrlConnections并在JAVA程序中获得了一些响应。响应数据将像

    {
    "data": [
        {
    "id": 1,
            "userId": 1,
            "name": "ABC",
            "modified": "2014-12-04",
            "created": "2014-12-04",
            "items": [
                {
                    "email": "abc@gmail.com",
                    "links": [
                        {
                            .
                            .
                            .
                            .
                        }
                    ]
                }
            ]
            }
        ]
}
Run Code Online (Sandbox Code Playgroud)

从此响应中,可以使用以下Java代码获取“ name ”字段的值。

JSONArray items = newObj.getJSONArray("data");
for (int it=0 ; it < items.length() ; it++){
    JSONObject contactItem = items.getJSONObject(it);
    String userName = contactItem.getString("name");
    System.out.println("Name----------"+userName);
}
Run Code Online (Sandbox Code Playgroud)

但是我的要求是,我需要获取“ 电子邮件 ” 的值..我应该如何为此编码..任何建议..

在此先感谢.. Chitra

java json

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