Home > Software engineering >  Check value of yaml child -Python
Check value of yaml child -Python

Time:11-19

Hey guys I have two yaml files.

One yaml with jobs and ther information as childs.

other yaml (yaml2) with requirements:

and I want to check if the value of the Child "stage" is the same as in the requirements

in my code the father node must be generic so it can be different jobs. But it can have the child stage or not. stage can be AV, BV, CV

and in my other yaml2 with requirements I have also stage ( which can also be AV, BV or CV)

Now I want to run a job if the conditions are met for the requirements yaml2.

How can I check it? Cause my key can always be something else in the job yaml.

job yaml:

jobx:
  based: false
  stage: AV
joby:
  based: true
jobxyz:
  based: false
  composition: basis
  stage: BV

yaml2: (requirements)

requirements:
  based: false
  stage: AV
  composition: basis
  filled: true

CodePudding user response:

To avoid using defined indexes you could use a for cycle to check elements. Like for examples:

with open(r'job.yml') as file:
    jobs = yaml.load(file, Loader=yaml.FullLoader)
    with open(r'requirements.yml') as file:
        requirements = yaml.load(file2, Loader=yaml.FullLoader)
           for job in jobs:
               if(job["stage"] == requirements["stage"]):
                     #do job

I suggest you also check if variable is defined if it's not always set in your file

  • Related