Skip to content

Commit

Permalink
refactor: more specific exception type and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
BainanXia committed Feb 21, 2020
1 parent 1c71fcf commit 2cd4b2f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions powersimdata/scenario/scenario_info.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

from prereise.gather.constants import abv2state, state2loadzone, \
interconnect2loadzone

Expand Down Expand Up @@ -36,10 +38,10 @@ def area_to_loadzone(self, area):
elif area in {'Texas', 'Western', 'Eastern'}:
loadzone_set = interconnect2loadzone[area]
elif area == 'all':
loadzone_set = list(self.grid.zone2id.keys())
loadzone_set = set(self.grid.zone2id.keys())
else:
print("%s is incorrect." % area)
raise Exception('Invalid area')
raise ValueError('Invalid area')
return loadzone_set

def check_time_range(self, start_time, end_time):
Expand All @@ -58,12 +60,12 @@ def check_time_range(self, start_time, end_time):
(end_time not in self.pg.index):
print('Available time range [%s, %s]' % (str(self.pg.index[0]),
str(self.pg.index[-1])))
raise Exception('Invalid time range')
raise ValueError('Time range out of scope!')
start_i = self.pg.index.get_loc(start_time)
end_i = self.pg.index.get_loc(end_time)
if start_i > end_i:
print('start_time falls behind end_time!')
raise Exception('Invalid time range')
raise ValueError('Invalid time range: '
'start_time falls behind end_time!')
return start_i, end_i

def get_capacity(self, gentype, area):
Expand All @@ -81,7 +83,7 @@ def get_capacity(self, gentype, area):
(self.grid.plant['type'] == gentype) &
(self.grid.plant['zone_name'].isin(loadzone_set))]['Pmax'].sum()
if total_capacity == 0:
print('Warning: no such type of generator in the area specified!')
warnings.warn('No such type of generator in the area specified!')
return float(total_capacity)

def get_generation(self, gentype, area, start_time, end_time):
Expand Down Expand Up @@ -129,7 +131,7 @@ def get_profile_resource(self, gentype, area, start_time, end_time):
(self.grid.plant['zone_name'].
isin(loadzone_set))].index)
if gentype not in self.profile:
raise Exception('Invalid resource type')
raise ValueError('Invalid resource type')
query_profile_df = self.profile[gentype][plant_id_list]
self.check_time_range(start_time, end_time)
total_resource = query_profile_df.loc[start_time:end_time].sum().sum()
Expand Down Expand Up @@ -176,7 +178,8 @@ def get_capacity_factor(self, gentype, area, start_time, end_time):
total_hours = end_i - start_i + 1
total_capacity = self.get_capacity(gentype, area)
if total_capacity == 0:
raise ZeroDivisionError('Division by zero')
raise ZeroDivisionError('No such type of generator in the area '
'specified. Division by zero.')
total_generation = self.get_generation(gentype, area,
start_time, end_time)
cf = round(total_generation / (total_hours * total_capacity), 4)
Expand Down

0 comments on commit 2cd4b2f

Please sign in to comment.