Python Forum
delete a Python object that matches an atribute
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
delete a Python object that matches an atribute
#1
Hi,

How do I delete a Python object that matches an atribute?
I have the following code and I would like to delete created object with the attribute IP = 10.192.168.1.
It is not working, it only works if I do it directly like this:
del id1
.
I want to be able to delete the object if it matches an IP address.


import gc

class tech_dispo:
       
   tech_list = []
   techCount = 0

   def __init__(self, nom, ip,pc):
     
      if (nom in tech_dispo.tech_list): 
          print ("Tech in list") 
      else:
          self.nom = nom
          self.ip = ip
          self.pc = pc
          tech_dispo.techCount += 1
          tech_dispo.tech_list.append(nom)


   def display_total_tech(self):
    print ("Total tech %d" % tech_dispo.techCount)
    return tech_dispo.techCount

   
   def displaytech(self):
      print (self.nom,self.ip,self.pc)




id1 = tech_dispo("carl", "10.192.168.1","PC-01")

id2 = tech_dispo("Stéphane", "10.192.168.2","PC-02")

# Check created objects in class
for obj in gc.get_objects():
             if isinstance(obj, tech_dispo):
                 print(obj)

# Delete object with IP = 10.192.168.1
for obj in gc.get_objects():
             if isinstance(obj, tech_dispo):
                 if (obj.ip == "10.192.168.1"):
                      print("delete")
                      del obj


for obj in gc.get_objects():
             if isinstance(obj, tech_dispo):
                 print(obj)
Reply
#2
The problem is that you are trying to use the garbage collector as a container for the tech_dispo instances. That's not the way it should be used. In normal code, one does not interact directly with the garbage collector. When you need a container for a collection of instances, just create one. See the below example: I create a class TechDispoTeam, which is just a kind of dictionary to keep the instances. Potentially, I could have several teams
class TechDispo:
    
    def __init__(self, nom, ip, pc):
        self.nom = nom
        self.ip = ip
        self.pc = pc

    def __repr__(self):
        return "TechDispo{}".format((self.nom, self.ip, self.pc))
        
class TechDispoTeam(dict):
    def tech_dispo(self, nom, ip, pc):
        if nom not in self:
            self[nom] = TechDispo(nom, ip, pc)
        return self[nom]

team = TechDispoTeam()

t1 = team.tech_dispo('carl', '10.192.168.1', 'PC-01')
t2 = team.tech_dispo('Stéphane', '10.192.168.2', 'PC-02')

print('There are {} techs in the team'.format(len(team)))

# show all instances
for t in team.values():
    print(t)
    
# delete instance with ip '10.192.168.1'
for nom, t in list(team.items()):
    if t.ip == '10.192.168.1':
        del team[nom]

print('There are {} techs in the team'.format(len(team)))
Several questions could be addressed: what to do if the team recruits a second Carl? What to do if we call team.tech_dispo('carl', '10.192.168.7', 'PC-5') but Carl is already in the team? Should the new IP and PC replace the old ones for Carl? The current code ignores the new settings, etc.
Reply
#3
Thanks a lot, it is working great.
I really apreciate it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python update binary object (override delivered Object properties) pierre38 4 1,773 May-19-2022, 07:52 AM
Last Post: pierre38
  DELETE Post using Python FaceBook Graph API BIG_PESH 0 1,471 Mar-24-2022, 08:28 PM
Last Post: BIG_PESH
  Why doesn't gc delete an object without forcing a garbage collection call? AlekseyPython 5 3,765 Mar-19-2019, 02:10 AM
Last Post: micseydel
  Not Able To Delete First Node From Python Linked List ribena1980 8 4,275 Mar-05-2019, 03:14 PM
Last Post: ichabod801
  Detecting if image matches another kainev 2 2,915 Dec-02-2018, 02:00 PM
Last Post: kainev
  How to detect and tell user that no matches were found in a list RedSkeleton007 6 3,895 Jul-19-2018, 06:27 PM
Last Post: woooee
  for any loop _ want to know which matches 3Pinter 8 4,376 Jun-14-2018, 10:04 AM
Last Post: buran
  Find duplicate images and delete them using python and openCVq Prince_Bhatia 2 10,044 Dec-05-2017, 05:52 PM
Last Post: Prince_Bhatia
  Unable to delete duplicates in excel with Python tysondogerz 2 9,673 Nov-07-2017, 11:25 AM
Last Post: tysondogerz
  checking for matches between lists henrik0706 2 2,688 Oct-24-2017, 11:08 AM
Last Post: henrik0706

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020