[SOLVED] Adding "for" loop results inside a "logger" message

Issue

I have the following function logic.

def funct(arg):
    ...
    return result_list

param_list = ['param1','param2']
for my_arg in param_list
    my_results = funct(my_arg)

How do I get the full list of results for running the function on both parameters? Is there a better way to organize this?

Following one of the answers I tried to apply that logic buy I’m not getting the desired results:

from pythonping import ping
from log_class import NetddLog

def ip_probe(ip, ip_probe_logger, ipsl):

    def ping_ip():
        try: 
            ip_probe_logger.info(f"Pinging {ip} started")
            result = ping(ip, count=5, df=True, payload='64')
            ip_probe_logger.info(f"Pinging {ip} succeded")
        except Exception as err:
            ip_probe_logger.error(f"Pinging {ip} failed {err}")

    def trace_ip():
        #function for traceroute
        pass

    ping_ip()
    #trace_ip()
    
    log_contents = ip_probe_logger.log_capture_string.getvalue()
    ipsl.append(log_contents)

    return ipsl

if __name__ == "__main__":
    ipsl = ['Start']
    ip_probe_logger = NetddLog("IP_PROBE", level="INFO") 
    ip_list = ["192.168.255.68", "192.168.254.108"]
    for ip in ip_list:
        ipsl.extend(ip_probe(ip, ip_probe_logger, ipsl))
    print('')
    for log_item in ipsl:
        print(log_item.rstrip())

I get a result with lots of repetition, but what I am trying to achieve is adding each individual ping result to IPSL list after the first ‘Start’ item.

Start
2022-03-24 01:12:48,444|IP_PROBE|INFO: Pinging 192.168.255.68 started
2022-03-24 01:12:48,770|IP_PROBE|INFO: Pinging 192.168.255.68 succeded
Start
2022-03-24 01:12:48,444|IP_PROBE|INFO: Pinging 192.168.255.68 started
2022-03-24 01:12:48,770|IP_PROBE|INFO: Pinging 192.168.255.68 succeded
2022-03-24 01:12:48,444|IP_PROBE|INFO: Pinging 192.168.255.68 started
2022-03-24 01:12:48,770|IP_PROBE|INFO: Pinging 192.168.255.68 succeded
2022-03-24 01:12:48,772|IP_PROBE|INFO: Pinging 192.168.254.108 started
2022-03-24 01:12:49,078|IP_PROBE|INFO: Pinging 192.168.254.108 succeded
Start
2022-03-24 01:12:48,444|IP_PROBE|INFO: Pinging 192.168.255.68 started
2022-03-24 01:12:48,770|IP_PROBE|INFO: Pinging 192.168.255.68 succeded
Start
2022-03-24 01:12:48,444|IP_PROBE|INFO: Pinging 192.168.255.68 started
2022-03-24 01:12:48,770|IP_PROBE|INFO: Pinging 192.168.255.68 succeded
2022-03-24 01:12:48,444|IP_PROBE|INFO: Pinging 192.168.255.68 started
2022-03-24 01:12:48,770|IP_PROBE|INFO: Pinging 192.168.255.68 succeded
2022-03-24 01:12:48,772|IP_PROBE|INFO: Pinging 192.168.254.108 started
2022-03-24 01:12:49,078|IP_PROBE|INFO: Pinging 192.168.254.108 succeded

By changing the body of for loop to: ip_probe(ip, ip_probe_logger, ipsl)
I still get a repetition probably because of the second function return.

Start
2022-03-24 01:22:32,829|IP_PROBE|INFO: Pinging 192.168.255.68 started
2022-03-24 01:22:33,235|IP_PROBE|INFO: Pinging 192.168.255.68 succeded
2022-03-24 01:22:32,829|IP_PROBE|INFO: Pinging 192.168.255.68 started
2022-03-24 01:22:33,235|IP_PROBE|INFO: Pinging 192.168.255.68 succeded
2022-03-24 01:22:33,236|IP_PROBE|INFO: Pinging 192.168.254.108 started
2022-03-24 01:22:33,504|IP_PROBE|INFO: Pinging 192.168.254.108 succeded

Solution

Make my_results a list, and extend it on each call.

my_results = []
for my_arg in param_list
    my_results.extend(funct(my_arg))

Answered By – Barmar

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.