Skip to content

Commit

Permalink
Tiny fixes, add comments and docstrings.
Browse files Browse the repository at this point in the history
  • Loading branch information
mishaturnbull committed Jan 17, 2018
1 parent bb991da commit 14635fb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
25 changes: 10 additions & 15 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import time as tm
from pyspeedtest import pretty_speed

from settings import REC_FILE, ANALYZE_FILE, ANALYTICS_REC_FILE, \
STANDARDS_ENABLE, STANDARD_PING, STANDARD_DOWN, \
Expand Down Expand Up @@ -33,15 +34,9 @@


def avg(l):
"""Average of a list."""
return round(sum(l) / float(len(l)), 2)


def cspd(spd):
n = spd / 1000000.0
n = round(n, 3)
return str(n) + " Mbit/s"


# meta
statlines.append(" " * 10 + " INTERNET SPEED REPORT")
statlines.append("McVey 3rd floor north wing common area")
Expand All @@ -60,17 +55,17 @@ def cspd(spd):

# download
statlines.append(" " * 10 + "DOWNLOAD STATISTICS" + " " * 10)
statlines.append("Fastest download: " + cspd(max(records['down'])))
statlines.append("Slowest download: " + cspd(min(records['down'])))
statlines.append("Average download: " + cspd(avg(records['down'])))
statlines.append("Fastest download: " + pretty_speed(max(records['down'])))
statlines.append("Slowest download: " + pretty_speed(min(records['down'])))
statlines.append("Average download: " + pretty_speed(avg(records['down'])))

statlines.append("\n")

# upload
statlines.append(" " * 10 + " UPLOAD STATISTICS " + " " * 10)
statlines.append("Fastest upload: " + cspd(max(records['up'])))
statlines.append("Slowest upload: " + cspd(min(records['up'])))
statlines.append("Average upload: " + cspd(avg(records['up'])))
statlines.append("Fastest upload: " + pretty_speed(max(records['up'])))
statlines.append("Slowest upload: " + pretty_speed(min(records['up'])))
statlines.append("Average upload: " + pretty_speed(avg(records['up'])))

statlines.append("\n")

Expand Down Expand Up @@ -109,7 +104,7 @@ def cspd(spd):
for down in records['down']:
if down < STANDARD_DOWN:
n_down_below_std += 1
statlines.append("Number of download speeds below " + cspd(STANDARD_DOWN) + ": " +
statlines.append("Number of download speeds below " + pretty_speed(STANDARD_DOWN) + ": " +
str(n_down_below_std))
p_down_below_std = round(n_down_below_std / totaltries, 3)
statlines.append("Percentage of download speeds below standard: " +
Expand All @@ -122,7 +117,7 @@ def cspd(spd):
for up in records['up']:
if up < STANDARD_UP:
n_up_below_std += 1
statlines.append("Number of upload speeds below " + cspd(STANDARD_UP) + ": " +
statlines.append("Number of upload speeds below " + pretty_speed(STANDARD_UP) + ": " +
str(n_up_below_std))
p_up_below_std = round(n_up_below_std / totaltries, 3)
statlines.append("Percentage of upload speeds below standard: " +
Expand Down
1 change: 1 addition & 0 deletions csv_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from settings import CSV_INPUT_FILE, CSV_OUTPUT_FILE, CSV_CLEAR_INFILE, REC_FILE

def reformat_date(datefield):
"""Convert pretty date to date separated by columns."""
date = time.strptime(datefield, "%a %b %d %w %Y at %H:%M:%S")
out = time.strftime("%Y,%m,%d,%w,%H,%M,%S,", date)
timeidx = (date.tm_sec + date.tm_min*60 + date.tm_hour*3600)/86400 + \
Expand Down
11 changes: 10 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# -*- coding: utf-8 -*-

"""
Main program for PySpeedMonitor.
Monitors internet speed at set intervals. Can be configured through the
accompanying `settings.py` file.
"""

import datetime
import time
import sys
Expand All @@ -14,7 +21,9 @@
print(" pip install pyspeedtest")
sys.exit(1)

# lazy man's debugging
def dprint(level, msg):
"""Debug printer. Args are [level], [message]."""
if VERBOSITY >= level:
print(msg)

Expand All @@ -39,7 +48,7 @@ def dprint(level, msg):
dprint(1, "It didn't work! Joining error line...")
newline = ", ".join([curtime, LOCATION, e.__repr__()]) + '\n'

dprint(1, "Test completed, result:")
dprint(2, "Test completed, result:")
dprint(1, newline)
with open(REC_FILE, 'a') as record:
record.write(newline)
Expand Down

0 comments on commit 14635fb

Please sign in to comment.