Skip to content

Commit

Permalink
MNT: Modernize setup.py, beef up test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
phobson committed Jan 27, 2015
1 parent dd0b507 commit f6f27b9
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 85 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -20,7 +20,10 @@ install:
- cp test/matplotlibrc .

script:
- nosetests --verbose --with-id --with-coverage --cover-package=metar
- cd ~
- source activate testenv
- python -c "import metar; metar.test()"
#- nosetests --verbose --with-id --with-coverage --cover-package=metar

after_success:
- coveralls
23 changes: 0 additions & 23 deletions MANIFEST

This file was deleted.

49 changes: 33 additions & 16 deletions metar/station.py
Expand Up @@ -6,11 +6,8 @@

# std lib stuff
import datetime
from six.moves.urllib import request
from six.moves.urllib import error
from six.moves.urllib import parse
from six.moves import http_cookiejar
import os
import sys
import pdb
import codecs

Expand All @@ -20,6 +17,12 @@
import matplotlib.dates as mdates
import pandas

#compat
from six.moves.urllib import request
from six.moves.urllib import error
from six.moves.urllib import parse
from six.moves import http_cookiejar

# metar stuff
from . import metar
from . import datatypes
Expand Down Expand Up @@ -49,13 +52,14 @@ class WeatherStation(object):
"""

def __init__(self, sta_id, city=None, state=None, country=None,
lat=None, lon=None, max_attempts=10):
lat=None, lon=None, max_attempts=10, show_progress=False):
self.sta_id = sta_id
self.city = city
self.state = state
self.country = country
self.position = datatypes.position(lat, lon)
self.max_attempts = max_attempts
self._max_attempts = max_attempts
self._show_progress = show_progress

if self.state:
self.name = "%s, %s" % (self.city, self.state)
Expand All @@ -69,6 +73,19 @@ def __init__(self, sta_id, city=None, state=None, country=None,
self._wunder_nonairport = None
self._asos = None

@property
def show_progress(self):
return self._show_progress
@show_progress.setter
def show_progress(self, value):
self._show_progress = value

@property
def max_attempts(self):
return self._max_attempts
@max_attempts.setter
def max_attempts(self, value):
self._max_attempts = value

@property
def wunderground(self):
Expand Down Expand Up @@ -430,7 +447,9 @@ def _get_data(self, startdate, enddate, source, filename):
labelfxn = lambda ts, status: '{} {}: {}'.format(
self.sta_id, ts.strftime('%Y.%m.%d'), status
)
progress = metar.ProgressBar(timestamps, labelfxn=labelfxn)

if self.show_progress:
progress = metar.ProgressBar(timestamps, labelfxn=labelfxn)

data = None
for n, ts in enumerate(timestamps):
Expand All @@ -440,7 +459,8 @@ def _get_data(self, startdate, enddate, source, filename):
newdata, status = self._read_csv(ts, source)
data = data.append(newdata)

progress.animate(n+1, status)
if self.show_progress:
progress.animate(n+1, status)

# add a row number to each row
data['rownum'] = list(range(data.shape[0]))
Expand Down Expand Up @@ -715,16 +735,13 @@ def _process_sky_cover(obs):


def getAllStations():
station_file_name = "reference/nsd_cccc.txt"
#station_file_url = "http://www.noaa.gov/nsd_cccc.txt"
stationfile = os.path.join(sys.prefix, 'metar_data', 'reference', 'nsd_cccc.txt')
stations = {}

fh = open(station_file_name, 'r')
for line in fh:
f = line.strip().split(";")
stations[f[0]] = (f[0], f[3], f[4], f[5], f[7], f[8])

fh.close()
with open(stationfile, 'r') as fh:
for line in fh:
f = line.strip().split(";")
stations[f[0]] = (f[0], f[3], f[4], f[5], f[7], f[8])

return stations

Expand Down
33 changes: 21 additions & 12 deletions metar/tests/exporters_tests.py
@@ -1,21 +1,30 @@
import os
import sys

import nose.tools as ntools
from metar import station
from metar import exporters
import datetime as dt
import pandas
import matplotlib
import matplotlib.pyplot as plt
from six import StringIO

from metar import station
from metar import exporters


@ntools.nottest
def getTestFile(filename):
return os.path.join(sys.prefix, 'metar_data', 'test_data', filename)

class test_exporter(object):
def setup(self):
self.fivemin = pandas.read_csv('test/data_for_tests.csv',
self.fivemin = pandas.read_csv(getTestFile('data_for_tests.csv'),
parse_dates=True, index_col=0)
self.hourly = self.fivemin.resample('1H', how='sum')

self.known_fivemin_swmm5_file = 'test/known_fivemin_swmm5.dat'
self.known_hourly_swmm5_file = 'test/known_hourly_swmm5.dat'
self.knwon_hourly_ncdc_file = 'test/known_hourly_NCDC.dat'
self.known_fivemin_swmm5_file = getTestFile('known_fivemin_swmm5.dat')
self.known_hourly_swmm5_file = getTestFile('known_hourly_swmm5.dat')
self.knwon_hourly_ncdc_file = getTestFile('known_hourly_NCDC.dat')

with open(self.known_fivemin_swmm5_file, 'r') as f:
self.known_fivemin_swmm5 = f.read()
Expand All @@ -36,7 +45,7 @@ def test_dumpSWMM5Format_form(self):
col='precip',
freq='5min',
dropzeros=True,
filename='test/test_dumpSWMM.dat'
filename=getTestFile('test_dumpSWMM.dat')
)
ntools.assert_true(isinstance(data, pandas.DataFrame))
ntools.assert_list_equal(
Expand All @@ -51,7 +60,7 @@ def test_dumpSWMM5Format_DropZeros(self):
col='precip',
freq='5min',
dropzeros=True,
filename='test/test_dumpSWMM_withoutZeros.dat'
filename=getTestFile('test_dumpSWMM_withoutZeros.dat')
)
ntools.assert_equal(data[data.precip == 0].shape[0], 0)

Expand All @@ -62,12 +71,12 @@ def test_dumpSWMM5Format_KeepZeros(self):
col='precip',
freq='5min',
dropzeros=False,
filename='test/test_dumpSWMM_withZeros.dat'
filename=getTestFile('test_dumpSWMM_withZeros.dat')
)
ntools.assert_greater(data[data.precip == 0].shape[0], 0)

def test_dumpSWMM5Format_Result5min(self):
testfilename = 'test/test_dumpSWMM_fivemin.dat'
testfilename = getTestFile('test_dumpSWMM_fivemin.dat')
data = exporters.SWMM5Format(
self.fivemin,
'Test-Station',
Expand All @@ -85,7 +94,7 @@ def test_dumpSWMM5Format_Result5min(self):
ntools.assert_equal(known_data, test_data)

def test_dumpSWMM5Format_ResultHourly(self):
testfilename = 'test/test_dumpSWMM_hourly.dat'
testfilename = getTestFile('test_dumpSWMM_hourly.dat')
data = exporters.SWMM5Format(
self.fivemin,
'Test-Station',
Expand All @@ -103,7 +112,7 @@ def test_dumpSWMM5Format_ResultHourly(self):
ntools.assert_equal(known_data, test_data)

def test_dumpNCDCFormat(self):
testfilename = 'test/test_dumpNCDCFormat.dat'
testfilename = getTestFile('test_dumpNCDCFormat.dat')
data = exporters.NCDCFormat(
self.hourly,
'041685',
Expand Down
41 changes: 25 additions & 16 deletions metar/tests/graphics_tests.py
@@ -1,55 +1,64 @@
from nose.tools import *
from metar import station
from metar import graphics
import os
import sys

import nose.tools as ntools
import datetime as dt
import pandas
import matplotlib
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = False

from metar import station
from metar import graphics

@ntools.nottest
def getTestFile(filename):
return os.path.join(sys.prefix, 'metar_data', 'test_data', filename)

class test_graphics():
def setup(self):
self.freqlist = ['5min', 'hourly', 'daily', 'weekly']
self.df = pandas.read_csv('test/data_for_graphics_tests.csv',
self.df = pandas.read_csv(getTestFile('data_for_graphics_tests.csv'),
parse_dates=True, index_col=0)

def teardown(self):
plt.close('all')

def test_rainClock(self):
'''Confirm that rainClock returns an mpl figure and two axes'''
fig = graphics.rainClock(self.df, fname='test/test_rainClock.png')
assert_true(isinstance(fig, matplotlib.figure.Figure))
fig = graphics.rainClock(self.df, fname=getTestFile('test_rainClock.png'))
ntools.assert_true(isinstance(fig, matplotlib.figure.Figure))
pass

def test_windRose_kt(self):
'''Confirm that windRose returns an mpl figure and one axis'''
fig = graphics.windRose(self.df, fname='test/test_windRose_kt.png',
fig = graphics.windRose(self.df, fname=getTestFile('test_windRose_kt.png'),
mph=False)
assert_true(isinstance(fig, matplotlib.figure.Figure))
ntools.assert_true(isinstance(fig, matplotlib.figure.Figure))

def test_windRose_mph(self):
'''Confirm that windRose returns an mpl figure and one axis'''
fig = graphics.windRose(self.df, fname='test/test_windRose_mph.png',
fig = graphics.windRose(self.df, fname=getTestFile('test_windRose_mph.png'),
mph=True)
assert_true(isinstance(fig, matplotlib.figure.Figure))
ntools.assert_true(isinstance(fig, matplotlib.figure.Figure))

def test_hyetograph(self):
'''Confirm that hyetograph returns an mpl figure and one axis'''
for freq in self.freqlist:
fname = 'test/test_hyetograph_%s.png' % freq
fname = getTestFile('test_hyetograph_%s.png' % freq)
fig = graphics.hyetograph(self.df, freq=freq, fname=fname)
assert_true(isinstance(fig, matplotlib.figure.Figure))
ntools.assert_true(isinstance(fig, matplotlib.figure.Figure))

def test_psychromograph(self):
'''Confirm that psychromograph returns an mpl figure and one axis'''
for freq in self.freqlist:
fname = 'test/test_psychromograph_%s.png' % freq
fname = getTestFile('test_psychromograph_%s.png' % freq)
fig = graphics.psychromograph(self.df, freq=freq, fname=fname)
assert_true(isinstance(fig, matplotlib.figure.Figure))
ntools.assert_true(isinstance(fig, matplotlib.figure.Figure))

def test_temperaturePlot(self):
'''Confirm that temperaturePlot returns an mpl figure and one axis'''
for freq in self.freqlist:
fname = 'test/test_temperaturePlot_%s.png' % freq
fname = getTestFile('test_temperaturePlot_%s.png' % freq)
fig = graphics.temperaturePlot(self.df, freq=freq, fname=fname)
assert_true(isinstance(fig, matplotlib.figure.Figure))
ntools.assert_true(isinstance(fig, matplotlib.figure.Figure))
15 changes: 12 additions & 3 deletions metar/tests/stations_tests.py
@@ -1,6 +1,7 @@
import shutil
import datetime as dt
import os
import sys

import nose.tools as ntools
import numpy as np
Expand All @@ -11,10 +12,12 @@
from metar import station
from metar import metar

@ntools.nottest
class fakeClass(object):
def value(self):
return 'item2'

@ntools.nottest
def makeFakeRainData():
tdelta = dt.datetime(2001, 1, 1, 1, 5) - dt.datetime(2001, 1, 1, 1, 0)
start = dt.datetime(2001, 1, 1, 12, 0)
Expand All @@ -32,6 +35,11 @@ def makeFakeRainData():
return daterange, rain_raw


@ntools.nottest
def getTestFile(filename):
return os.path.join(sys.prefix, 'metar_data', 'test_data', filename)


class test_station():
def setup(self):
self.max_attempts = 3
Expand Down Expand Up @@ -225,9 +233,10 @@ def test_check_step(self):
ntools.assert_raises(ValueError, station._check_step, 'fart')

def test_check_file(self):
ntools.assert_equal(station._check_file('test/testfile1'), 'bad')
ntools.assert_equal(station._check_file('test/testfile2'), 'ok')
ntools.assert_equal(station._check_file('test/testfile3'), 'not there')
known_results = ['bad', 'ok', 'not there']
for n, known_result in enumerate(known_results, 1):
fn = getTestFile('testfile{:d}'.format(n))
ntools.assert_equal(station._check_file(fn), known_result)

def test_check_dirs(self):
pass
Expand Down

0 comments on commit f6f27b9

Please sign in to comment.