Skip to content

Commit

Permalink
josuebrunel#37 : Almost ready to test, I promise.
Browse files Browse the repository at this point in the history
  • Loading branch information
peacing committed Jul 27, 2015
1 parent 77d00e5 commit 6b7cd9d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
12 changes: 12 additions & 0 deletions fantasy_sport/fantasy_sport.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,18 @@ def make_roster_move(self, league_keys, roster)
"""
uri = self._build_uri(None, league_keys, sub='roster')
uri = 'league/{0}'.format(uri)

def add_player(self, player_key, team_key)

This comment has been minimized.

Copy link
@josuebrunel

josuebrunel Jul 27, 2015

You've skipped the ":"

def add_player(self, player_key, team_key):
"""
Add a player to your team
Only two things to specify are player key and team key
yfs.add_player('346.p.9171', '346.l.1328.t.12')
"""

self.player_key = player_key
self.team_key = team_key

roster = Transaction(type='add', player_key=self.player_key, team_key=self.team_key)



75 changes: 72 additions & 3 deletions fantasy_sport/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ def to_xml(self,):
"""Return object as a xml string
"""
return ctree.tostring(self.xml)

@abc.abstractmethod
def xml_builder_waiver(self,):
raise NotImplementedError

@abc.abstractmethod
def xml_builder_pending_trade_put(self,):
raise NotImplementedError

@abc.abstractmethod
def xml_builder_add(self,):
raise NotImplementedError

@abc.abstractmethod
def xml_builder_drop(self,):
raise NotImplementedError

@abc.abstractmethod
def xml_builder_adddrop(self,):
raise NotImplementedError

@abc.abstractmethod
def xml_builder_pending_trade_post(self,):
raise NotImplementedError


class Roster(Base):
Expand Down Expand Up @@ -90,13 +114,15 @@ class Player(Base):
- position
"""

def __init__(self, player_key, position):
def __init__(self, player_key, type=None, position=None, destination_team_key=None, source_team_key=None,):
"""Initialize a player object
"""
super(Base, self).__init__()

self.player_key = player_key
self.position = position


self.xml_builder()
self.json_builder()

Expand Down Expand Up @@ -133,10 +159,17 @@ class Transaction(Base):
- action
"""

def __init__(self, type, transaction_key=None, waiver_priority=None, faab_bid=None, action=None, trade_note=None, voter_team_key=None):
def __init__(self, type, transaction_key=None, waiver_priority=None, faab_bid=None,
action=None, trade_note=None, voter_team_key=None
player_key=None, team_key=None,
):
"""Initialize a Transaction object
- types
-- PUT: waiver, pending_trade
-- POST: add, drop, add/drop, pending_trade (for proposing trades)
"""
super(Base, self).__init()
super(Base, self).__init__()

self.type = type
self.transaction_key = transaction_key
Expand All @@ -145,10 +178,46 @@ def __init__(self, type, transaction_key=None, waiver_priority=None, faab_bid=No
self.action = action
self.trade_note = trade_note
self.voter_team_key = voter_team_key
self.player_key = player_key
self.team_key = team_key

types = {'waiver': xml_builder_waiver, 'pending_trade': xml_builder_pending_trade_put,
'add': xml_builder_add, 'drop': xml_builder_drop, 'add/drop': xml_builder_adddrop
}

if self.type in types:
types[self.type](
else:
raise Exception("Method %s not implemented" % self.type

self.xml_builder()
#self.json_builder()

def xml_builder_add(self,):
"""Convert object to xml for adding a player
"""

content = ctree.Element('fantasy_content')
transaction = ctree.SubElement(content, 'transaction')

type = ctree.SubElement(transaction, 'type')
type.text = self.type

player = ctreeSubElement(transaction, 'player')

player_key = ctree.SubElement(player, 'player_key')
player_key.text = self.player_key

transaction_data = ctree.SubElement(player, 'transaction_data')

type = ctree.SubElement(transaction_data, 'type')
type.text = self.type

dest_team_key = ctree.SubElement(transaction_data, 'destination_team_key')
dest_team_key.text = self.team_key

self.xml = content

def xml_builder(self,):
"""Convert object to xml
"""
Expand Down

3 comments on commit 6b7cd9d

@josuebrunel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh great @unpairestgood, this is very interesting. I look forward to implementing all that ... Very good Paul

@peacing
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I'm very happy to say I got add_player to work in my latest commit! However, I simplified a lot of the code because I was trying to debug an error, which ended up being a simple example of this:(http://stackoverflow.com/questions/14247732/python-syntaxerror-non-keyword-after-keyword-arg) in the init parameters for the Transaction class.

To test the other functions I will undo some of the commenting out to make the program more flexible. I'm referring in particular to my use of a dictionary in line 184 to call different xml_builder functions based on the transaction type. Do you think this is a good way of doing this? I was trying to be a little more sophisticated than writing a bunch of if statements 😀

I know the code is perhaps a little less elegant with multiple build_xml functions. But I thought it was the easiest way to make the function calls in tests.py as simple as possible. For example, I wanted the parameters for add_player to be just the player code of the player you wish to add to your team. Right now you do have to also type in your team code and league code, but I envision those being moved to more of 'global' variables soon.

If there were only one build_xml function for all of add_player, pending_trade, edit waiver, etc... then the build_xml function would have tons of if statements because the input XML string is very different for each of them. That's my justification for splitting them up into more but simpler xml_builder functions.

Take care,

@josuebrunel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @unpairestgood ,

To test the other functions I will undo some of the commenting out to make the program more flexible. I'm referring in particular to my use of a dictionary in line 184 to call different xml_builder functions based on the transaction type. Do you think this is a good way of doing this? I was trying to be a little more sophisticated than writing a bunch of if statements

It's alright for me, i think i would have done the same. Waiting for you pull request 😉

Have a good one buddy

Please sign in to comment.