I wanted to make use of the speedtest-cli while also incorporating pandas into the project. My initial goal, which I am posting here, was to take the values of the speed test and incorporate them into a CSV. I've been learning about Pandas and figured it would be a good way to accomplish the project.
import speedtest import pandas as pd import os # Initial Speedtest variables s = speedtest.Speedtest() servers =  # File path variables localpath = '' filepath = '' filename = '' # Functions # def speedtest(): """Makes use of the speed-test-cli python wrapper to get speed test data and returns in a dictionary""" s.get_servers(servers) s.get_best_server() s.download() s.upload() s.results.share() results_dict = s.results.dict() return results_dict def convert_mbps(arg): arg = arg / 1000000 return arg def csv_upload(localpath, filepath, filename): """Attempts to append a csv file and, if none exists, creates one in a specified path""" try: df2 = pd.read_csv(localpath) df2 = df2.append(df) df2.to_csv(os.path.join(filepath, filename), index=False) except OSError: df.to_csv(os.path.join(filepath, filename), index=False) # Speedtest and convert to Data Frame result = speedtest() df = pd.io.json.json_normalize(result) df2 = None # Uploads CSV csv_upload(localpath, filepath, filename)
I've tried as best as I could to document throughout my code, as well as to not statically specify parameters within functions; something which I've been bad for in the past. I've also tried to incorporate in some exception handling, though this is something that I believe needs more work in my code.
I've tested the code within a cron, it does exactly what it seems to be functioning as I would like it to. Future developments will surround analyzing data collected from the CSV; trying to determine trends in bandwidth or latency as a function of time of day. Any feedback would be most appreciated.