如何让Python函数返回静态变量?

时间:2017-09-18 01:30:59

标签: python python-3.x flask wtforms

我正在尝试构建WTForms custom validator来检查重复的图像哈希值。为此,我在表单验证器中使用嵌套函数。问题是当我在外部函数中设置一个变量(内部函数的结果)时,对外部变量的任何后续引用再次调用内部函数。

例如:

def dupe_check(self, field):
    def get_hash():
        f = field.data
        img = Image.open(f)
        imghash = imagehash.dhash(img)
        f.seek(0)
        return imghash
    imghash = get_hash()
    hashcheck = Sights.query.filter(Sights.image_hash == imghash).first()
    if hashcheck == imghash:
        print('dupe')
        raise ValidationError('duplicate hash!')
    else:
        print('no dupe') 

如果在数据库中没有哈希运行,我打印no dupe并上传图像。

但是,如果数据库中存在哈希,当执行到达if hashcheck == imghash:时,我崩溃了,因为imghash似乎再次调用get_hash()而不是简单地返回先前的哈希字符串使用imghash = get_hash()设置。

您可以在下面看到imagehash程序被调用(和崩溃),而不仅仅是哈希字符串。

127.0.0.1 - - [17/Sep/2017 17:49:36] "POST /add HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/mrrg/dev/flaskimg/project/sights/views.py", line 74, in add_image
    if form.validate_on_submit():
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/flask_wtf/form.py", line 101, in validate_on_submit
    return self.is_submitted() and self.validate()
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/wtforms/form.py", line 310, in validate
    return super(Form, self).validate(extra)
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/wtforms/form.py", line 152, in validate
    if not field.validate(self, extra):
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/wtforms/fields/core.py", line 204, in validate
    stop_validation = self._run_validation_chain(form, chain)
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/wtforms/fields/core.py", line 224, in _run_validation_chain
    validator(form, self)
  File "/home/mrrg/dev/flaskimg/project/sights/forms.py", line 71, in dupe_check
    if hashcheck == imghash:
  File "/home/mrrg/dev/flaskimg/venv/lib/python3.5/site-packages/imagehash/__init__.py", line 85, in __eq__
    return numpy.array_equal(self.hash.flatten(), other.hash.flatten())
AttributeError: 'Sights' object has no attribute 'hash'

我以为imghash = get_hash()会设置从内部函数返回的变量的新实例?如何设置静态变量来操作,并避免对内部函数的其他调用?

我也尝试在内部函数imghash上设置global / nonlocal,结果相同。

1 个答案:

答案 0 :(得分:0)

据我所知,您的代码尝试将Sight对象与ImageHash对象进行比较,但Sight类代码实现仅比较2 Sight对象,因此出现错误