Skip to content

Instantly share code, notes, and snippets.

@maulikmadhavi
Created May 25, 2025 04:40
Show Gist options
  • Select an option

  • Save maulikmadhavi/e951bc0bb292803ad7ba16130735464e to your computer and use it in GitHub Desktop.

Select an option

Save maulikmadhavi/e951bc0bb292803ad7ba16130735464e to your computer and use it in GitHub Desktop.
This script is used to setup configuration from yaml and use as namedtuple
single_cls: false
img_size: 640
rect: false
class_names:
0: person
1: bicycle
2: car
3: motorcycle
4: airplane
5: bus
6: train
7: truck
8: boat
9: traffic light
10: fire hydrant
11: stop sign
12: parking meter
13: bench
14: bird
15: cat
16: dog
17: horse
18: sheep
19: cow
import yaml
from collections import namedtuple
# 1. Read the YAML file
with open("data.yaml", "r") as f:
config_data = yaml.safe_load(f)
print("Config Data:", config_data)
# 2. Create a namedtuple class from the keys
ConfigTuple = namedtuple("ConfigTuple", list(config_data.keys())) # Initialize namedtuple with keys from the YAML file
print("ConfigTuple:", ConfigTuple)
print(dir(ConfigTuple)) # Print the attributes of the namedtuple class
# 3. Instantiate it with values from the YAML
opt = ConfigTuple(**config_data) # set values from the YAML file to the namedtuple
print("Opt:", opt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment