| Conditions | 20 |
| Total Lines | 82 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| 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 build.rsudp.c_alert.Alert.__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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | import sys, os |
||
| 37 | def __init__(self, sta=5, lta=30, thresh=1.6, reset=1.55, bp=False, |
||
| 38 | debug=True, cha='HZ', q=False, sound=False, deconv=False, |
||
| 39 | *args, **kwargs): |
||
| 40 | |||
| 41 | """ |
||
| 42 | Initializing the alert thread with parameters to set up the recursive |
||
| 43 | STA-LTA trigger, filtering, and the channel used for listening. |
||
| 44 | |||
| 45 | """ |
||
| 46 | super().__init__() |
||
| 47 | self.sender = 'Alert' |
||
| 48 | self.alive = True |
||
| 49 | |||
| 50 | if q: |
||
| 51 | self.queue = q |
||
| 52 | else: |
||
| 53 | printE('no queue passed to consumer! Thread will exit now!', self.sender) |
||
| 54 | sys.stdout.flush() |
||
| 55 | sys.exit() |
||
| 56 | |||
| 57 | self.default_ch = 'HZ' |
||
| 58 | self.sta = sta |
||
| 59 | self.lta = lta |
||
| 60 | self.thresh = thresh |
||
| 61 | self.reset = reset |
||
| 62 | self.debug = debug |
||
| 63 | self.args = args |
||
| 64 | self.kwargs = kwargs |
||
| 65 | self.raw = rs.Stream() |
||
| 66 | self.stream = rs.Stream() |
||
| 67 | cha = self.default_ch if (cha == 'all') else cha |
||
| 68 | self.cha = cha if isinstance(cha, str) else cha[0] |
||
| 69 | self.sps = rs.sps |
||
| 70 | self.inv = rs.inv |
||
| 71 | self.stalta = np.ndarray(1) |
||
| 72 | self.maxstalta = 0 |
||
| 73 | self.units = 'counts' |
||
| 74 | |||
| 75 | deconv = deconv.upper() if deconv else False |
||
| 76 | self.deconv = self.deconv if (deconv in rs.UNITS) else False |
||
| 77 | if self.deconv and rs.inv: |
||
| 78 | self.units = '%s (%s)' % (rs.UNITS[0], rs.UNITS[1]) if (self.deconv in rs.UNITS) else self.units |
||
| 79 | printM('Signal deconvolution set to %s' % (self.deconv), self.sender) |
||
| 80 | else: |
||
| 81 | self.units = rs.UNITS['CHAN'][1] |
||
| 82 | self.deconv = False |
||
| 83 | printM('Alert stream units are %s' % (self.units.strip(' ').lower()), self.sender) |
||
| 84 | |||
| 85 | self.exceed = False |
||
| 86 | self.sound = sound |
||
| 87 | if bp: |
||
| 88 | self.freqmin = bp[0] |
||
| 89 | self.freqmax = bp[1] |
||
| 90 | self.freq = 0 |
||
| 91 | if (bp[0] <= 0) and (bp[1] >= (self.sps/2)): |
||
| 92 | self.filt = False |
||
| 93 | elif (bp[0] > 0) and (bp[1] >= (self.sps/2)): |
||
| 94 | self.filt = 'highpass' |
||
| 95 | self.freq = bp[0] |
||
| 96 | desc = 'low corner %s' % (bp[0]) |
||
| 97 | elif (bp[0] <= 0) and (bp[1] <= (self.sps/2)): |
||
| 98 | self.filt = 'lowpass' |
||
| 99 | self.freq = bp[1] |
||
| 100 | else: |
||
| 101 | self.filt = 'bandpass' |
||
| 102 | else: |
||
| 103 | self.filt = False |
||
| 104 | |||
| 105 | if self.cha not in str(rs.chns): |
||
| 106 | printE('Could not find channel %s in list of channels! Please correct and restart.' % self.cha, self.sender) |
||
| 107 | sys.exit(2) |
||
| 108 | |||
| 109 | listen_ch = '?%s' % self.cha |
||
| 110 | printM('Starting Alert trigger with sta=%ss, lta=%ss, and threshold=%s on channel=%s' |
||
| 111 | % (self.sta, self.lta, self.thresh, listen_ch), self.sender) |
||
| 112 | if self.filt == 'bandpass': |
||
| 113 | printM('Alert stream will be %s filtered from %s to %s Hz' |
||
| 114 | % (self.filt, self.freqmin, self.freqmax), self.sender) |
||
| 115 | elif self.filt in ('lowpass', 'highpass'): |
||
| 116 | modifier = 'below' if self.filt in 'lowpass' else 'above' |
||
| 117 | printM('Alert stream will be %s filtered %s %s Hz' |
||
| 118 | % (self.filt, modifier, self.freq), self.sender) |
||
| 119 | |||
| 287 |