-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnrel_utility.py
174 lines (139 loc) · 4.1 KB
/
nrel_utility.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import requests
import sys
from requests.exceptions import (
ConnectionError,
TooManyRedirects,
Timeout,
HTTPError,
)
from nrel_utility_errors import NRELFail, NRELNoResults, NRELError
from __version__ import VERSION
class NRELUtilityWrapper(object):
"""
"""
def __init__(self, api_key='DEMO_KEY'):
"""
"""
self.api_key = api_key
def get_nrel_utility_data(self, address):
"""
NREL Utility API
"""
url = 'http://developer.nrel.gov/api/utility_rates/v3.json'
params = {
'format': 'json',
'address': address,
'api_key': self.api_key
}
return self.get_data(url, params)
def get_data(self, url, params):
""""""
try:
headers = {
'User-Agent': 'NREL Utility Wrapper/' + VERSION + ' (Python)'
}
request = requests.get(
url=url,
params=params,
headers=headers,
)
print (request.url)
except (ConnectionError, TooManyRedirects, Timeout):
raise NRELFail
try:
request.raise_for_status()
except HTTPError:
raise NRELFail
try:
response_json = request.json()
except ValueError:
print ("NREL utility data is not a valid json structure")
raise NRELFail
if not response_json:
print ("NREL utility API did not return any results")
raise NRELNoResults
if response_json['errors']:
raise NRELError(response_json['errors'])
else:
return response_json
class NRELUtilityResults(object):
"""
"""
attribute_mapping = {}
def __init__(self, data):
"""
Creates instance of GeocoderResult from the provided JSON data array
"""
self.data = data
self.len = len(self.data)
self.current_data = self.data
def __len__(self):
return self.len
def __iter__(self):
return self
def return_next(self):
if self.current_index >= self.len:
raise StopIteration
self.current_data = self.data[self.current_index]
self.current_index += 1
return self
def __unicode__(self):
return self.utility_name
if sys.version_info[0] >= 3: # Python 3
def __str__(self):
return self.__unicode__()
def __next__(self):
return self.return_next()
else: # Python 2
def __str__(self):
return self.__unicode__().encode('utf8')
def next(self):
return self.return_next()
@property
def count(self):
return self.len
@property
def utility_name(self):
"""
Returns the name of the utilities
(From NREL: If there are multiple utility companies serving the
location, the names will be returned as a pipe-delimited string.)
"""
return self.current_data['outputs']['utility_name']
@property
def address(self):
"""
Returns the address of the NREL request
"""
return self.current_data['inputs']['address']
@property
def zipcode(self):
"""
Returns the zipcode of the NREL request
"""
return self.current_data['inputs']['address'][-5:]
@property
def utility_list(self):
"""
Returns the name and company ids of the utilities as a python list
"""
utility_list = []
json_attribute = self.current_data['outputs']['utility_info']
for item in json_attribute:
utility_list.append([item['utility_name'], item['company_id']])
return utility_list
@property
def residential(self):
"""
"""
return self.current_data['outputs']['residential']
@property
def commercial(self):
"""
"""
return self.current_data['outputs']['commercial']
@property
def industrial(self):
"""
"""
return self.current_data['outputs']['industrial']