未识别Python字典键

时间:2015-06-01 10:04:55

标签: python dictionary

我有一个像下面这样的方法,

@staticmethod
def get_repo(tenant_id):
    """

    :param int tenant_id:
    :return: GitRepository object
    :rtype: GitRepository
    """
    AgentGitHandler.log.info(AgentGitHandler.__git_repositories)
    AgentGitHandler.log.info("Tenant ID %s" %tenant_id)
    for key,value in AgentGitHandler.__git_repositories.iteritems():
            if tenant_id == key:
                    AgentGitHandler.log.info("Key Matching")
                    AgentGitHandler.log.info(AgentGitHandler.__git_repositories[key])
            AgentGitHandler.log.info("Key %s : value %s ", key,value)
    if tenant_id in AgentGitHandler.__git_repositories:
        return AgentGitHandler.__git_repositories[tenant_id]
    AgentGitHandler.log.info("False condition")
    return None

我试图调用上面的函数,如下所示,

git_repo = AgentGitHandler.get_repo(tenant_id)
git_repo.scheduled_update_task.terminate()

tenant_id参数值是-1234,它在字典中作为键。 但我得到的答案如下。

2015-06-01 13:44:54,979:INFO:Processing Tenant unsubscribed event: [tenant] -1234 [application ID] single-cartridge-app
2015-06-01 13:44:54,980:INFO:{u'-1234': <modules.artifactmgt.git.agentgithandler.GitRepository instance at 0x1cbeb00>}
2015-06-01 13:44:54,980:INFO:Tenant ID -1234
2015-06-01 13:44:54,980:INFO:Key -1234 : value <modules.artifactmgt.git.agentgithandler.GitRepository instance at 0x1cbeb00>
2015-06-01 13:44:54,980:INFO:False condition
2015-06-01 13:44:54,980:INFO:GIT_REPO None
2015-06-01 13:44:54,980:ERROR:Error processing 'ApplicationSignUpRemovedEvent' event
Traceback (most recent call last):
  File "/mnt/apache-stratos-python-cartridge-agent-4.1.0-SNAPSHOT/modules/subscriber/eventsubscriber.py", line 103, in run
    handler(event_msg)
  File "agent.py", line 294, in on_application_signup_removed
    self.__event_handler.on_application_signup_removed_event(event_obj)
  File "/mnt/apache-stratos-python-cartridge-agent-4.1.0-SNAPSHOT/modules/event/eventhandler.py", line 355, in on_application_signup_removed_event
    AgentGitHandler.remove_repo(application_signup_removal_event.tenantId)
  File "/mnt/apache-stratos-python-cartridge-agent-4.1.0-SNAPSHOT/modules/artifactmgt/git/agentgithandler.py", line 414, in remove_repo
    git_repo.scheduled_update_task.terminate()
AttributeError: 'NoneType' object has no attribute 'scheduled_update_task'

为什么if条件会变错?

1 个答案:

答案 0 :(得分:2)

你在doc字符串中说tenant_id是一个int,它似乎是你日志中的一个,但你的字典键是一个unicode字符串-1234 != u'-1234'