football 0.2.0 - Adding classes, helper functions and increasing usability

in #utopian-io6 years ago

Repository

https://github.com/amosbastian/football

The last couple of weeks I have slowly been updating football, which is a Python wrapper for the football-data API. If needed you can find some additional information about it in my previous contribution here. This contribution covers the following changes

Updated
  • All sub resources are now classes including functions to call retrieve additional information
  • Team related functions can now use the name, shortname or code of the team instead of just its ID
  • Football functions use classes instead
  • Unit tests for each function changed to test respective classes
Added
  • Helper functions for Table and Team classes

I will go over each class below and explain which helper functions were added to the class (if any) below!

Competition

Example
>>> competition = football.competition(445)
>>> print(competition)
Premier League 2017/18
>>> teams = competition.teams()
>>> for team in teams:
...     print(team)
... 
Arsenal FC
Leicester City FC
Watford FC
Liverpool FC
Southampton FC
Swansea City FC
...

The class includes all the properties that were available in the JSON returned by the previous version. It also includes three functions that allow you to get a list of the competition's fixtures (Fixture objects) and players (Player objects), and also the competition's table (Table object).

Fixture

Example
>>> fixture = football.fixture(159031)
>>> print(fixture)
Manchester United FC 2-1 Liverpool FC - 2018-03-10T12:30:00Z
>>> print(fixture.winner)
Manchester United FC

Also includes all properties that were already available. Have also added a function that sets the winner of the fixture (if possible) and the __str__ function that shows which teams are playing (or have already played) each other and when.

Player

Example
>>> players = football.players("Manchester United FC")
>>> for player in players:
...     print(player.name)
... 
Sergio Romero
Eric Bailly
Chris Smalling
Daley Blind
Luke Shaw
Matteo Darmian
...

Same as the other classes, but also includes a function team() that returns the team (Team object) the player is currently playing for. The player() function in the Footballclass was also updated so players of a team can not only be retrieved by a team's ID, but also the team's name, shortname or code!

Table

Before sorting

After sorting


Includes the function sort(column) which allows the user to sort the table by the given column. Also uses the PrettyTable package in the __str__ function to print the Table in an actual table (makes sense, right?), as seen above.

Team

Includes similar functions to the other classes that allow the user to retrieve a list of the team's players and fixtures. However, I also added three other functions, depth() which allows the user to see the positional depth of a team, results() which allows the user to get a team's results (games that were already played) and upcoming_fixtures() which allows the user to get a team's upcoming fixtures.

Since a few days the API has some wrong information (not sure why), so that's why the upcoming fixtures don't really make sense, nor the positional depth (seems like all players at the World Cup have disappeared from their club teams). Other than that, the latter two functions allow the user to also specify which competition they want to limit it to (e.g. only see results from the Premier League).


So basically every field in the original JSON was turned into a property of each respective class.

Originally I wanted to make the functions like team() that allow a user to get e.g. a player's team automatically send a request to the API so it does not have to be done manually, but because the API has a rate limit of 50 requests per minute I changed this. To do this I simply added a utils.py file that has a function that returns the needed headers, which allows each class to send requests themselves.

Furthermore, I also changed the Football class to check if the environment variable FOOTBALL_API_KEY is set, so the key doesn't need to be passed when instantiating the object. Of course I also updated each function in the Football class so it actually returns a (list of) object(s) instead and updated the tests to make sure everything works properly (if you find anything that doesn't, please let me know).

Finally I also added the constants.py file that includes some constants needed for e.g. formatting fixtures properly and allowing the user to use a team's name, shortname or code in team related functions (which is very useful in my opinion).

Roadmap

I have some plans for the future, but I really want to start learning JavaScript properly and return to working on Nutmega (have huge plans for this), but anyway here they are:

  • Create proper documentation (hi @ms10398)
  • Add more helper functions
  • Improve the use of filters
  • Add utilities for team/league/competition codes, names etc.
  • Include a CLI

GitHub Account

https://github.com/amosbastian

Commits & pull requests

Sort:  
  • Great job on the huge amount of work here.
  • Do you have plans for a game or gui to use this?
  • This makes my head spin: return [Fixture(fixture) for fixture in fixtures["fixtures"]]

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks, @helo! Actually I do have some plans like that and will probably work on it over the summer, so stay stuned!

And I love list comprehensions, haha!

Great thing Amos.
I did set up a poisson prediction model but in excel.
I know that by using java or Pi it would have much more value. But I can’t code 😔
Keep it going!

Thanks! Also that's seriously cool! Where did you get the statistics to create a prediction model? When I wanted to make this package I was looking at other APIs that have some more information like Opta's, but they are all paid subscriptions as far as I could tell. I found this website last year, which seems pretty cool and could possibly be scraped...

The pure data is downloaded from football-data.co.uk
A real gold mine for football data!
If you need some assistence let me know!

Hey @amosbastian
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations @amosbastian! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.28
TRX 0.12
JST 0.033
BTC 61726.60
ETH 3041.60
USDT 1.00
SBD 3.86