Python dictionary key is not identified

I have a method like following,

@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

I am trying to get call the above function like following,

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

tenant_id parameter value is -1234 which is in the dictionary as a key. But I am getting a response like below.

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'

Why is the if condition getting false ?

Upvotes: 0

Views: 88

Answers (1)

kylieCatt
kylieCatt

Reputation: 11039

You say in the doc string that tenant_id is an int, and it appears to be one in your log, but your dictionary key is a unicode string, -1234 != u'-1234'.

Upvotes: 2

Related Questions