Example Code to Display Premier League Table

(This code is experimental and in development at the moment!)

I think the Football Data part of CodeFurther, really illustrates the code futher concept. By way of whetting your appetite, by the end of this blog entry, we will find out how many Spanish players, players in El Classico (The football derby between Real Madrid and FC Barcelona), and then once we know that we will send the information in a text message (SMS).  Does that seem like a bit of a stretch? Well, let’s see…

Ok, so here is some code that I’m working on at the moment. I’m focussing on the Football Data part of CodeFurther. I’m trying to make the data more interesting and more accessible.

League Tables

So here’s an extract that prints the current Premier League table…

When executed, this code produces something like this…

Other Leagues

So how about a look at the Spanish La Liga.

And, as you’d expect here is the output:

Team Details

OK, so let’s say you now want to look in depth at God’s own football team – Southampton FC.  First let’s look at the data we have on the team itself:

…and this produces the following output:

Adding Headings and Underlines

That’s OK, but I wanted to make CodeFurther make it easy to print column headings and underlines. So by adding the following code:

…and this time we can see that the data has column headings and a line of underline symbols:

Fixtures

And now, let’s say we want to look at the fixtures for God’s own football team, the code would look something like this…

And we’ll just add the heading and underlines as we did before…

…and the result is…

Working with Objects That Look Like Strings

The clever bit, if there is a clever bit, is that instead of fixture being a string (as it appears to be), it is actually an object that, when we use the print()function, displays a nice string representation of the object. To prove that we are dealing with an object and not just a string, we can print the individual elements of the fixtureobject too.

…and here’s the output…

Whilst the output isn’t as well formatted, you can probably see that this arrangement could be very useful in the classroom. It means that if  we need simplicity we can just use the print()function on the the object. Actually, if we use anything that tries to convert the object to a string it will return a well formatted version of the object. Whereas if we want to stretch our students, we can give them access to the individual attributes that make up the object itself. Like so:

We’ll see another example of this duality below as we look at the players in the team.

Player Details

We can also look at the squad of players for the various teams that we have access to. The following code lists all squad players for a team, and also displays the column headings and underlines.

God’s Own Football Team

The output shows the squad of players for God’s own football team…

Computing the Total Value of a Squad

Let’s imagine that we want to set our more able students the task of computing the total value of players in the squad. Well, we can do this by instead of treating the playerobject as a string and simply printing it (as we do in line 7 of the code above), we treat it as an object and access the individual attributes/properties such as:

So, to solve the challenge, we would need to keep a running total of the players’ market values. The following code achieves that…

And the result is…

Finding the Most Valuable Player

Another exercise we could set is to find the most valuable player. The logic here is to create a variable to store the current highest value player we’ve seen, and also remember the player who had that value. We then loop through the players and if we encounter a player with a higher value, we record that player as the new MVP and remember the new highest market value we’ve seen. The code for that might look like this:

…and the output…

 What if Southampton isn’t Your Team?

Well the answer is, “It should be!”. But, in the interests of balance, let’s take a look at a couple of inferior Spanish teams. This code should be relatively easy to follow, take a look and I’ll go through it below.

…and here’s the output…

What we see if we look back at the code, is that we’ve added another loop outside of the player loop. This time the loop looks like this…

This basically says that we’ve created a list that has two elements. One is the variable real_madrid and the other a variable called barcelona. Now on each iteration of the loop, the team variable will be set to the next variable in that list. So on the first running of the program, the team variable will take on the value of the real_madrid variable, and on the second iteration of the loop, the team variable will then be set to barcelona.

The rest of the code should be similar enough to the code we’re already familiar with. The outer loop, allows us to re-use that code, replacing the variable team with the next sequence in the list.

Sorting the lists by Player Value

Another exercise we might like to go through is sorting the list of players in a team by their market value.  Again, Python makes that very easy for us by supplying the sorted()function. So here is the code that displays the Real Madrid and Barcelona players and sorts them into reverse market value…

All of the magic happens on line 10…

What the sorted() function does here, is take the list that team.players returns,  and sorts it using the key supplied. The key definition looks a little cryptic, but it’s not really: lambda x: x.market_value.

This basically says – as I loop through the players in the list, I will call each player x, and then I will use the market_valueattribute of the object xto sort the list. Finally, I would like the list to be in reverse order – from highest to lowest, so I will pass a reversed= parameter and set its value to Trueby saying reversed=True.

The result of this program is shown below, and then we’ll look at another program that sorts the player data on another key…

Sorting on another key

So, what if we wanted to sort the information on another key? Let’s say the nationality of the players. The following code achieves this:

…and in the output below, we can see that the players are ordered in nationality order (alphabetically)…

Sorting on Date of Birth

Finally, to show how simple sorting is when we are dealing with objects, we’ll sort on the date of birth. The list will be sorted by oldest data first, which should mean that we will have a list of the oldest players first.

…and the results…

Keeping Count of the Number of Players for Each Country

Finally, let’s imagine that we want to keep a count of the most common nationalities in the Real Madrid and Barcelona teams. This means that we have to keep a counter for each nationality, and when we come across that nationality, we add one to the counter. Python gives us some great shortcuts for achieving this, but first to illustrate the programming challenge, we’ll do it long hand.

The strategy we will use is to employ a Python dictionary that will have a key of the nationality and a value of an integer count. If we encounter a nationality for the first time, then we need to add its key to our dictionary and set its counter to one.

The following code might do the job for us…

…and the output…

Sending that Text Message

OK, so now we have the number of Spanish players involved in El Classico, we can send that text message, or email, or tweet…  It’s really easy with CodeFurther… We basically add the following code to the beginning.

and this code to the end…

And this is the message send to my mobile phone…

Text message sent from Python

And here’s the entire code put together…

 

Wrap Up

I hope what you’ve seen here is that CodeFurther… allows coders of all ages and experience to get started, and as the student wants to push themselves further, then they can.

As I mentioned at the start of this entry, the functionality examined in this code isn’t yet available in the public version of CodeFurther. I expect I’ll get it published in the next week or two.

Danny Goodall

Posted in Example Code and tagged , .

Leave a Reply

Your email address will not be published. Required fields are marked *