gpt4 book ai didi

python - 名称错误 : global name 'has_no_changeset' is not defined

转载 作者:太空宇宙 更新时间:2023-11-04 11:05:51 25 4
gpt4 key购买 nike

好的 - 这里是 Python 新手 - 我想我正在做一些非常愚蠢的事情,你能告诉我它是什么,这样我们才能继续我们的生活吗?

我在第 55 行(我尝试调用函数 has_no_changeset)中收到错误 NameError: global name 'has_no_changeset' is not defined

from genshi.builder import tag

from trac.core import implements,Component
from trac.ticket.api import ITicketManipulator
from trac.ticket.default_workflow import ConfigurableTicketWorkflow
from trac.perm import IPermissionRequestor
from trac.config import Option, ListOption
import re

revision = "$Rev$"
url = "$URL$"

class CloseActionController(Component):
"""Support for close checking.

If a ticket is closed, it is NOT allowed if ALL the following conditions apply:
a) ticket is 'bug' ticket
b) resolution status is 'fixed'
c) none of the ticket's changes include a comment containing a changeset, i.e. regex "\[\d+\]"
d) the ticket does not have the keyword 'web'
"""

implements(ITicketManipulator)

# ITicketManipulator methods

def prepare_ticket(req, ticket, fields, actions):
"""Not currently called, but should be provided for future
compatibility."""
return


def has_no_changeset(ticket):
db = self.env.get_db_cnx()
cursor = db.cursor()

cursor.execute("SELECT newvalue FROM ticket_change WHERE ticket=%s AND field='comment'", (str(ticket.id).encode('ascii','replace'),))

for newvalue, in cursor:
if re.search("\[\d{5,}\]", newvalue):
return False

return True

def validate_ticket(me, req, ticket):
"""Validate a ticket after it's been populated from user input.

Must return a list of `(field, message)` tuples, one for each problem
detected. `field` can be `None` to indicate an overall problem with the

ticket. Therefore, a return value of `[]` means everything is OK."""

if ticket['type'] == 'bug' and ticket['resolution'] == 'fixed':
if ticket['keywords'] == None or ticket['keywords'].find('web') == -1:
if has_no_changeset(ticket):
return [(None, 'You can only close a bug ticket as "fixed" if you refer to a changeset somewhere within the ticket, e.g. with [12345]!')]

return[]

最佳答案

在引用当前类的方法时,您需要明确指定 self(或者在您的情况下,me):

if me.has_no_changeset(ticket):

您使用的是 me 而不是 self - 这是合法的,但强烈建议不要这样做。成员函数的第一个参数应该叫做self:

def validate_ticket(self, req, ticket):
# [...]
if self.has_no_changeset(ticket):

关于python - 名称错误 : global name 'has_no_changeset' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1017467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com