Crash Course on Python Week 5 Coursera

Crash Course on Python Coursera Week 5 Google IT Automation with Python 2022


Assessment - Object-oriented programming

In this exercise, we'll create a few classes to simulate a server that's taking connections from the outside and then a load balancer that ensures that there are enough servers to serve those connections.

To represent the servers that are taking care of the connections, we'll use a Server class. Each connection is represented by an id, that could, for example, be the IP address of the computer connecting to the server. For our simulation, each connection creates a random amount of load in the server, between 1 and 10.

Run the following code that defines this Server class.

In [1]:

Now run the following cell to create a Server instance and add a connection to it, then check the load:

In [2]:
3.7018176360354893

After running the above code cell, if you get a NameError message, be sure to run the Server class definition code block first. The output should be 0. This is because some things are missing from the Server class. So, you'll need to go back and fill in the blanks to make it behave properly.

Go back to the Server class definition and fill in the missing parts for the add_connection and load methods to make the cell above print a number different than zero. As the load is calculated randomly, this number should be different each time the code is executed.

Hint: Recall that you can iterate through the values of your connections dictionary just as you would any sequence.

Great! If your output is a random number between 1 and 10, you have successfully coded the add_connection and load methods of the Server class. Well done!

What about closing a connection? Right now the close_connection method doesn't do anything. Go back to the Server class definition and fill in the missing code for the close_connection method to make the following code work correctly:

In [3]:
0

You have successfully coded the close_connection method if the cell above prints 0.

Hint: Remember that del dictionary[key] removes the item with key key from the dictionary.

Alright, we now have a basic implementation of the server class. Let's look at the basic LoadBalancing class. This class will start with only one server available. When a connection gets added, it will randomly select a server to serve that connection, and then pass on the connection to the server. The LoadBalancing class also needs to keep track of the ongoing connections to be able to close them. This is the basic structure:

In [20]:

As with the Server class, this class is currently incomplete. You need to fill in the gaps to make it work correctly. For example, this snippet should create a connection in the load balancer, assign it to a running server and then the load should be more than zero:

In [21]:
4.373910601271952

After running the above code, the output is 0. Fill in the missing parts for the add_connection and avg_load methods of the LoadBalancing class to make this print the right load. Be sure that the load balancer now has an average load more than 0 before proceeding.

What if we add a new server?

In [22]:
2.186955300635976

The average load should now be half of what it was before. If it's not, make sure you correctly fill in the missing gaps for the add_connection and avg_load methods so that this code works correctly.

Hint: You can iterate through the all servers in the self.servers list to get the total server load amount and then divide by the length of the self.servers list to compute the average load amount.

Fantastic! Now what about closing the connection?

In [23]:
0.0

Fill in the code of the LoadBalancing class to make the load go back to zero once the connection is closed.

Great job! Before, we added a server manually. But we want this to happen automatically when the average load is more than 50%. To make this possible, fill in the missing code for the ensure_availability method and call it from the add_connection method after a connection has been added. You can test it with the following code:

In [24]:
[35.13%,64.97%,2.44%]

The code above adds 20 new connections and then prints the loads for each server in the load balancer. If you coded correctly, new servers should have been added automatically to ensure that the average load of all servers is not more than 50%.

Run the following code to verify that the average load of the load balancer is not more than 50%.

In [25]:
34.179644135774

Awesome! If the average load is indeed less than 50%, you are all done with this assessment.


Week 5 Video Explanation :