Small formatting changes

- mostly brought over from [this MJ commit](d0f18561ea)
- 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
This commit is contained in:
Shay Carter 2023-08-31 11:19:42 -06:00
parent c33d8022fe
commit cae26d16c3
2 changed files with 15 additions and 24 deletions

View file

@ -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)

View file

@ -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)