BenBrostoff / draftfast

A tool to automate and optimize DraftKings and FanDuel lineup construction.
278 stars 113 forks source link

Output in JSON or CSV #151

Closed malpaso closed 4 years ago

malpaso commented 4 years ago

This is probably an enhancement but it would be nice to toggle the output of rosters to JSON or CSV files instead of ASCII.

BenBrostoff commented 4 years ago

I definitely see the value here but I’m not sure it’s a library responsibility. Remember that the optimize function here returns a Roster, which you can then parse however you want. I have a few APIs I’ve built around draftfast that do exactly this - one is a Flask app for instance that just jsonifies some rosters. CSV you could do the same approach - parse Roster instances and then write to a CSV. Does this make sense?

On Thu, Oct 17, 2019 at 1:14 AM Bill Tindal notifications@github.com wrote:

This is probably an enhancement but it would be nice to toggle the output of rosters to JSON or CSV files instead of ASCII.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/BenBrostoff/draftfast/issues/151?email_source=notifications&email_token=ABVK35DUUCYFJPMM2NBBVYTQO7YDNA5CNFSM4JBUJV2KYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HSLDSKA, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABVK35D3O74LN5VNGU3OWW3QO7YDNANCNFSM4JBUJV2A .

malpaso commented 4 years ago

@BenBrostoff That makes sense and funnily enough I've tried it but getting errors trying to convert the Rosters (not an experienced python dev). Seems serializing to JSON is a bit more complicated in python. Any hints? I'm trying with json.dumps() at the moment, I suspect I need a custom decoder on the roster object?

BenBrostoff commented 4 years ago

No problem, I’ll post some example code later. I would say as advice you should try to convert to a dict where keys are strings and values are either floats or strings - this will definitely work. JSON conversion will error out if you try to convert a class instance that may have special types.

On Thu, Oct 17, 2019 at 8:14 AM Bill Tindal notifications@github.com wrote:

@BenBrostoff https://github.com/BenBrostoff That makes sense and funnily enough I've tried it but getting errors trying to convert the Rosters (not an experienced python dev). Seems serializing to JSON is a bit more complicated in python. Any hints? I'm trying with json.dumps() at the moment, I suspect I need a custom decoder on the roster object?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/BenBrostoff/draftfast/issues/151?email_source=notifications&email_token=ABVK35CEVIYKN2C5VR6CYGLQPBJJPA5CNFSM4JBUJV2KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBP4A7A#issuecomment-543146108, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABVK35B3LWAXSML3LK24LQLQPBJJPANCNFSM4JBUJV2A .

BenBrostoff commented 4 years ago

@malpaso See below:

import json

roster = run(
    rule_set=rules.DK_NFL_RULE_SET,
    player_pool=player_pool,
    constraints=constraints,
    verbose=True,
    optimizer_settings=OptimizerSettings(
        no_offense_against_defense=True,
    ),
)

j = json.dumps(
    [
        {
            'name': p.name,
            'proj': p.proj,
            'team': p.team,
            'pos': p.pos
        }
        for p in roster.players
    ]
)

print(j)

prints

{"name": "Dalvin Cook", "proj": 24.93, "team": "MIN", "pos": "RB"}, {"name": "Cooper Kupp", "proj": 22.27, "team": "LAR", "pos": "WR"}, {"name": "Lamar Jackson", "proj": 27.71, "team": "BAL", "pos": "QB"}, {"name": "Austin Ekeler", "proj": 22.85, "team": "LAC", "pos": "RB"}, {"name": "Terry McLaurin", "proj": 19.96, "team": "WAS", "pos": "WR"}, {"name": "DJ Chark Jr.", "proj": 19.8, "team": "JAX", "pos": "WR"}, {"name": "Hunter Henry", "proj": 21.5, "team": "LAC", "pos": "TE"}, {"name": "Bears", "proj": 11.2, "team": "CHI", "pos": "DST"}, {"name": "Will Dissly", "proj": 12.32, "team": "SEA", "pos": "TE"}]

Closing this issue - let me know if you're still having trouble here. I don't think JSON / CSV conversion should be part of the core library but it's very do-able and I can help if you want to talk about consulting (my e-mail is in my profile).

malpaso commented 4 years ago

Thanks @BenBrostoff for the help!