import yaml import munch def readYAML(fileName): # read a YAML file and convert to a munch object with open(fileName,'r') as stream: try: yOut=yaml.safe_load(stream) yB = munch.munchify(yOut) except yaml.YAMLError as exc: return('') return(yB) def dumpYAML(fileName,yB): # convert a munch object back to YAML dictionary and write out yOut = munch.unmunchify(yB) with open(fileName,'w') as f1: yaml.safe_dump(yB,f1) f1.close() f2 = '/home/elliot/Documents/alarm-system/alarmDevices.txt' devices = readYAML(f2) print(devices.GPIO18.type) print(exec('devices.GPIO18.type')) for key in devices: pin = key.split('GPIO')[1] if pin[0]=='0': pin = pin.split('0')[1] codeToExecute = 'b'+ pin+' = Button(' + pin + ',pull_up=True, pin_factory='+fac1+',bounce_time=0.01)' print(codeToExecute) print('devices.'+key+'.type') print(exec('devices.'+key+'.type'))