Conditions | 6 |
Total Lines | 52 |
Code Lines | 38 |
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:
1 | import sys |
||
44 | def run(printFREQ=60, port=8888): |
||
45 | ''' |
||
46 | Initialize stream and print constants, then process data for packet loss. |
||
47 | |||
48 | :param int printFREQ: Value in seconds denoting the frequency with which this program will report packets lost |
||
49 | :param int port: Local port to listen on |
||
50 | |||
51 | ''' |
||
52 | global DPtime, DPttlLoss |
||
53 | printM("Initializing...") |
||
54 | raspberryshake.initRSlib(dport=port, rsstn='Z0000') # runs in quiet mode; suppresses needless output but shows errors |
||
55 | add_debug_handler() # now start console output |
||
56 | # initialize data stream constants |
||
57 | printM('Opened data port successfully.') |
||
58 | DP = raspberryshake.getDATA() |
||
59 | CHAN = raspberryshake.getCHN(DP) # first channel - doesn't matter which, used to stop looping |
||
60 | TR = raspberryshake.tf # transmission rate - in milliseconds |
||
61 | TRE = (TR+TR*.5) / 1000. # time diff / error to identify a missed packet |
||
62 | SR = raspberryshake.sps # sample / second |
||
63 | ttlCHN = raspberryshake.getTTLCHN() # total number of channels |
||
64 | printM(" Total Channels: %s" % ttlCHN) |
||
65 | printM(" Sample Rate: %s samples / second" % SR) |
||
66 | printM(" TX Rate: Every %s milliseconds" % TR) |
||
67 | |||
68 | # start processing data packets for packet loss detection |
||
69 | # initialize |
||
70 | chnNum = 0 |
||
71 | while chnNum < ttlCHN: |
||
72 | DP = raspberryshake.getDATA() |
||
73 | CHAN = raspberryshake.getCHN(DP) |
||
74 | DPtime[CHAN] = raspberryshake.getTIME(DP) |
||
75 | timeStart[CHAN] = DPtime[CHAN] |
||
76 | DPttlLoss[CHAN] = 0 |
||
77 | chnNum += 1 |
||
78 | |||
79 | printM('Data Packet reading begun.') |
||
80 | printM('Will report any DP loss as it happens and totals every %s seconds.' % printFREQ) |
||
81 | |||
82 | while 1: # loop forever |
||
83 | DP = raspberryshake.getDATA() |
||
84 | CHAN = raspberryshake.getCHN(DP) |
||
85 | timeS = raspberryshake.getTIME(DP) |
||
86 | timeD = timeS - DPtime[CHAN] |
||
87 | if abs(timeD) > TRE: |
||
88 | printM("DP loss of %s second(s) Current TS: %s, Previous TS: %s" % (round(timeD, 3), timeS, DPtime[CHAN])) |
||
89 | DPttlLoss[CHAN] += abs(int(timeD * TR)) |
||
90 | DPtime[CHAN] = timeS |
||
91 | |||
92 | if int(timeS) % printFREQ == 0: |
||
93 | if printTTLS(CHAN, TR): |
||
94 | timeStart[CHAN] = timeS |
||
95 | DPttlLoss[CHAN] = 0 |
||
96 | |||
162 |