-
Notifications
You must be signed in to change notification settings - Fork 3
/
Class14_InClassDemoClass.py
123 lines (96 loc) · 3.9 KB
/
Class14_InClassDemoClass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Provides TimeSeries class for plotting time series stored in ODM.
"""
import pymysql
import matplotlib.pyplot as plt
from matplotlib import dates
__author__ = "Jon Goodall"
__copyright__ = "Copyright 2014, Jon Goodall"
__credits__ = ["Jon Goodall"]
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Jon Goodall"
__email__ = "[email protected]"
__status__ = "alpha"
class TimeSeries():
"""The summary line for a class docstring should fit on one line.
If the class has public attributes, they should be documented here
in an ``Attributes`` section and follow the same formatting as a
function's ``Args`` section.
Attributes:
attr1 (str): Description of `attr1`.
attr2 (list of str): Description of `attr2`.
attr3 (int): Description of `attr3`.
"""
def __init__(self, SiteID, VariableID):
"""Example of docstring on the __init__ method.
The __init__ method may be documented in either the class level
docstring, or as a docstring on the __init__ method itself.
Either form is acceptable, but the two should not be mixed. Choose one
convention to document the __init__ method and be consistent with it.
Note:
Do not include the `self` parameter in the ``Args`` section.
Args:
param1 (str): Description of `param1`.
param2 (list of str): Description of `param2`. Multiple
lines are supported.
param3 (int, optional): Description of `param3`, defaults to 0.
"""
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', \
db='LBRODM_small')
sql_statement = "SELECT LocalDateTime, DataValue FROM DataValues \
WHERE \
SiteID = " + str(SiteID) + " AND \
VariableID = " + str(VariableID) + " AND \
QualityControlLevelID = 1 \
ORDER BY LocalDateTime"
cursor = conn.cursor()
cursor.execute(sql_statement)
rows = cursor.fetchall()
cursor.close()
conn.close()
LocalDateTimes, DataValues = zip(*rows)
self.LocalDateTimes = LocalDateTimes
self.DataValues = DataValues
conn = pymysql.connect(host='localhost', port=3306, user='root', \
passwd='', db='LBRODM_small')
sql_statement = "SELECT SiteName FROM Sites \
WHERE SiteID = " + str(SiteID)
cursor = conn.cursor()
cursor.execute(sql_statement)
rows = cursor.fetchall()
cursor.close()
conn.close()
self.SiteName = rows[0][0]
def plot(self):
"""Class methods are similar to regular functions.
Note:
Do not include the `self` parameter in the ``Args`` section.
Args:
param1: The first parameter.
param2: The second parameter.
Returns:
True if successful, False otherwise.
"""
fig = plt.figure()
ax = fig.add_subplot(111)
from matplotlib import rc
font = {'family' : 'normal',
'weight' : 'normal',
'size' : 12}
rc('font', **font)
ax.set_ylabel("Temperature ($^\circ$C)") #hard coded for now
ax.set_xlabel("Date/Time") #hard coded for now
ax.xaxis.set_minor_locator(dates.MonthLocator())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))
ax.xaxis.set_major_locator(dates.YearLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n%Y'))
ax.grid(True)
ax.plot(self.LocalDateTimes, self.DataValues)
ax.set_title(self.SiteName)
fig.tight_layout()
fig.savefig('Class14_InClassDemoClass.png')
if __name__ == '__main__': #code to execute if called from command-line
ts = TimeSeries(2, 36)
ts.plot()