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…

Premier League table using CodeFurther

Python

1

2

3

4

fromcodefurther.football importpremier_league

forleague_entry inpremier_league.league_table:

print(league_entry)

When executed, this code produces something like this…

Example output...

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

1 Chelsea FC 23 52 20 32 53

2 Manchester City FC 23 46 23 23 48

3 Manchester United FC 23 39 22 17 43

4 FC Southampton 22 37 16 21 42

5 Tottenham Hotspur FC 23 35 30 5 40

6 FC Arsenal London 22 39 25 14 39

7 Liverpool FC 23 33 27 6 38

8 West Ham United FC 23 35 27 8 36

9 Stoke City FC 23 26 28 -2 32

10 Swansea City 22 26 30 -4 30

11 Newcastle United 23 29 35 -6 30

12 Everton FC 23 31 34 -3 26

13 Crystal Palace 23 25 34 -9 23

14 Sunderland AFC 23 21 33 -12 23

15 West Bromwich Albion 23 20 32 -12 22

16 Aston Villa FC 22 11 25 -14 22

17 FC Burnley 23 21 38 -17 20

18 Hull City FC 23 20 33 -13 19

19 Queens Park Rangers 23 24 42 -18 19

20 Leicester City 23 21 37 -16 17

Other Leagues

So how about a look at the Spanish La Liga.

Python

1

2

3

4

fromcodefurther.football importla_liga

forleague_entry inla_liga.league_table:

print(league_entry)

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

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

1 CF Real Madrid 21 70 18 52 54

2 FC Barcelona 21 57 11 46 50

3 Atlético Madrid 21 43 20 23 47

4 Sevilla CF 21 35 24 11 42

5 Valencia CF 21 38 19 19 41

6 Villarreal CF 21 35 20 15 38

7 FC Málaga 21 24 21 3 35

8 SD Eibar 22 25 31 -6 27

9 Espanyol Barcelona 21 27 32 -5 26

10 Celta Vigo 21 20 23 -3 24

11 Deportivo La Coruna 22 20 34 -14 24

12 Athletic Bilbao 21 18 26 -8 23

13 Rayo Vallecano 21 22 38 -16 23

14 Real Sociedad San Sebastián 21 21 28 -7 22

15 FC Getafe 21 16 29 -13 20

16 UD Almeria 21 18 33 -15 19

17 Córdoba CF 21 16 31 -15 18

18 Granada CF 21 14 33 -19 18

19 CF Elche 21 18 42 -24 17

20 Levante UD 21 13 37 -24 16

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:

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.

Python

1

2

3

4

5

6

7

8

9

10

11

fromcodefurther.football importsouthampton

forfixture insouthampton.fixtures:

print(

fixture.match_day,

fixture.date,

fixture.result,

fixture.home_team_name,

"v",

fixture.away_team_name

)

…and here’s the output…

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

1 2014-08-17T12:30:00Z 2 - 1 Liverpool FC v FC Southampton

2 2014-08-23T14:00:00Z 0 - 0 FC Southampton v West Bromwich Albion

3 2014-08-30T14:00:00Z 1 - 3 West Ham United FC v FC Southampton

4 2014-09-13T14:00:00Z 4 - 0 FC Southampton v Newcastle United

5 2014-09-20T14:00:00Z 0 - 1 Swansea City v FC Southampton

6 2014-09-27T14:00:00Z 2 - 1 FC Southampton v Queens Park Rangers

7 2014-10-05T13:05:00Z 1 - 0 Tottenham Hotspur FC v FC Southampton

8 2014-10-18T14:00:00Z 8 - 0 FC Southampton v Sunderland AFC

9 2014-10-25T14:00:00Z 1 - 0 FC Southampton v Stoke City FC

10 2014-11-01T15:00:00Z 0 - 1 Hull City FC v FC Southampton

11 2014-11-08T15:00:00Z 2 - 0 FC Southampton v Leicester City

12 2014-11-24T20:00:00Z 1 - 1 Aston Villa FC v FC Southampton

13 2014-11-30T13:30:00Z 0 - 3 FC Southampton v Manchester City FC

14 2014-12-03T19:45:00Z 1 - 0 FC Arsenal London v FC Southampton

15 2014-12-08T20:00:00Z 1 - 2 FC Southampton v Manchester United FC

16 2014-12-13T15:00:00Z 1 - 0 FC Burnley v FC Southampton

17 2014-12-20T15:00:00Z 3 - 0 FC Southampton v Everton FC

18 2014-12-26T15:00:00Z 1 - 3 Crystal Palace v FC Southampton

19 2014-12-28T14:05:00Z 1 - 1 FC Southampton v Chelsea FC

20 2015-01-01T15:00:00Z 2 - 0 FC Southampton v FC Arsenal London

21 2015-01-11T16:00:00Z 0 - 1 Manchester United FC v FC Southampton

22 2015-01-17T17:30:00Z 1 - 2 Newcastle United v FC Southampton

23 2015-02-01T16:00:00Z 0 - 1 FC Southampton v Swansea City

24 2015-02-07T15:00:00Z - Queens Park Rangers v FC Southampton

25 2015-02-11T19:45:00Z - FC Southampton v West Ham United FC

26 2015-02-22T16:15:00Z - FC Southampton v Liverpool FC

27 2015-02-28T15:00:00Z - West Bromwich Albion v FC Southampton

28 2015-03-03T19:45:00Z - FC Southampton v Crystal Palace

29 2015-03-15T13:30:00Z - Chelsea FC v FC Southampton

30 2015-03-21T15:00:00Z - FC Southampton v FC Burnley

31 2015-04-04T14:00:00Z - Everton FC v FC Southampton

32 2015-04-11T14:00:00Z - FC Southampton v Hull City FC

33 2015-04-18T14:00:00Z - Stoke City FC v FC Southampton

34 2015-04-25T14:00:00Z - FC Southampton v Tottenham Hotspur FC

35 2015-05-02T14:00:00Z - Sunderland AFC v FC Southampton

36 2015-05-09T14:00:00Z - Leicester City v FC Southampton

37 2015-05-16T14:00:00Z - FC Southampton v Aston Villa FC

38 2015-05-24T14:00:00Z - Manchester City FC v FC Southampton

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:

Python

1

2

3

4

5

6

7

8

print(

fixture.match_day,

fixture.date,

fixture.result,

fixture.home_team_name,

"v",

fixture.away_team_name

)

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.

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:

Python

1

2

3

4

5

6

player.squad_number

player.name

player.nationality

player.date_of_birth

player.contract_expires

player.market_value

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

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:

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.

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…

Python

1

forteam in[real_madrid,barcelona]:

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…

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:
lambdax: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 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.

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…

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

fromcodefurther.football importreal_madrid,barcelona

# Create a dictionary where our nationality will be the key and the

# count of the number of times we've seen that nationality will be

# the corresponding value

nationality_counter={}

forteam in[real_madrid,barcelona]:

forplayer inteam.players:

# Let's check if this nationality exists in our dictionary. If it

# doesn't then we will need to create it

ifplayer.nationality notinnationality_counter:

nationality_counter[player.nationality]=0

# Now add one to our counter

nationality_counter[player.nationality]+=1

# Now loop through the keys in our nationality_counter dictionary and print

# the total number of players with that nationality

fornationality innationality_counter.keys():

print(

"The number of players with the nationality",

nationality,

"is",

nationality_counter[nationality]

)

…and the output…

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

The number of players with the nationality Brazil is 4

The number of players with the nationality Chile is 1

The number of players with the nationality Mexico is 1

The number of players with the nationality Costa Rica is 1

The number of players with the nationality Wales is 1

The number of players with the nationality Germany is 3

The number of players with the nationality Belgium is 1

The number of players with the nationality France is 3

The number of players with the nationality Spain is 22

The number of players with the nationality Portugal is 3

The number of players with the nationality Croatia is 2

The number of players with the nationality Uruguay is 1

The number of players with the nationality Colombia is 1

The number of players with the nationality Argentina is 2

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.

Python

1

fromcodefurther.textmessage importTextMessage

and this code to the end…

Python

1

2

3

4

5

6

text_message=TextMessage()

message='The number of Spanish players in El Cassico is: '+str(nationality_counter['Spain'])

text_message.send(

'+44**** ******',

message

)

And this is the message send to my mobile phone…

And here’s the entire code put together…

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

fromcodefurther.football importreal_madrid,barcelona

fromcodefurther.textmessage importTextMessage

# Create a dictionary where our nationality will be the key and the

# count of the number of times we've seen that nationality will be

# the corresponding value

nationality_counter={}

forteam in[real_madrid,barcelona]:

forplayer inteam.players:

# Let's check if this nationality exists in our dictionary. If it

# doesn't then we will need to create it

ifplayer.nationality notinnationality_counter:

nationality_counter[player.nationality]=0

# Now add one to our counter

nationality_counter[player.nationality]+=1

# Now loop through the keys in our nationality_counter dictionary and print

# the total number of players with that nationality

fornationality innationality_counter.keys():

print(

"The number of players with the nationality",

nationality,

"is",

nationality_counter[nationality]

)

text_message=TextMessage()

message='The number of Spanish players in El Cassico is: '+str(nationality_counter['Spain'])

text_message.send(

'+447795 054500',

message

)

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.