From c33d8022feba1e62c15356207efd80dd10740247 Mon Sep 17 00:00:00 2001 From: Shay Carter Date: Thu, 31 Aug 2023 10:44:05 -0600 Subject: [PATCH 1/2] Replace type() with isinstance() - changes were brought over from [MJ's previous commit](https://github.com/Unidata/python-awips/commit/9f55c3d0efd4a040268eb582470fe4a9377fb7eb) --- awips/gfe/IFPClient.py | 9 ++++----- .../dataplugin/gfe/db/objects/GFERecord.py | 4 ++-- .../common/dataplugin/gfe/db/objects/ParmID.py | 4 ++-- .../raytheon/uf/common/dataplugin/level/Level.py | 16 +++++++--------- .../uf/common/dataplugin/level/MasterLevel.py | 4 ++-- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/awips/gfe/IFPClient.py b/awips/gfe/IFPClient.py index d1e3f46..5709af9 100644 --- a/awips/gfe/IFPClient.py +++ b/awips/gfe/IFPClient.py @@ -42,7 +42,7 @@ from dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.server.messa # Date Ticket# Engineer Description # ------------ ---------- ----------- -------------------------- # 07/26/12 dgilling Initial Creation. -# +# 08/31/23 srcarter@ucar From MJ - replace type with isinstance # # @@ -59,7 +59,7 @@ class IFPClient(object): self.__siteId = site def commitGrid(self, request): - if type(request) is CommitGridRequest: + if isinstance(request, CommitGridRequest): return self.__commitGrid([request]) elif self.__isHomogenousIterable(request, CommitGridRequest): return self.__commitGrid([cgr for cgr in request]) @@ -74,8 +74,7 @@ class IFPClient(object): return ssr def getParmList(self, id): - argType = type(id) - if argType is DatabaseID: + if isinstance(argType, DatabaseID): return self.__getParmList([id]) elif self.__isHomogenousIterable(id, DatabaseID): return self.__getParmList([dbid for dbid in id]) @@ -102,7 +101,7 @@ class IFPClient(object): return True def getGridInventory(self, parmID): - if type(parmID) is ParmID: + if isinstance(parmID, ParmID): sr = self.__getGridInventory([parmID]) list = [] try: diff --git a/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/gfe/db/objects/GFERecord.py b/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/gfe/db/objects/GFERecord.py index 16453be..1ca1cd8 100644 --- a/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/gfe/db/objects/GFERecord.py +++ b/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/gfe/db/objects/GFERecord.py @@ -39,12 +39,12 @@ class GFERecord(PersistableDataObject): self.dataTime = None self.parmId = None if timeRange is not None: - if type(timeRange) is TimeRange: + if isinstance(timeRange, TimeRange): self.dataTime = DataTime(refTime=timeRange.getStart(), validPeriod=timeRange) else: raise TypeError("Invalid TimeRange object specified.") if parmId is not None: - if type(parmId) is ParmID.ParmID: + if isinstance(parmId, ParmID.ParmID): self.parmId = parmId self.parmName = parmId.getParmName() self.parmLevel = parmId.getParmLevel() diff --git a/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/gfe/db/objects/ParmID.py b/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/gfe/db/objects/ParmID.py index e479aad..1d91003 100644 --- a/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/gfe/db/objects/ParmID.py +++ b/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/gfe/db/objects/ParmID.py @@ -36,9 +36,9 @@ class ParmID(object): if (parmIdentifier is not None) and (dbId is not None): self.parmName = parmIdentifier - if type(dbId) is DatabaseID: + if isinstance(dbId, DatabaseID): self.dbId = dbId - elif type(dbId) is str: + elif isinstance(dbId, str): self.dbId = DatabaseID(dbId) else: raise TypeError("Invalid database ID specified.") diff --git a/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/level/Level.py b/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/level/Level.py index 594822d..5ae6273 100644 --- a/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/level/Level.py +++ b/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/level/Level.py @@ -31,7 +31,7 @@ # 06/29/15 4480 dgilling Implement __hash__, __eq__, # __str__ and rich comparison operators. # 02/17/22 8608 mapeters Subclass PersistableDataObject -# +# 08/31/23 srcarter@ucar From MJ - replace type with isinstance # import numpy @@ -71,17 +71,15 @@ class Level(PersistableDataObject): return hashCode def __eq__(self, other): - if type(self) != type(other): - return False - else: + if isinstance(self, type(other)): return (self.masterLevel, self.levelonevalue, self.leveltwovalue) == \ (other.masterLevel, other.levelonevalue, other.leveltwovalue) - + return False def __ne__(self, other): return not self.__eq__(other) def __lt__(self, other): - if type(self) != type(other): + if not isinstance(self, type(other)): return NotImplemented elif self.masterLevel.getName() != other.masterLevel.getName(): return NotImplemented @@ -113,7 +111,7 @@ class Level(PersistableDataObject): return False def __le__(self, other): - if type(self) != type(other): + if not isinstance(self, type(other)): return NotImplemented elif self.masterLevel.getName() != other.masterLevel.getName(): return NotImplemented @@ -121,7 +119,7 @@ class Level(PersistableDataObject): return self.__lt__(other) or self.__eq__(other) def __gt__(self, other): - if type(self) != type(other): + if not isinstance(self, type(other)): return NotImplemented elif self.masterLevel.getName() != other.masterLevel.getName(): return NotImplemented @@ -153,7 +151,7 @@ class Level(PersistableDataObject): return False def __ge__(self, other): - if type(self) != type(other): + if not isinstance(self, type(other)): return NotImplemented elif self.masterLevel.getName() != other.masterLevel.getName(): return NotImplemented diff --git a/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/level/MasterLevel.py b/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/level/MasterLevel.py index caa50e7..84516ac 100644 --- a/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/level/MasterLevel.py +++ b/dynamicserialize/dstypes/com/raytheon/uf/common/dataplugin/level/MasterLevel.py @@ -30,7 +30,7 @@ # 06/29/15 4480 dgilling Implement __hash__, __eq__ # and __str__. # 02/17/22 8608 mapeters Subclass PersistableDataObject -# +# 08/31/23 srcarter@ucar From MJ - replace type with isinstance # from dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.persist import PersistableDataObject @@ -49,7 +49,7 @@ class MasterLevel(PersistableDataObject): return hash(self.name) def __eq__(self, other): - if type(self) != type(other): + if not isinstance(self, type(other)): return False else: return self.name == other.name From cae26d16c3af99d02e07fa91967ee7a7fa00b7b0 Mon Sep 17 00:00:00 2001 From: Shay Carter Date: Thu, 31 Aug 2023 11:19:42 -0600 Subject: [PATCH 2/2] Small formatting changes - mostly brought over from [this MJ commit](https://github.com/Unidata/python-awips/commit/d0f18561ea6863c2aa27a236d948ea329d089518) - RequestConstraint - use fully qualified import - replace type with isinstance - swap 'null' == value to value == 'null' - properly check string object - DataTime - condense many lines into single lines of code --- .../dataquery/requests/RequestConstraint.py | 14 +++++------ .../com/raytheon/uf/common/time/DataTime.py | 25 ++++++------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/dynamicserialize/dstypes/com/raytheon/uf/common/dataquery/requests/RequestConstraint.py b/dynamicserialize/dstypes/com/raytheon/uf/common/dataquery/requests/RequestConstraint.py index be67109..6e228e5 100644 --- a/dynamicserialize/dstypes/com/raytheon/uf/common/dataquery/requests/RequestConstraint.py +++ b/dynamicserialize/dstypes/com/raytheon/uf/common/dataquery/requests/RequestConstraint.py @@ -27,11 +27,11 @@ # Jun 27, 2016 5725 tgurney Add NOT IN # Jul 22, 2016 2416 tgurney Add evaluate() # Jun 26, 2019 7888 tgurney Python 3 fixes -# +# Aug 31, 2023 srcarter@ucar Small formatting and logic changes # import re -from ...time import DataTime +from dynamicserialize.dstypes.com.raytheon.uf.common.time import DataTime class RequestConstraint(object): @@ -212,7 +212,7 @@ class RequestConstraint(object): return self._evalValue.match(value) is not None def _evalIsNull(self, value): - return value is None or 'null' == value + return value is None or value == 'null' # DAF-specific stuff begins here ########################################## @@ -228,11 +228,11 @@ class RequestConstraint(object): @staticmethod def _stringify(value): - if type(value) in {int, bool, float}: + if isinstance(value, (int, bool, float)): return str(value) - elif type(value) is str: + elif isinstance(value, str): return value - elif type(value) is bytes: + elif isinstance(value, bytes): return value.decode() else: # Collections are not allowed; they are handled separately. @@ -249,7 +249,7 @@ class RequestConstraint(object): except TypeError: raise TypeError("value for IN / NOT IN constraint must be an iterable") stringValue = ', '.join(cls._stringify(item) for item in iterator) - if len(stringValue) == 0: + if not stringValue: raise ValueError('cannot use IN / NOT IN with empty collection') obj = cls() obj.setConstraintType(constraintType) diff --git a/dynamicserialize/dstypes/com/raytheon/uf/common/time/DataTime.py b/dynamicserialize/dstypes/com/raytheon/uf/common/time/DataTime.py index 4b401e7..70eb10e 100644 --- a/dynamicserialize/dstypes/com/raytheon/uf/common/time/DataTime.py +++ b/dynamicserialize/dstypes/com/raytheon/uf/common/time/DataTime.py @@ -39,6 +39,7 @@ # plus misc cleanup # 09/13/19 7888 tgurney Python 3 division fixes # 11/18/19 7881 tgurney Fix __hash__ +# 08/31/23 srcarter@ucar Small formatting fixes to match MJ's changes import calendar @@ -59,12 +60,8 @@ _TIME = r'(\d{2}:\d{2}:\d{2})' _MILLIS = '(?:\.(\d{1,3})(?:\d{1,4})?)?' REFTIME_PATTERN_STR = _DATE + '[ _]' + _TIME + _MILLIS FORECAST_PATTERN_STR = r'(?:[ _]\((\d+)(?::(\d{1,2}))?\))?' -VALID_PERIOD_PATTERN_STR = r'(?:\[' + REFTIME_PATTERN_STR + \ - '--' + REFTIME_PATTERN_STR + r'\])?' -STR_PATTERN = re.compile( - REFTIME_PATTERN_STR + - FORECAST_PATTERN_STR + - VALID_PERIOD_PATTERN_STR) +VALID_PERIOD_PATTERN_STR = r'(?:\[' + REFTIME_PATTERN_STR + '--' + REFTIME_PATTERN_STR + r'\])?' +STR_PATTERN = re.compile(REFTIME_PATTERN_STR + FORECAST_PATTERN_STR + VALID_PERIOD_PATTERN_STR) class DataTime(object): @@ -88,18 +85,14 @@ class DataTime(object): self.fcstTime = 0 self.refTime = refTime if validPeriod is not None and not isinstance(validPeriod, TimeRange): - raise ValueError( - "Invalid validPeriod object specified for DataTime.") + raise ValueError("Invalid validPeriod object specified for DataTime.") self.validPeriod = validPeriod - self.utilityFlags = EnumSet( - 'com.raytheon.uf.common.time.DataTime$FLAG') + self.utilityFlags = EnumSet('com.raytheon.uf.common.time.DataTime$FLAG') self.levelValue = numpy.float64(-1.0) if self.refTime is not None: if isinstance(self.refTime, datetime.datetime): - self.refTime = int( - calendar.timegm( - self.refTime.utctimetuple()) * 1000) + self.refTime = int(calendar.timegm(self.refTime.utctimetuple()) * 1000) elif isinstance(self.refTime, time.struct_time): self.refTime = int(calendar.timegm(self.refTime) * 1000) elif hasattr(self.refTime, 'getTime'): @@ -124,8 +117,7 @@ class DataTime(object): fcstTimeMin = groups[4] periodStart = groups[5], groups[6], (groups[7] or 0) periodEnd = groups[8], groups[9], (groups[10] or 0) - self.refTime = self._getTimeAsEpochMillis( - rDate, rTime, rMillis) + self.refTime = self._getTimeAsEpochMillis(rDate, rTime, rMillis) if fcstTimeHr is not None: self.fcstTime = int(fcstTimeHr) * 3600 @@ -134,8 +126,7 @@ class DataTime(object): if periodStart[0] is not None: self.validPeriod = TimeRange() - periodStartTime = self._getTimeAsEpochMillis( - *periodStart) + periodStartTime = self._getTimeAsEpochMillis(*periodStart) self.validPeriod.setStart(periodStartTime // 1000) periodEndTime = self._getTimeAsEpochMillis(*periodEnd) self.validPeriod.setEnd(periodEndTime // 1000)