Skip to content

Commit fba24a5

Browse files
committed
Cleanup readServices and base
1 parent b572e29 commit fba24a5

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

odm2api/ODM2/services/readService.py

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import (absolute_import, division, print_function)
22

3+
import warnings
4+
35
from odm2api.ODM2 import serviceBase
46
from odm2api.ODM2.models import (
57
ActionAnnotations, ActionDirectives, ActionExtensionPropertyValues, Actions,
@@ -29,7 +31,7 @@
2931
SpatialReferenceExternalIdentifiers, SpatialReferences, SpecimenBatchPositions,
3032
SpectraResultValueAnnotations, SpectraResultValues, TaxonomicClassifierExternalIdentifiers,
3133
TaxonomicClassifiers, TimeSeriesResultValueAnnotations, TimeSeriesResultValues,
32-
TimeSeriesResults, TrajectoryResultValueAnnotations, TrajectoryResultValues,
34+
TrajectoryResultValueAnnotations, TrajectoryResultValues,
3335
TransectResultValueAnnotations, TransectResultValues, Units, VariableExtensionPropertyValues,
3436
VariableExternalIdentifiers, Variables,
3537
)
@@ -38,8 +40,6 @@
3840

3941
from sqlalchemy import distinct, exists
4042

41-
import warnings
42-
4343
__author__ = 'sreeder'
4444

4545

@@ -75,8 +75,9 @@ def __init__(self, affiliation, person, org):
7575

7676

7777
class SamplingFeatureDataSet():
78-
datasets={}
79-
related_features={}
78+
datasets = {}
79+
related_features = {}
80+
8081
def __init__(self, samplingfeature, datasetresults, relatedfeatures):
8182
sf = samplingfeature
8283

@@ -94,26 +95,24 @@ def __init__(self, samplingfeature, datasetresults, relatedfeatures):
9495
self.assignDatasets(datasetresults)
9596
self.assignRelatedFeatures(relatedfeatures)
9697

97-
9898
print(self.datasets)
9999

100100
def assignDatasets(self, datasetresults):
101101
self.datasets = {}
102102
if datasetresults:
103103
for dsr in datasetresults:
104104
if dsr.DataSetObj not in self.datasets:
105-
#if the dataset is not in the dictionary, add it and the first result
106-
self.datasets[dsr.DataSetObj]=[]
105+
# if the dataset is not in the dictionary, add it and the first result
106+
self.datasets[dsr.DataSetObj] = []
107107
res = dsr.ResultObj
108108
# res.FeatureActionObj = None
109109
self.datasets[dsr.DataSetObj].append(res)
110110
else:
111-
#if the dataset is in the dictionary, append the result object to the list
111+
# if the dataset is in the dictionary, append the result object to the list
112112
res = dsr.ResultObj
113113
# res.FeatureActionObj = None
114114
self.datasets[dsr.DataSetObj].append(res)
115115

116-
117116
def assignRelatedFeatures(self, relatedfeatures):
118117
self.related_features = {}
119118
if relatedfeatures:
@@ -169,7 +168,8 @@ def resultExists(self, result):
169168
)
170169
return ret.scalar()
171170

172-
except:
171+
except Exception as e:
172+
print('Error running Query: {}'.format(e))
173173
return None
174174

175175
# Annotations
@@ -225,7 +225,8 @@ def getAnnotations(self, annottype=None, codes=None, ids=None, **kwargs):
225225
query = query.filter(Annotations.AnnotationID.in_(ids))
226226
return query.all()
227227

228-
except:
228+
except Exception as e:
229+
print('Error running Query: {}'.format(e))
229230
return None
230231

231232
# CV
@@ -384,15 +385,16 @@ def getVariables(self, ids=None, codes=None, sitecode=None, results=False):
384385
variables = [
385386
x[0] for x in
386387
self._session.query(distinct(Results.VariableID))
387-
.filter(Results.FeatureActionID == FeatureActions.FeatureActionID)
388-
.filter(FeatureActions.SamplingFeatureID == SamplingFeatures.SamplingFeatureID)
389-
.filter(SamplingFeatures.SamplingFeatureCode == sitecode).all()
388+
.filter(Results.FeatureActionID == FeatureActions.FeatureActionID)
389+
.filter(FeatureActions.SamplingFeatureID == SamplingFeatures.SamplingFeatureID)
390+
.filter(SamplingFeatures.SamplingFeatureCode == sitecode).all()
390391
]
391392
if ids:
392393
ids = list(set(ids).intersection(variables))
393394
else:
394395
ids = variables
395-
except:
396+
except Exception as e:
397+
print('Error running Query: {}'.format(e))
396398
pass
397399

398400
if results:
@@ -402,7 +404,8 @@ def getVariables(self, ids=None, codes=None, sitecode=None, results=False):
402404
ids = list(set(ids).intersection(variables))
403405
else:
404406
ids = variables
405-
except:
407+
except Exception as e:
408+
print('Error running Query: {}'.format(e))
406409
pass
407410

408411
query = self._session.query(Variables)
@@ -520,10 +523,10 @@ def getSamplingFeatures(self, ids=None, codes=None, uuids=None,
520523
if results:
521524
try:
522525
fas = [x[0] for x in self._session.query(distinct(Results.FeatureActionID)).all()]
523-
except:
526+
except Exception as e:
527+
print('Error running Query: {}'.format(e))
524528
return None
525-
sf = [x[0] for x in self._session.query(distinct(FeatureActions.SamplingFeatureID))
526-
.filter(FeatureActions.FeatureActionID.in_(fas)).all()]
529+
sf = [x[0] for x in self._session.query(distinct(FeatureActions.SamplingFeatureID)).filter(FeatureActions.FeatureActionID.in_(fas)).all()] # noqa
527530
if ids:
528531
ids = list(set(ids).intersection(sf))
529532
else:
@@ -557,8 +560,8 @@ def getRelatedSamplingFeatures(self, sfid=None, rfid=None, relationshiptype=None
557560
558561
"""
559562

560-
sf = self._session.query(distinct(SamplingFeatures.SamplingFeatureID))\
561-
.select_from(RelatedFeatures)
563+
sf = self._session.query(distinct(SamplingFeatures.SamplingFeatureID)) \
564+
.select_from(RelatedFeatures)
562565

563566
if sfid:
564567
sf = sf.join(RelatedFeatures.RelatedFeatureObj).filter(RelatedFeatures.SamplingFeatureID == sfid)
@@ -576,8 +579,6 @@ def getRelatedSamplingFeatures(self, sfid=None, rfid=None, relationshiptype=None
576579
print('Error running Query: {}'.format(e))
577580
return None
578581

579-
580-
581582
# Action
582583
def getActions(self, ids=None, acttype=None, sfid=None, **kwargs):
583584
"""
@@ -656,7 +657,6 @@ def getUnits(self, ids=None, name=None, unittype=None, **kwargs):
656657
print('Error running Query: {}'.format(e))
657658
return None
658659

659-
660660
# Organization
661661
def getOrganizations(self, ids=None, codes=None):
662662
"""
@@ -738,7 +738,7 @@ def getAffiliations(self, ids=None, personfirst=None, personlast=None, orgcode=N
738738
return None
739739

740740
# Results
741-
def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simulationid=None,
741+
def getResults(self, ids=None, restype=None, uuids=None, actionid=None, simulationid=None,
742742
variableid=None, siteid=None, sfids=None, sfuuids=None, sfcodes=None, **kwargs):
743743

744744
# TODO what if user sends in both type and actionid vs just actionid
@@ -791,10 +791,10 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
791791
if uuids:
792792
query = query.filter(Results.ResultUUID.in_(uuids))
793793
if simulationid:
794-
query = query.join(FeatureActions)\
795-
.join(Actions)\
796-
.join(Simulations)\
797-
.filter_by(SimulationID=simulationid)
794+
query = query.join(FeatureActions) \
795+
.join(Actions) \
796+
.join(Simulations) \
797+
.filter_by(SimulationID=simulationid)
798798
if actionid:
799799
query = query.join(FeatureActions).filter_by(ActionID=actionid)
800800
if 'sfid' in kwargs:
@@ -810,16 +810,15 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
810810
query = query.join(FeatureActions).filter(FeatureActions.SamplingFeatureID.in_(sfids))
811811

812812
if siteid:
813-
814813
sfids = [x[0] for x in self._session.query(
815814
distinct(SamplingFeatures.SamplingFeatureID))
816815
.select_from(RelatedFeatures)
817816
.join(RelatedFeatures.SamplingFeatureObj)
818817
.filter(RelatedFeatures.RelatedFeatureID == siteid)
819818
.all()
820-
]
819+
]
821820

822-
#TODO does this code do the same thing as the code above?
821+
# TODO does this code do the same thing as the code above?
823822
# sf_list = self.getRelatedSamplingFeatures(rfid=siteid)
824823
# sfids = []
825824
# for sf in sf_list:
@@ -834,7 +833,7 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
834833
return None
835834

836835
# Datasets
837-
def getDataSets(self, ids= None, codes=None, uuids=None, dstype=None):
836+
def getDataSets(self, ids=None, codes=None, uuids=None, dstype=None):
838837
"""
839838
Retrieve a list of Datasets
840839
@@ -906,7 +905,7 @@ def getDataSetsResults(self, ids=None, codes=None, uuids=None, dstype=None):
906905
if all(v is None for v in [ids, codes, uuids]):
907906
raise ValueError('Expected DataSetID OR DataSetUUID OR DataSetCode argument')
908907

909-
q = self._session.query(DataSetsResults)\
908+
q = self._session.query(DataSetsResults) \
910909
.join(DataSets)
911910
if ids:
912911
q = q.filter(DataSets.DataSetID.in_(ids))
@@ -955,12 +954,11 @@ def getDataSetsValues(self, ids=None, codes=None, uuids=None, dstype=None):
955954
resids.append(ds.ResultID)
956955

957956
try:
958-
return self.getResultValues(resultids = resids)
957+
return self.getResultValues(resultids=resids)
959958
except Exception as e:
960959
print('Error running Query {}'.format(e))
961960
return None
962961

963-
964962
def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=None, sftype=None):
965963
"""
966964
Retrieve a list of Datasets associated with the given sampling feature data.
@@ -990,11 +988,12 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
990988
991989
"""
992990

993-
994991
# make sure one of the three arguments has been sent in
995992
if all(v is None for v in [ids, codes, uuids, sftype]):
996-
raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode OR samplingFeatureType '
997-
'argument')
993+
raise ValueError(
994+
'Expected samplingFeatureID OR samplingFeatureUUID '
995+
'OR samplingFeatureCode OR samplingFeatureType '
996+
'argument')
998997

999998
sf_query = self._session.query(SamplingFeatures)
1000999
if sftype:
@@ -1011,18 +1010,17 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
10111010
sf_list.append(sf)
10121011

10131012
try:
1014-
sfds=[]
1013+
sfds = []
10151014
for sf in sf_list:
10161015

1017-
q = self._session.query(DataSetsResults)\
1018-
.join(Results)\
1019-
.join(FeatureActions)\
1016+
q = self._session.query(DataSetsResults) \
1017+
.join(Results) \
1018+
.join(FeatureActions) \
10201019
.filter(FeatureActions.SamplingFeatureID == sf.SamplingFeatureID)
10211020

10221021
if dstype:
10231022
q = q.filter_by(DatasetTypeCV=dstype)
10241023

1025-
10261024
vals = q.all()
10271025

10281026
related = self.getRelatedSamplingFeatures(sf.SamplingFeatureID)
@@ -1081,17 +1079,21 @@ def getEquipment(self, codes=None, equiptype=None, sfid=None, actionid=None, **k
10811079
warnings.warn('The parameter \'type\' is deprecated. Please use the equiptype parameter instead.',
10821080
DeprecationWarning, stacklevel=2)
10831081
equiptype = kwargs['type']
1082+
1083+
# NOTE: Equiptype currently unused!
1084+
if equiptype:
1085+
pass
10841086
e = self._session.query(Equipment)
10851087
if sfid:
10861088
e = e.join(EquipmentUsed) \
1087-
.join(Actions) \
1088-
.join(FeatureActions) \
1089-
.filter(FeatureActions.SamplingFeatureID == sfid)
1089+
.join(Actions) \
1090+
.join(FeatureActions) \
1091+
.filter(FeatureActions.SamplingFeatureID == sfid)
10901092
if codes:
10911093
e = e.filter(Equipment.EquipmentCode.in_(codes))
10921094
if actionid:
10931095
e = e.join(EquipmentUsed).join(Actions) \
1094-
.filter(Actions.ActionID == actionid)
1096+
.filter(Actions.ActionID == actionid)
10951097
return e.all()
10961098

10971099
def CalibrationReferenceEquipment(self):

odm2api/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import (absolute_import, division, print_function)
22

3+
34
class serviceBase(object):
45
def __init__(self, session_factory, debug=False):
56
"""Must send in either a session_factory."""
@@ -42,10 +43,9 @@ def __repr__(self):
4243
if 'obj' in v.lower():
4344
del valuedict[v]
4445

45-
if v == "_sa_instance_state":
46-
del valuedict["_sa_instance_state"]
47-
return "<%s(%s)>" % (self.__class__.__name__, str(valuedict))
48-
46+
if v == '_sa_instance_state':
47+
del valuedict['_sa_instance_state']
48+
return '<%s(%s)>' % (self.__class__.__name__, str(valuedict))
4949

5050

5151
class modelBase():

0 commit comments

Comments
 (0)