Conditions | 10 |
Total Lines | 64 |
Code Lines | 33 |
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 bricknil.bricknil.main() 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 | # Copyright 2019 Virantha N. Ekanayake |
||
104 | async def main(system): |
||
105 | """ |
||
106 | Entry-point coroutine that handles everything. This is to be run run |
||
107 | in bricknil's main loop. |
||
108 | |||
109 | You normally don't need to use this directly, instead use start() |
||
110 | """ |
||
111 | |||
112 | # Instantiate the Bluetooth LE handler/queue |
||
113 | ble_q = BLEventQ() |
||
114 | # The web client out_going queue |
||
115 | web_out_queue = Queue() |
||
116 | # Instantiate socket listener |
||
117 | # task_socket = await spawn(socket_server, web_out_queue, ('',25000)) |
||
118 | # task_tcp = await spawn(bricknil_socket_server, web_out_queue, ('',25000)) |
||
119 | # await task_tcp.join() |
||
120 | |||
121 | # Call the user's system routine to instantiate the processes |
||
122 | await system() |
||
123 | |||
124 | hub_tasks = [] |
||
125 | hub_peripheral_listen_tasks = [] # Need to cancel these at the end |
||
126 | |||
127 | # Connect all the hubs first before enabling any of them |
||
128 | for hub in Hub.hubs: |
||
129 | hub.web_queue_out = web_out_queue |
||
130 | task_connect = spawn(ble_q.connect(hub)) |
||
131 | await task_connect |
||
132 | |||
133 | for hub in Hub.hubs: |
||
134 | # Start the peripheral listening loop in each hub |
||
135 | task_listen = spawn(hub.peripheral_message_loop()) |
||
136 | hub_peripheral_listen_tasks.append(task_listen) |
||
137 | |||
138 | # Need to wait here until all the ports are set |
||
139 | # Use a faster timeout the first time (for speeding up testing) |
||
140 | first_delay = True |
||
141 | for name, peripheral in hub.peripherals.items(): |
||
142 | while peripheral.port is None: |
||
143 | hub.message_info(f"Waiting for peripheral {name} to attach to a port") |
||
144 | if first_delay: |
||
145 | first_delay = False |
||
146 | await sleep(0.1) |
||
147 | else: |
||
148 | await sleep(1) |
||
149 | |||
150 | # Start each hub |
||
151 | task_run = spawn(hub.run()) |
||
152 | hub_tasks.append(task_run) |
||
153 | |||
154 | # Now wait for the tasks to finish |
||
155 | ble_q.message_info(f'Waiting for hubs to end') |
||
156 | |||
157 | for task in hub_tasks: |
||
158 | await task |
||
159 | ble_q.message_info(f'Hubs end') |
||
160 | await ble_q.disconnect() |
||
161 | for task in hub_peripheral_listen_tasks: |
||
162 | task.cancel() |
||
163 | |||
164 | # Print out the port information in debug mode |
||
165 | for hub in Hub.hubs: |
||
166 | if hub.query_port_info: |
||
167 | hub.message_info(pprint.pformat(hub.port_info)) |
||
168 | |||
183 |