Search code examples
pythonlistdictionarydata-structuresfifo

batchwise values consumption using python list and dictionary


Hello all i need a little help in finding solution for a datastructure problem using python . I have two datastructures, batches dictionary which contain batch qty for a certain batch and second datastucture so_data is list of orders. Now task is to loop over so_data and consume batchwise quantities in batches on FIFO basis.

batches = {'batch1': 200, 'batch2': 300, 'batch3': 200, 'batch4': 200,
         'batch5': 400, 'batch6': 100, 'batch7': 200, 'batch8': 300}

so_data = [50, 50, 50, 10, 340, 10, 40, 20, 150, 330, 50, 150, 20, 50, 30, 100, 60]


def batchwise_consumption(so_data, batches):
    print(batches)
    for so_qty in so_data:

        for bch,bch_qty in batches.items():
            if bch_qty > 0:
                print('order qty: ', so_qty, ', curr batch qty: ', bch_qty, ', curr batch: ', bch)
                if bch_qty >= so_qty:
                    # order fullfilled by one batch
                    batches[bch] = bch_qty - so_qty
                    print('\trem qty: ', batches[bch])
                    break # break on full filling order
                else:
                    # order fullfilled by partial batches
                    partial_qty = batches[bch]
                    batches[bch] = batches[bch] - batches[bch]
                    rem_so_qty = so_qty-partial_qty
                    print('\tpartial qty', partial_qty, ', rem so qty', rem_so_qty)
                    while partial_qty != so_qty:
                        # make so_qty from multiple batches 
                        break # break on full filling order

    print(batches)
batchwise_consumption(so_data, batches)

This problem is part of dataset, following is simulation of batchwise consumption batchwise consumption

Any help or some better solution is appreciated. Thanks in advance for helping .


Solution

  • Finally resolved: Well the task was to consume batchwise stock for Sales order list. So after looping sales order I checked:

    If so_qty is less than batch qty and fit within given index of batch qty then decreased so_qty from batch qty, then appended current batch into consumed batch list of dictionary which is maintaining batchwise consumption for each order and finally set current so_qty to 0 as order is fullfilled

    Else if so_qty is greater than batch qty than reduce batch qty from current so_qty, append remaining batch qty to consumed batch list, set current batch to 0 and continue with same so_qty unless it became 0 and break the loop for current so_qty when 0.

    batches = {'batch1': 200, 'batch2': 300, 'batch3': 200, 'batch4': 200,
             'batch5': 400, 'batch6': 100, 'batch7': 200, 'batch8': 300, 'batch9': 50}
    
    so_data = [50, 50, 50, 10, 340, 10, 40, 20, 150, 330, 50, 150, 20, 50, 30, 100, 60, 400]
    
    def batchwise_consumption(so_data, batches):
        order_no=0
        for so_qty in so_data:
            order_no +=1
            print(order_no, '\tso qty ', so_qty)
            baches_consumed = []
            for bch,bch_qty in batches.items():
                if bch_qty > 0:
                    if so_qty <= bch_qty:
                        batches[bch] = bch_qty - so_qty
                        baches_consumed.append({bch: so_qty})
                        so_qty = 0
    
                    elif so_qty > bch_qty:
                        so_qty = so_qty - bch_qty
                        batches[bch] = 0
                        baches_consumed.append({bch: bch_qty})
                        continue
    
                    if so_qty == 0:
                        break
            print('\t', baches_consumed)
    
    batchwise_consumption(so_data, batches)