Created
May 27, 2020 12:03
-
-
Save kurzweil777/6f8ceaecfedeb521e62d0d6233240e6e to your computer and use it in GitHub Desktop.
First Attempts in OOP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| class Server: | |
| def __init__(self): | |
| """Creates a new server instance, with no active connections.""" | |
| self.connections = {} | |
| def add_connection(self, connection_id): | |
| """Adds a new connection to this server.""" | |
| connection_load = random.random() * 10 + 1 | |
| connection = {connection_id: connection_load} | |
| self.connections.update(connection) | |
| def close_connection(self, connection_id): | |
| """Closes a connection on this server.""" | |
| del self.connections[connection_id] | |
| def load(self): | |
| """Calculates the current load for all connections.""" | |
| total = sum(self.connections.values()) | |
| return total | |
| def __str__(self): | |
| """Returns a string with the current load of the server""" | |
| return "{:.2f}%".format(self.load()) | |
| server = Server() | |
| server.add_connection("192.168.1.1") | |
| print(server.load()) |
This is not working
What is not working exactly? If you add more connections, the server load will increase
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not working