adds decorators to reduce boilerplate code

This commit is contained in:
Andy Eschbacher
2017-01-12 17:03:01 -05:00
parent 7322931ca1
commit 4b3481b1a6
@@ -6,56 +6,45 @@ NULL_VALUE_ERROR = ('No usable data passed to analysis. Check your input rows '
'for null values and fill in appropriately.')
def verify_data(data):
if len(data) == 0:
plpy.error(NULL_VALUE_ERROR)
def verify_data(f):
def wrapper(*args, **kwargs):
try:
print('kwargs: %s' % str(kwargs))
data = f(*args, **kwargs)
if len(data) == 0:
plpy.error(NULL_VALUE_ERROR)
else:
return data
except Exception, err:
plpy.error('Analysis failed: {}'.format(err))
return wrapper
class AnalysisDataProvider(object):
@verify_data
def get_getis(self, w_type, params):
"""fetch data for getis ord's g"""
try:
query = pu.construct_neighbor_query(w_type, params)
data = plpy.execute(query)
# if there are no neighbors or all nulls, exit
verify_data(data)
return data
except plpy.SPIError, err:
plpy.error('Analysis failed: %s' % err)
query = pu.construct_neighbor_query(w_type, params)
return plpy.execute(query)
@verify_data
def get_markov(self, w_type, params):
"""fetch data for spatial markov"""
try:
query = pu.construct_neighbor_query(w_type, params)
data = plpy.execute(query)
verify_data(data)
return data
except plpy.SPIError, err:
plpy.error('Analysis failed: %s' % err)
query = pu.construct_neighbor_query(w_type, params)
return plpy.execute(query)
@verify_data
def get_moran(self, w_type, params):
"""fetch data for moran's i analyses"""
try:
query = pu.construct_neighbor_query(w_type, params)
data = plpy.execute(query)
# if there are no neighbors, exit
verify_data(data)
return data
except plpy.SPIError, err:
plpy.error('Analysis failed: %s' % err)
query = pu.construct_neighbor_query(w_type, params)
return plpy.execute(query)
@verify_data
def get_nonspatial_kmeans(self, query):
"""fetch data for non-spatial kmeans"""
try:
data = plpy.execute(query)
verify_data(data)
return data
except plpy.SPIError, err:
plpy.error('Analysis failed: %s' % err)
return plpy.execute(query)
@verify_data
def get_spatial_kmeans(self, params):
"""fetch data for spatial kmeans"""
query = '''
@@ -66,9 +55,5 @@ class AnalysisDataProvider(object):
FROM ({subquery}) As a
WHERE "{geom_col}" IS NOT NULL
'''.format(**params)
try:
data = plpy.execute(query)
verify_data(data)
return data
except plpy.SPIError, err:
plpy.error('Analysis failed: %s' % err)
return plpy.execute(query)