0

I am a new user to python and am trying to update a class I have that is called Player(self, name, position) through a new class call API(object). In the class API(), I have the CRUD approach in creating a player, retrieving a player, updating a player, and lastly, deleting a player. I am struggling with the method in my update and delete function.

For update, I am calling self.retrieve_player[name] since it is a method that already reads the existing players from the initialized dictionary (found in the init file of class API.

Also, the main() function at the bottom is instantiating these methods by creating an instance of the object, calling API and the specified methods. e.g.:

c = API()
c.create_player('Buster Posey', 'Catcher')
c.retrieve_player('james')
c.update_player('Buster Posey', 'Firstbaseman')

The code I am struggling with is outputting an updated object that I created:

    def update_player(self, name, position): 
        updated_player = self.retrieve_player(name)
        updates = self.create_player(name, position)
        updated_player = updates
        return updates
        print('updated now!')

    def delete_player(self, name, position):
        del_player = self.retrieve_player[name]
        if name in del_player:
            del self._team[key]

For update_player, I was playing with concept since the mentality for updating variables is something like:

a = "apple"
b = "orange"
x = a
x = b #now apple is replaced with the value orange

e.g.:

I created the player (Buster Posey, Catcher) in c.create_player. I want to update this player to reflect his new position, e.g. (Buster Posey, FirstBaseman) through the method update_player.

Afterwards, I want to delete the player altogether by using the method delete_player.


Right now, when I use the current method that is defined for update_player, I get the created_player and the updated_player... python prints: Buster Posey, Catcher Buster Posey, FirstBaseman instead of ONLY printing Buster Posey, FirstBaseman.

More code:

class Player(object): 
    def __init__(self, name, position):
        self._name = name
        self._position = position       

class API(object): 
    playercount = 0
    managercount = 0

    def __init__(self):
        self._team = {} #acts like a db
        self._index = 0
        API.playercount += 1
        API.managercount += 1

##This is the CRUD method for the entity Player       
    def create_player(self, name, position):
        new_player = Player(name, position)
        self._team[self._index] = new_player
        self._index += 1
        print(name, position)

    def retrieve_player(self, name): # need to iterate through the dictionary
        for person in self._team.itervalues():
            if person._name == name:
                return person
                print(name)
            elif person._name != name: 
                print('Sorry, this gent does not exist.') 
thesayhey
  • 938
  • 3
  • 17
  • 38
  • Welcome to Stack Overflow! Please explain exactly what the difficulty you're having with the code is, as it's not entirely clear from the question and it's vital for understanding your problem. – SuperBiasedMan Jul 22 '15 at 16:24
  • Thank you @SuperBiasedMan, I just updated the question. I am struggling with implementing updates to an existing object with the method update_player and deleting said object with the method delete_player. – thesayhey Jul 22 '15 at 16:38
  • I've just read the (edited) question and I'm also struggling to understand what you're after. What's your *question*? – loopbackbee Jul 22 '15 at 16:44
  • @amarie It's not easy to give answer to this question just by seeing the peace of code given, please share your classes defined. – gsb-eng Jul 22 '15 at 16:54
  • @gsb-eng just added more detail – thesayhey Jul 22 '15 at 17:08

2 Answers2

0

Tweaked your code, I think this is what you were looking for.

class Player(object): 
    def __init__(self, name, position):
        self._name = name
        self._position = position

    def __repr__(self):
        return "%s --> %s" % (self._name, self._position)  

class API(object): 
    playercount = 0
    managercount = 0

    def __init__(self):
        self._team = {} #acts like a db
        self._index = 0
        API.playercount += 1
        API.managercount += 1

    ##This is the CRUD method for the entity Player       
    def create_player(self, name, position):
        new_player = Player(name, position)
        self._team[self._index] = new_player
        self._index += 1
        print(name, position)

    def retrieve_player(self, name): # need to iterate through the dictionary
         for index, person in self._team.items():
             if person._name == name:
                 print('Found this guy.. : ' + str(name))
                 return person
         print('No team member with name : ' + str(name))

    def update_player(self, name, position): 
        # predicting that you are trying to update position
        updated_player = self.retrieve_player(name)
        if updated_player:
            temp_pos = updated_player._position
            updated_player._position = position
            print('updated %s position from %s to %s' %( name, str(temp_pos), str(position)))
            return updated_player
        else:
            print('No team member with name : ' + str(name))

    def delete_player(self, name, position):
        for index, person in self._team.items():
            print(index, person._name)
            if person._name == name and person._position == position:
                self._team[index] = None
                del person
                print('Deleted team member : ' + str(name))
                return 1
        print('No team member with name : ' + str(name))

team = API()
team.create_player('A', 'Catcher')
team.create_player('B', 'FirstBatsman')
print(team._team)

player = team.retrieve_player('A')
print(player._name)
team.delete_player('B', 'FirstBatsman')
print(team._team)
gsb-eng
  • 1,211
  • 1
  • 9
  • 16
  • This make more sense! Thank you @gsb-eng ... I like the way you organized the main() calls too. I will incorporate this style of organization with programming. By the way, was the __repr__ method added for debugging or just something I should do everytime I create classes. I refer to this: http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python – thesayhey Jul 22 '15 at 19:25
0

Thanks to gsb-eng for helping me clarify the process. My issue was in attempting to change the name when I meant change the position, which requires calling the retrieve_player, I needed to iterate through the dictionary, than implementing a new position variable and value for the formerly created player.

This made it easier to clarify the delete_player method, which needed to iterate through the dictionary again and use the built-in function del.

Lastly, I forgot to use str() when printing variables.

CODE:

def update_player(self, name, position): # call retrieve () player than update
    #update position of player since name doesn't change 
    updated_player = self.retrieve_player(name)
    if updated_player:
        temp_pos = updated_player._position
        updated_player._position = position
        print('Updated %s position from %s to %s' %( name, str(temp_pos), str(position)))
        return updated_player
    else:
        print('No team member with name : ' + str(name))

def delete_player(self, name, position):
    for index, person in self._team.items():
        print(index, person._name)
        if person._name == name and person._position == position:
            self._team[index] = None
            del person
            print('Deleted team member : ' + str(name))
            return 1
    print('No team member with name : ' + str(name))   
thesayhey
  • 938
  • 3
  • 17
  • 38