Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
setconfig setting
#1
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.




Error:
Traceback (most recent call last):   File "C:/Users/Downloads/Yaml-parser.py", line 48, in <module>     testConfig.setConfigSetting("version", 2) TypeError: setConfigSetting() missing 1 required keyword-only argument: 'setting'
Can someone please help me with the error above in line 48.

Thank you.
Reply
#2
(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 = setting
tried executing this code but an error is popping out.

Error:
Traceback (most recent call last):   File "C:/Users/Downloads/Yaml-parser.py", line 48, in <module>     testConfig.setConfigSetting("version", 2) TypeError: setConfigSetting() missing 1 required keyword-only argument: 'setting'
Can someone please help me with the error above in line 48.

Thank you.

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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
Can you please write in the code and give it to me
Reply
#4
(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?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
yeaaah brah it's working
Reply
#6
You could just delete the method, since you never call it anyway.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020