I'm learning python and I'm trying to use a dictionary (or any other way) to setup a "list dictionary" that I can iterate with a for loop.
Here's and example of the data structure that I need to loop through:
name: alert1
id: 12345
name: alert2
id: 54321
I've got it working with the code below, but I'm not sure I'm doing the "recommended" way, or if there are any big flaws with the code:
# Gets arguments
option = (sys.argv[1])
def disable_alert(alert_name,alert_id):
disable_alert = {'enabled':False,'dampeningCategory':'ONCE'}
print "Disabling alert %s (ID: %s)" % (alert_name,alert_id)
req = requests.put(endpoint+'alert/definition/%s' % (alert_id),json.dumps(disable_alert),headers=headers,auth=auth)
check_status = req.json()['enabled']
if check_status == False:
print "Alert %s disabled\n" % alert_name
else:
print "Alert %s did not disable\n" % alert_name
alerts = {'name':['ils.txdatasource.dbpool','SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE','SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE'],'id':['10435','10423','10421']}
if option == "disable":
count = 0
for nothing in alerts['name']:
disable_alert(alerts['name'][count],alerts['id'][count])
count = count - 1
else:
print "I don't know that option"
Here's an example output of the working code:
$ python jon_alerts.py disable
Disabling alert ils.txdatasource.dbpool (ID: 10435)
Alert ils.txdatasource.dbpool disabled
Disabling alert SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE (ID: 10423)
Alert SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE disabled
Disabling alert SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE (ID: 10421)
Alert SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE disabled