Skip to content
Dima Kudosh edited this page Dec 24, 2016 · 6 revisions

Installation

Installing pydfs-lineup-optimizer is simple with pip, just run this in your terminal:

$ pip install pydfs-lineup-optimizer

Usage

Creating optimal lineups with pydfs-lineup-optimizer is very simple. Begin by importing Optimizer and Settings from the pydfs_lineup_optimizer module. For example, if you want optimize lineup for Yahoo Fantasy NBA it will look like this:

>>> from pydfs_lineup_optimizer import LineupOptimizer, YahooBasketballSettings

and now you can create optimizer. Optimizer constructor takes only settings argument:

optimizer = LineupOptimizer(YahooBasketballSettings)

Now you must load player list. Firstly you must download csv file from your dfs site and then pass path to it to your optimizer:

optimizer.load_players_from_CSV("path_to_csv")

You can get all players:

optimizer.players

Also, You can find all players with similar name using find_players methods:

optimizer.find_players('james')

Or get only one player with closest match:

optimizer.get_player_by_name('Rodney Hood')

And remove some players from list using remove player method:

optimizer.remove_player(player)

Also if you want to add some player for your lineup even if optimizer doesn't select it you can do it using add_player_to_lineup, and remove it using remove_player_from_lineup:

optimizer.add_player_to_lineup(player)
optimizer remove_player_from_lineup(player)

If it's impossible to add or remove player for some reasons it's will throw LineupOptimizerException.

Then you can create optimal lineups using optimize method:

lineups = optimizer.optimize(n=10)

It's return generator that yield 10 Lineup objects. n is unnecessary argument, If you don't specify n it will return generator with all possible lineups started from highest fppg to lowest fppg. If you want some players from same team or from same position you can add unnecessary teams or positions arguments. This arguments are dictionaries where key name of team/position and value number of players for this team/position:

lineups = optimizer.optimize(teams={'OKC': 4})

If it's impossible to evaluate optimal lineups it will immediately throw StopIteration. Now you can get all players from your lineups and projection/budget for your lineup:

lineups = optimizer.optimize(n=10)
for lineup in lineups:
    print(lineup.lineup)  # list of players
    print(lineup.fantasy_points_projection)
    print(lineup.salary_costs)

Lineup class implements str method, so you can write this:

str(lineup)

You can clear your lineup using reset_lineup method:

optimizer.reset_lineup()
Clone this wiki locally