Python how to parse yaml file11/27/2022 ![]() To add all the content from value to arg_dict, you shouldn't use àppend but extend. The name execute probably needs to be improved for something more meaningful. G = parser.add_argument_group('Device Targets') Removing the class, you get something like : def execute(): Then you can keep an isolated version that you maintain your self. If you do write a class, I find it clearer to have an init to define the different members but in your case, you could stop writing classes. I suggest you fork an open licensed module like the Phil-Factor/PSYaml as this isn't a trivial task. In your current version of the code, it is hard to see what is stored in an instance and how the data flows around the different methods. I get this for the output: Namespace(age=, delay=30, name=, stupid=False) If my config-file has the following data: name: Īnd I run my code like this: python test.py -conf args.txt -name 'Mark' G.add_argument('-stupid', dest='stupid', default=False, action='store_true') G.add_argument('-age', default=, action='append') G.add_argument('-name', default=, action='append') Below is an example of how I envision using the approach for a much larger project: import yaml class Reactor: def. The class attributers are then used to obtain the values associated with the parameters. I am trying to parse the command line arguments using argparse, and if the user specifies a yaml file for the config-file, add those arguments to the args from argparse import argparse To allow easier access to the nested parameters, I use an intermediate Python class to parse the dictionary values into class attributes. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |