Skip to content

Instantly share code, notes, and snippets.

@kurzweil777
Created May 27, 2020 12:03
Show Gist options
  • Save kurzweil777/6f8ceaecfedeb521e62d0d6233240e6e to your computer and use it in GitHub Desktop.
Save kurzweil777/6f8ceaecfedeb521e62d0d6233240e6e to your computer and use it in GitHub Desktop.
First Attempts in OOP
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())
@kurzweil777
Copy link
Author

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