![]() |
setconfig setting - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: setconfig setting (/thread-3415.html) |
setconfig setting - pydeveloper - May-22-2017 import yaml import sys import traceback class YAMLConfigReader: def __init__(self, configFile): if configFile.strip() == "": self.configFile = "node-config.yaml" else: self.configFile = configFile with open(self.configFile, 'r') as stream: try: self.configSettings = yaml.load(stream) print(self.configSettings) except yaml.YAMLError as exc: print(exc) self.printException(sys.exc_info()) except: print("Unknown Exception caught") self.printException(sys.exc_info()) def getConfigSetting(self, *args): currentDict = self.configSettings for s in args: currentDict = currentDict[s] print(currentDict) return currentDict def setConfigSetting(self, *args, setting): currentDict = self.configSettings for s in args: currentDict = currentDict[s] print(currentDict) currentDict = setting def printException(self, exceptionInfo): print("ExceptionType:", exceptionInfo[0]) print("ExceptionValue:", exceptionInfo[1]) print("TraceBack:", traceback.print_tb(exceptionInfo[2])) testConfig = YAMLConfigReader("node-config.yaml") testConfig.getConfigSetting("version") testConfig.getConfigSetting("hostInfo", "nameShort") testConfig.getConfigSetting("oamGroup","nodes") testConfig.getConfigSetting("oamGroup", "dn", "node") testConfig.setConfigSetting("version", 2) testConfig.getConfigSetting("version")tried executing this code but an error is popping out. Can someone please help me with the error above in line 48.Thank you. RE: setconfig setting - Ofnuts - May-22-2017 (May-22-2017, 04:20 AM)pydeveloper Wrote:def setConfigSetting(self, *args, setting): currentDict = self.configSettings for s in args: currentDict = currentDict[s] print(currentDict) currentDict = settingtried executing this code but an error is popping out. In your functon declaration, the *args catches all positional parameters, so "setting" must be an option keyword parameter.I don't see much benefint in being able to set several settings to the same value so you can just as well use simpler setting_name, setting_value args.
RE: setconfig setting - pydeveloper - May-22-2017 Can you please write in the code and give it to me RE: setconfig setting - Ofnuts - May-22-2017 (May-22-2017, 08:32 PM)pydeveloper Wrote: Can you please write in the code and give it to me No, because I don't want to influence you one way or another. Either you are a seasoned python programmer and you know what you are doing when you use constructs such as *args , or the consequences are a bit above your head right now and you write code that you completely understand. No shame doing so. And IMHO there is no need for such constructs here (in both the get..() and the set...()).PS: is your getConfigSettting() method really working when you give several arguments? RE: setconfig setting - pydeveloper - May-22-2017 yeaaah brah it's working RE: setconfig setting - nilamo - May-22-2017 You could just delete the method, since you never call it anyway. |