Skip to content

Instantly share code, notes, and snippets.

@hackerdem
Last active April 6, 2016 13:39
Show Gist options
  • Save hackerdem/b62a536394423b6dc3cf to your computer and use it in GitHub Desktop.
Save hackerdem/b62a536394423b6dc3cf to your computer and use it in GitHub Desktop.
A python challenge from Lynda. Air temperature, barometric pressure and wind speed data will be retrieved from a web page and mean and median will be calculated for the given set of data.
import urllib.request
import ssl
import statistics
def calculate_mean(d):
x=statistics.mean(d['Air_temp'])
y=statistics.mean(d['Barometric_press'])
z=statistics.mean(d['Wind_Speed'])
return x,y,z
def calculate_median(d):
x=statistics.median(d['Air_temp'])
y=statistics.median(d['Barometric_press'])
z=statistics.median(d['Wind_Speed'])
return x,y,z
def get_data(target):
#bypassing certificate verification because of certificate problem on data provider website
context=ssl._create_unverified_context()
req=urllib.request.urlopen(target,context=context).read().decode(encoding='utf-8').split('\n')
req.pop(0)
datalist={'Date':[],'Time':[],'Air_temp':[],'Barometric_press':[],'Wind_Speed':[]}
for line in req:
elements=line.split()
datalist['Date'].append(elements[0])
datalist['Time'].append(elements[1])
datalist['Air_temp'].append(float(elements[2]))
datalist['Barometric_press'].append(float(elements[3]))
datalist['Wind_Speed'].append(float(elements[8]))
return datalist
if __name__=='__main__':
target="https://lpo.dt.navy.mil/data/DM/Environmental_Data_Deep_Moor_2015.txt"
data_list=get_data(target)
k=calculate_mean(data_list)
print("Calculated mean values for:\n")
print("Air Temperature:{}, Barometric Pressure:{}, Wind Speed: {}".format(k[0],k[1],k[2]))
r=calculate_median(data_list)
print("Calculated median values for:\n")
print("Air Temperature:{}, Barometric Pressure:{}, Wind Speed: {}".format(r[0],r[1],r[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment