| Conditions | 8 |
| Total Lines | 53 |
| Code Lines | 34 |
| 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 select |
||
| 39 | def run_cycle(server_socket, socket_list, clients): |
||
| 40 | read_sockets, _, exception_sockets = select.select( |
||
| 41 | socket_list, [], socket_list |
||
| 42 | ) |
||
| 43 | |||
| 44 | for notified_socket in read_sockets: |
||
| 45 | if notified_socket == server_socket: |
||
| 46 | client_socket, client_address = server_socket.accept() |
||
| 47 | |||
| 48 | user = receive_message(client_socket) |
||
| 49 | |||
| 50 | if not user: |
||
| 51 | continue |
||
| 52 | |||
| 53 | socket_list.append(client_socket) |
||
| 54 | clients[client_socket] = user |
||
| 55 | |||
| 56 | print( |
||
| 57 | f"Accepted new connection from {client_address[0]}:" |
||
| 58 | f"{client_address[1]} username: {user['data'].decode('utf-8')}" |
||
| 59 | ) |
||
| 60 | |||
| 61 | else: |
||
| 62 | message = receive_message(notified_socket) |
||
| 63 | |||
| 64 | if not message: |
||
| 65 | print( |
||
| 66 | "Closed connection from", |
||
| 67 | clients[notified_socket]['data'].decode('utf-8') |
||
| 68 | ) |
||
| 69 | |||
| 70 | socket_list.remove(notified_socket) |
||
| 71 | del clients[notified_socket] |
||
| 72 | continue |
||
| 73 | |||
| 74 | user = clients[notified_socket] |
||
| 75 | print( |
||
| 76 | f"receive_message from {user['data'].decode('utf-8')}:", |
||
| 77 | message['data'].decode('utf-8') |
||
| 78 | ) |
||
| 79 | |||
| 80 | for client_socket in clients: |
||
| 81 | if client_socket != notified_socket: |
||
| 82 | client_socket.send( |
||
| 83 | ( |
||
| 84 | user["header"] + user["data"] |
||
| 85 | + message['header'] + message["data"] |
||
| 86 | ) |
||
| 87 | ) |
||
| 88 | |||
| 89 | for notified_socket in exception_sockets: |
||
| 90 | socket_list.remove(notified_socket) |
||
| 91 | del clients[notified_socket] |
||
| 92 |