netprobe_lite
netprobe_lite copied to clipboard
Fix: Division by 0 issue in presentation
Suggested consideration for the submitted code snippet to avoid Division by 0 issue in presentation error message. #51
Division by zero error:
- When calculating the averages (
average_latency,average_loss, andaverage_jitter), you're dividing bylen(stats_netprobe['stats']). Ifstats_netprobe['stats']is empty, this will raise a ZeroDivisionError.
Type consistency:
- You are converting
item['latency'],item['loss'], anditem['jitter']to float before adding them to the totals. Ensure that these values are always present in item and are numeric (either a float or a string that can be converted to a float). If not, this might raise a ValueError.
Redundant looping:
- You are iterating over
stats_netprobe['stats']twice. You can combine the loops to calculate the totals and add the individual metrics in the same iteration, improving efficiency.
Handling missing keys:
- If any of the keys (
'latency','loss','jitter', or'site') are missing from item, your code will raise a KeyError. You can either check the presence of the keys or use.get()with default values.