I’m working on a script where I passed a dict in a class constructor.
At starting all my classes was in the same file than the main script.
My main function call the constructor passing a dict. When I check if a key is in value, all is ok:
if "HOSTALIAS" in myDict:
self.__alias=myDict["HOSTALIAS"]
In order to do things well, I want separate classes from main script in a separate file.
So I imported my class like this:
from modules.incident import Incident
All works except the check if key exists. By doing like this, my if is bypassed as it does not contain my key, but in my constructor, I looped from the keys and I see the searched key.
For the details, I work for dev a checkmk plugin.
Main script sample:
from modules.incident import Incident
dict=utils.context #return a dict
def main:
inc=Incident(ctx)
incident.py
class file:
class Incident :
def __init__ (self, context) :
if "HOSTALIAS" in context:
#NEVER PASSED HERE
self.__alias = context["HOSTALIAS"]
print(context["HOSTALIAS"]) #Return the value expected
What I don’t understand is that if I put all this stuff in the same file without import and do not modify the code, it works.
In separate files, I meet this trouble.