0

i have a problem with python objects where i am creating and iterating over many objects simultaneously.

objects are always created with the correct attributes, but when i iterate over those objects, i check for a certain attribute in the object, this specific attribute randomly gets deleted from the object and then i get an error that the object does not have this attribute.

code:

      dead_list = []
      for key, trackable_vehicle in self.lane_vehciles.items():
         if trackable_vehicle.vehicle_status == 'DEAD' :
            dead_list.append(key)

      for key in dead_list:
         save_object(trackable_vehicle.vehicle_type+'_'+str(trackable_vehicle.object_ID), trackable_vehicle)
         self.lane_vehciles.pop(key, None)

Error: 'TrackableTruck' object has no attribute 'vehicle_status'

please note that this code works for me 95% of the time, and this happens for random objects although all objects has the same structure.

after a lot of debugging i just cant get my head around why this happens. the object is correctly created and if i print it before iteration it does have the "vehicle_status" attribute but after iteration the attribute disappears from the object

for example:

object at creation: {"object_ID": 55, "vehicle_type": "truck", "OTS": "2020-03-10 16:07:16", "lane_ID": 2.0, "vehicle_status": "ALIVE"}

object after iteration: {"object_ID": 55, "vehicle_type": "truck", "OTS": "2020-03-10 16:07:16", "lane_ID": 2.0}

i hope someone with better knowledge at python than myself can help me. thanks

EDIT object definition:

class TrackableTruck:
    def __init__(self, object_ID, vehicle_type, OTS, lane_ID, vehicle_status):

        # info from tracking server
        self.object_ID = object_ID
        self.vehicle_type = vehicle_type
        self.OTS = OTS
        self.lane_ID = lane_ID
        self.vehicle_status = vehicle_status

code to check and change vehicle status:

d = pickle.loads(full_msg[self.HEADERSIZE:])
d = json.loads(d)
vehicle_type = str(d['vehicle_type'])
vehicle_lane = int(d['lane_ID'])
vehicle_id = int(d['object_ID'])
vehicle_status = d['vehicle_status']

if vehicle_lane == 1:
    trackable_vehicle = self.streetLane_1.get_vehicle(vehicle_id)

    if trackable_vehicle is None:
        self.streetLane_1.add_vehicle(d)

    else:
        trackable_vehicle.vehicle_status = 'DEAD'   

object creation code:

def object_decoder(self,json_obj):
      return TrackableTruck(int(json_obj['object_ID']), json_obj['vehicle_type'], json_obj['OTS'], json_obj['lane_ID'], json_obj['vehicle_status'])

def add_vehicle(self, json_obj):
      trackable_vehicle = self.object_decoder(json_obj)
      self.lane_vehciles[int(trackable_vehicle.object_ID)] = trackable_vehicle
MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
ibrahim.bond
  • 127
  • 1
  • 3
  • 12
  • 3
    Please show us more code, especially your class definition with init. – h4z3 Mar 10 '20 at 14:38
  • 2
    Do you use the attribute anywhere else in your code in the process? Because you must be doing something somewhere, things don't just happen at random. – AndrejH Mar 10 '20 at 14:38
  • 1
    Are you sure you didn't just misspell vehicles on `for key, trackable_vehicle in self.lane_vehciles.items():` – damaredayo Mar 10 '20 at 14:38
  • 3
    We need a [MCVE] to have a chance of solving this. That includes enough (and ideally minimal) code to actually reproduce your error, and input that produces the error, expected output, and actual output (including full traceback if applicable). All as text, in the body of the question. As is, with the information provided, the solution could just as easily be "Martians" as anything else. – ShadowRanger Mar 10 '20 at 14:41
  • i have tried to include more code, i hope it is enough. – ibrahim.bond Mar 10 '20 at 14:49
  • @tyler as i said the code mostly works fine. but this happens for some objects. – ibrahim.bond Mar 10 '20 at 14:50
  • Is there any part in your code in which you delete ``vehicle_status`` from an object? – MisterMiyagi Mar 10 '20 at 14:55
  • @mistermiyagi No. – ibrahim.bond Mar 10 '20 at 14:56
  • 1
    Please provide the full traceback for "Error: 'TrackableTruck' object has no attribute 'vehicle_status'". – Guybrush Mar 10 '20 at 15:16
  • 1
    Sorry - this is bad code, and partial code - you should be using a library, or at least a generic code base to load those JSON records as Python vehicles -. The fragments of code above, without giving even a hint of the order they are run, are not enough for a meaningful answer. – jsbueno Mar 10 '20 at 15:17
  • 1
    You either are deleting the attribute explictly with a `del`, or are using the instance's `__dict__` instead of a JSON object when making a call to `.pop`, or creating your objects in a different way, not calling its `__init__` (by calling `__new__` or maybe trying to unpickle old versions of the same object). Or have ore than one `TrackableTruck` class definition in your code base - these are the only options. With the snippets above, one can't tell which. – jsbueno Mar 10 '20 at 15:20
  • i will try writing a code to for reproduction. – ibrahim.bond Mar 10 '20 at 15:54

0 Answers0