Conditions | 17 |
Total Lines | 76 |
Code Lines | 51 |
Lines | 76 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like dynamicserialize.dstypes.com.raytheon.uf.common.time.DataTime.DataTime.__init__() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # |
||
42 | def __init__(self, refTime=None, fcstTime=None, validPeriod=None): |
||
43 | """ |
||
44 | Construct a new DataTime. |
||
45 | May also be called as DataTime(str) to parse a string and create a |
||
46 | DataTime from it. Some examples of valid DataTime strings: |
||
47 | |||
48 | '2016-08-02 01:23:45.0' |
||
49 | '2016-08-02 01:23:45.123' |
||
50 | '2016-08-02 01:23:45.0 (17)', |
||
51 | '2016-08-02 01:23:45.0 (17:34)' |
||
52 | '2016-08-02 01:23:45.0[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' |
||
53 | '2016-08-02 01:23:45.456_(17:34)[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' |
||
54 | """ |
||
55 | if fcstTime is not None: |
||
56 | self.fcstTime = int(fcstTime) |
||
57 | else: |
||
58 | self.fcstTime = 0 |
||
59 | self.refTime = refTime |
||
60 | if validPeriod is not None and not isinstance(validPeriod, TimeRange): |
||
61 | raise ValueError("Invalid validPeriod object specified for DataTime.") |
||
62 | self.validPeriod = validPeriod |
||
63 | self.utilityFlags = EnumSet('com.raytheon.uf.common.time.DataTime$FLAG') |
||
64 | self.levelValue = numpy.float64(-1.0) |
||
65 | |||
66 | if self.refTime is not None: |
||
67 | if isinstance(self.refTime, datetime.datetime): |
||
68 | self.refTime = int(calendar.timegm(self.refTime.utctimetuple()) * 1000) |
||
69 | elif isinstance(self.refTime, time.struct_time): |
||
70 | self.refTime = int(calendar.timegm(self.refTime) * 1000) |
||
71 | elif hasattr(self.refTime, 'getTime'): |
||
72 | # getTime should be returning ms, there is no way to check this |
||
73 | # This is expected for java Date |
||
74 | self.refTime = int(self.refTime.getTime()) |
||
75 | else: |
||
76 | try: |
||
77 | self.refTime = int(self.refTime) |
||
78 | except ValueError: |
||
79 | # Assume first arg is a string. Attempt to parse. |
||
80 | match = STR_PATTERN.match(self.refTime) |
||
81 | if match is None: |
||
82 | raise ValueError('Could not parse DataTime info from ' |
||
83 | + str(refTime)) |
||
84 | |||
85 | groups = match.groups() |
||
86 | rMillis = groups[2] or 0 |
||
87 | fcstTimeHr = groups[3] |
||
88 | fcstTimeMin = groups[4] |
||
89 | periodStart = groups[5], groups[6], (groups[7] or 0) |
||
90 | periodEnd = groups[8], groups[9], (groups[10] or 0) |
||
91 | self.refTime = self._getTimeAsEpochMillis(groups[0], groups[1], rMillis) |
||
92 | |||
93 | if fcstTimeHr is not None: |
||
94 | self.fcstTime = int(fcstTimeHr) * 3600 |
||
95 | if fcstTimeMin is not None: |
||
96 | self.fcstTime += int(fcstTimeMin) * 60 |
||
97 | |||
98 | if periodStart[0] is not None: |
||
99 | self.validPeriod = TimeRange() |
||
100 | periodStartTime = self._getTimeAsEpochMillis(*periodStart) |
||
101 | self.validPeriod.setStart(periodStartTime / 1000) |
||
102 | periodEndTime = self._getTimeAsEpochMillis(*periodEnd) |
||
103 | self.validPeriod.setEnd(periodEndTime / 1000) |
||
104 | |||
105 | self.refTime = Date(self.refTime) |
||
106 | |||
107 | if self.validPeriod is None: |
||
108 | validTimeMillis = self.refTime.getTime() + int(self.fcstTime * 1000) |
||
109 | self.validPeriod = TimeRange() |
||
110 | self.validPeriod.setStart(validTimeMillis / 1000) |
||
111 | self.validPeriod.setEnd(validTimeMillis / 1000) |
||
112 | |||
113 | # figure out utility flags |
||
114 | if self.fcstTime: |
||
115 | self.utilityFlags.add("FCST_USED") |
||
116 | if self.validPeriod and self.validPeriod.isValid(): |
||
117 | self.utilityFlags.add("PERIOD_USED") |
||
118 | |||
266 |