Home > Net >  how to print from multiple lists to an api request
how to print from multiple lists to an api request

Time:09-23

i have 3 lists:

servers = ['DC1','DC2','DC3']
main_process = ['1.exe','2.exe','3.exe']
sub_process = ['s1','s2','s3']

and i have an api request in json that i need to print to a file as many time as the number of elements in the lists - in this case 3 - but with the the element from each list in the same location.

for example:

POST api/alerting/rule
{
  "params":{
      "aggType":"avg",
      "termSize":6,
      "thresholdComparator":">",
      "timeWindowSize":5,
      "timeWindowUnit":"m",
      "groupBy":"top",
      "threshold":[
         1000
      ],
      "index":[
         ".test-index"
      ],
      "timeField":"@timestamp",
      "aggField":"sheet.version",
      "SERVER":"DC1" # <--------- This Parameter
      "MAIN_PROCESS":"1.exe" # <--------- This Parameter
      "SUB_PROCESS":"s1" # <--------- This Parameter
   },
   "consumer":"alerts",
   "rule_type_id":".index-threshold",
   "schedule":{
      "interval":"1m" }
   "notify_when":"onActionGroupChange",
   "name":"my alert"
}

POST api/alerting/rule
{
  "params":{
      "aggType":"avg",
      "termSize":6,
      "thresholdComparator":">",
      "timeWindowSize":5,
      "timeWindowUnit":"m",
      "groupBy":"top",
      "threshold":[
         1000
      ],
      "index":[
         ".test-index"
      ],
      "timeField":"@timestamp",
      "aggField":"sheet.version",
      "SERVER":"DC2" # <--------- This Parameter
      "MAIN_PROCESS":"2.exe" # <--------- This Parameter
      "SUB_PROCESS":"s2" # <--------- This Parameter
   },
   "consumer":"alerts",
   "rule_type_id":".index-threshold",
   "schedule":{
      "interval":"1m" }
   "notify_when":"onActionGroupChange",
   "name":"my alert"
}

POST api/alerting/rule
{
  "params":{
      "aggType":"avg",
      "termSize":6,
      "thresholdComparator":">",
      "timeWindowSize":5,
      "timeWindowUnit":"m",
      "groupBy":"top",
      "threshold":[
         1000
      ],
      "index":[
         ".test-index"
      ],
      "timeField":"@timestamp",
      "aggField":"sheet.version",
      "SERVER":"DC3" # <--------- This Parameter
      "MAIN_PROCESS":"3.exe" # <--------- This Parameter
      "SUB_PROCESS":"s3" # <--------- This Parameter
   },
   "consumer":"alerts",
   "rule_type_id":".index-threshold",
   "schedule":{
      "interval":"1m" }
   "notify_when":"onActionGroupChange",
   "name":"my alert"
}

i know that i need some loops for that but i can't figure this out yet. i would prefer to do that in bash because the environment i'm working here but python would be a great solution as well.

thanks.

CodePudding user response:

You could do one for loop and then access the values using the same index in all three lists.

servers = ['DC1','DC2','DC3']
main_process = ['1.exe','2.exe','3.exe']
sub_process = ['s1','s2','s3']

if not (len(servers) == len(main_process) == len(sub_process)):
    raise Exception("The lists have not the same length")

for i in range(len(servers)):
    data = {
        "params":{
            ...,
            "SERVER": servers[i]
            "MAIN_PROCESS": main_process[i]
            "SUB_PROCESS": sub_process[i]
        },
        ...
    }

If you don't want to validate the length of lists, you can also loop up to the minimum:

for i in range(min(len(servers), len(main_process), len(sub_process)))

CodePudding user response:

Probably it would be easier to merge those 3 lists into a list of dictionaries, if the lists all have the same length:

data = [{'server': servers, 'main_process': main_process, 'sub_process': sub_process} for servers, main_process, sub_process in zip(servers, main_process, sub_process)]

Result:

[{'server': 'DC1', 'main_process': '1.exe', 'sub_process': 's1'}, {'server': 'DC2', 'main_process': '2.exe', 'sub_process': 's2'}, {'server': 'DC3', 'main_process': '3.exe', 'sub_process': 's3'}]

Then you can loop on the elements like this:

for item in data:
    print(item.get("server"), item.get("main_process"), item.get("sub_process"))

Not the recommended way, but you could also loop on the 3 lists in lockstep using indexes:

for i, server in enumerate(servers):
    print(server, main_process[i], sub_process[i])

Result:

DC1 1.exe s1
DC2 2.exe s2
DC3 3.exe s3

But using dictionary keys makes the code more explicit.

  • Related