1

I have 3 python classes. The Order class has an attribute that is a list passed at instantiation. The Cue class also has a list attribute but is initialized with an empty list. My goal is to update a group of Cue class instances dynamically by a parsing function that loops over Order.items list and evaluates the condition of in Cue.items_they_need_on_their_q list attribute and if True add it to the Cue.working attribute of that instance.

from itertools import count

class Item(object):
    
    _ids = count(0)
    def __init__(self, name, inventory):
    self.item_number = next(self._ids)

class Order:
    _order_ref_number = count(0)
    def __init__(self, items=[]):
        self.order_ref_number = next(self._order_ref_number)
        self.items = items


class Cue:
    """ alright, queue, im lazy """
    def __init__(self,  working=[], sold=[]):
        self.working = working
        self.sold = sold
   
    items_they_need_on_their_q = []

itemOne = Item(name='cow', inventory='100')

itemTwo = Item(name='pig', inventory='10')

itemThree = Item(name='goat', inventory='6')

itemFour = Item(name='buick', inventory='1000')

stationOne = Cue()
stationOne.items_they_need_on_their_q = [itemOne.item_number, itemThree.item_number]

stationTwo = Cue()
stationTwo.items_they_need_on_their_q = [itemTwo.item_number, itemFour.item_number]


def parse_order(takesOrderClass):
    for i in takesOrderClass.items:
         if i.item_number in stationOne.items_they_need_on_their_q:
            stationOne.working.append(i)
         
         if i.item_number in stationTwo.items_they_need_on_their_q:
            stationTwo.working.append(i)

bob = Order(items=[itemOne,itemTwo,itemFour])

parse_order(bob)

print(stationOne.working)
print(stationTwo.working)

I expected it would append only those items that resolved to True in the list and route those. Instead I get all three Item objects in both instances of Cues working lists

output: [<__main__.Item object at 0x0000017E19B48FD0>, <__main__.Item object at 0x0000017E19B48F70>, <__main__.Item object at 0x0000017E19B48D90>]
[<__main__.Item object at 0x0000017E19B48FD0>, <__main__.Item object at 0x0000017E19B48F70>, <__main__.Item object at 0x0000017E19B48D90>]

When I print the conditions to sanity check the order condition satisfies that indeed stationOne returns [True,False,False] when the order is looped over comparing items as well as stationTwo returning [False,False,True].

I also tried using the following conditions in the loop

if i.item_number == True: 
##and ...
check_one = i.item_number in stationOne.items_they_need_on_their_q 
if check_one == True:
   do the thing
##and ...
if check_one == 1:
   do the thing 

If these are all evaluating truthwise correctly why are the lists not effectively filtered and being populated with all results and what would be a better solution?

Y_no_work
  • 11
  • 2

0 Answers0