how to optimize this code and algorithm?

时间:2017-08-05 11:15:54

标签: python algorithm data-structures openerp

 def compute_qty(self):
    prduct = self.env["product.product"]
    attribute = self.env["product.attribute.value"]       
    for line in self.qty_line_id:
        stake_meter = line.pipe_size
        line_qty = line.pipe_qty
        pipe_list = []
        qty_list = []
        pipe_size_qty_list = []
        pipe_size_qty_list_of_list = []

        for obj in prduct.search([('product_tmpl_id','=',line.product_id.product_tmpl_id.id),('qty_available','>', 0)]):  
            for attr in obj.attribute_value_ids.ids:
                for name in attribute.search([('id','=', str(attr))]):
                    pipe_product_size_qty_list = []
                    if float(str(name.name)) >= line.pipe_size:
                        pipe_size_qty_list.append(float(str(name.name)))
                        pipe_size_qty_list.append(obj.qty_available)
                        pipe_size_qty_list_of_list.append(pipe_size_qty_list)
                        pipe_size_qty_list = []

        pipe_size_qty_list_of_list = sorted(pipe_size_qty_list_of_list)
        i = 0
        n = line_qty
        t = 0
        while n !=0 and i < len(pipe_size_qty_list_of_list):
            pipe_qty_need =  math.floor(float(pipe_size_qty_list_of_list[i][0] / pipe_meter))
            if pipe_qty_need == 1 and pipe_size_qty_list_of_list[i][0] > pipe_meter:     
                if pipe_size_qty_list_of_list[i][1] <= n:
                    pipe_size = pipe_size_qty_list_of_list[i][0]
                    pipe_qty = pipe_size_qty_list_of_list[i][1]
                    pipe_list.append(pipe_size)
                    qty_list.append(pipe_qty)        
                if pipe_size_qty_list_of_list[i][1] >  n:
                    pipe_size= pipe_size_qty_list_of_list[i][0]
                    pipe_qty= n
                    pipe_list.append(pipe_size)
                    qty_list.append(pipe_qty)
                    break
                n= n - pipe_qty
                t = t + pipe_qty
                i += 1

            if pipe_qty_need !=1 and pipe_size_qty_list_of_list[i][0] > pipe_meter:   
                if pipe_qty_need * pipe_size_qty_list_of_list[i][1] <= n:
                    pipe_size= pipe_size_qty_list_of_list[i][0]
                    pipe_qty= pipe_size_qty_list_of_list[i][1]  
                    pipe_list.append(pipe_size)
                    qty_list.append(pipe_qty)

                if pipe_qty_need * pipe_size_qty_list_of_list[i][1] > n:
                    pipe_size= pipe_size_qty_list_of_list[i][0]
                    pipe_qty=math.ceil(float(n/pipe_qty_need))
                    pipe_list.append(pipe_size)
                    qty_list.append(pipe_qty)
                    break
                n= n - pipe_qty
                t = t + pipe_qty
                i += 1
        raise UserError(_("pipe Test list %s")%(qty_list)) #here is the chosen quantity from the suitable size

im going to compute the quantity of possible length of pipe i can use to build a new object, i had to pick pipes from the stock depends on the size of the new object, for example:

- suppose i need 5 pipe of size "4.3" meter to build a new object:
- what i realy have in the stock: 
    - 1 pipe of size 5
    - 4 pipe of size 4.1
    - 2 pipe of size 4.4 
    - 10 pipe of size 9
  • cutting is possible while assembeling is not.
  • so here i should pick from the available quantity of size >= 4.3 sequentially from the smallest to the largest until the quatity i need is equal to 5 "the number of needed pipe"

  • from the exampe above i have to chose the following pipe:

    • 2 of "4.4"
    • 1 of "5"
    • 1 of "9" (here becaue it's sufficeint to produce 2 of (4.3) pipe)
  • what i actually did is appending pipe's size and quantity in a list of list in the form [[size,qty]] ,sorting and searching on that list. here is the list:

    • [[4.4,2],[5,1],[9,10]]
  • what i should get from that list is the possible size and quantity

    • [[4.4,2],[5,1][9,1]]
  • it's work fine but im looking for optimizing my code. thanks in advance

0 个答案:

没有答案