| Conditions | 10 |
| Total Lines | 66 |
| Code Lines | 37 |
| 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._run_all() 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 |
||
| 112 | async def _run_all(ble, system): |
||
| 113 | """Curio run loop |
||
| 114 | """ |
||
| 115 | print('inside curio run loop') |
||
| 116 | # Instantiate the Bluetooth LE handler/queue |
||
| 117 | ble_q = BLEventQ(ble) |
||
| 118 | # The web client out_going queue |
||
| 119 | web_out_queue = Queue() |
||
| 120 | # Instantiate socket listener |
||
| 121 | #task_socket = await spawn(socket_server, web_out_queue, ('',25000)) |
||
| 122 | task_tcp = await spawn(bricknil_socket_server, web_out_queue, ('',25000)) |
||
| 123 | await task_tcp.join() |
||
| 124 | |||
| 125 | # Call the user's system routine to instantiate the processes |
||
| 126 | await system() |
||
| 127 | |||
| 128 | hub_tasks = [] |
||
| 129 | hub_peripheral_listen_tasks = [] # Need to cancel these at the end |
||
| 130 | |||
| 131 | # Run the bluetooth listen queue |
||
| 132 | task_ble_q = await spawn(ble_q.run()) |
||
| 133 | |||
| 134 | # Connect all the hubs first before enabling any of them |
||
| 135 | for hub in Hub.hubs: |
||
| 136 | hub.web_queue_out = web_out_queue |
||
| 137 | task_connect = await spawn(ble_q.connect(hub)) |
||
| 138 | await task_connect.join() |
||
| 139 | |||
| 140 | |||
| 141 | for hub in Hub.hubs: |
||
| 142 | # Start the peripheral listening loop in each hub |
||
| 143 | task_listen = await spawn(hub.peripheral_message_loop()) |
||
| 144 | hub_peripheral_listen_tasks.append(task_listen) |
||
| 145 | |||
| 146 | # Need to wait here until all the ports are set |
||
| 147 | # Use a faster timeout the first time (for speeding up testing) |
||
| 148 | first_delay = True |
||
| 149 | for name, peripheral in hub.peripherals.items(): |
||
| 150 | while peripheral.port is None: |
||
| 151 | hub.message_info(f"Waiting for peripheral {name} to attach to a port") |
||
| 152 | if first_delay: |
||
| 153 | first_delay = False |
||
| 154 | await sleep(0.1) |
||
| 155 | else: |
||
| 156 | await sleep(1) |
||
| 157 | |||
| 158 | # Start each hub |
||
| 159 | task_run = await spawn(hub.run()) |
||
| 160 | hub_tasks.append(task_run) |
||
| 161 | |||
| 162 | |||
| 163 | # Now wait for the tasks to finish |
||
| 164 | ble_q.message_info(f'Waiting for hubs to end') |
||
| 165 | |||
| 166 | for task in hub_tasks: |
||
| 167 | await task.join() |
||
| 168 | ble_q.message_info(f'Hubs end') |
||
| 169 | |||
| 170 | for task in hub_peripheral_listen_tasks: |
||
| 171 | await task.cancel() |
||
| 172 | await task_ble_q.cancel() |
||
| 173 | |||
| 174 | # Print out the port information in debug mode |
||
| 175 | for hub in Hub.hubs: |
||
| 176 | if hub.query_port_info: |
||
| 177 | hub.message_info(pprint.pformat(hub.port_info)) |
||
| 178 | |||
| 224 |