How to add dictionary (list object) to dictionary object in

A forum for general discussion of the Python programming language.

How to add dictionary (list object) to dictionary object in

Postby priyanka_hdp » Wed May 08, 2013 9:26 am

here is my error when i going to do that in my code

Code: Select all
    TypeError: unhashable type: 'list'


Here is my python dictionary list object.

Code: Select all
    [
    {'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 1, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
     'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'tea'},
    {'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 2, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
    'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'tea'},
     'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'rubber'}
    ]


And i need to change it to this type dictionary object.because i need to create records in openerp with some conditions.

below show my required output

Code: Select all
    {'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 1, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
     'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'tea'},
    {'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 2, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
    'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'tea'},
    {'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 3, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
    'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'rubber'},
    {'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 4, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
     'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'rubber'}



please help me to sort this issue.

here shows my python code that i used to create above **1st result**
Code: Select all
    def temp_posting(self, cr, uid, ids, context=None):
        tea_v = {}
        tea_list_data = []
       
        if context is None:
            context = {}
        bpl_division_id = context['bpl_division_id']
        work_update_ids = self.pool.get('bpl.work.update').search(cr, uid, [('bpl_division_id', '=', bpl_division_id)])
        work_update_obj = self.pool.get('bpl.work.update').browse(cr, uid, work_update_ids)[0]
        if work_update_obj:
            work_update_obj.write({'state': 'negotiation'})       
       
        for record in work_update_obj.selected_tea_workers_update_line_ids:
            tea_list_data.append({'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'tea', 'names': 6.00, 'output':record.tea_total_kgs, 'over_kgs':2, 'scrap':3, 'pss':30.00})
        for record in work_update_obj.selected_rubber_workers_update_line_ids:
            tea_list_data.append({'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'rubber', 'names': 6.00, 'output':record.rubber_total_kgs, 'over_kgs':2, 'scrap':3, 'pss':30.00})   
        for record in work_update_obj.selected_sundry_workers_update_line_ids:
            tea_list_data.append({'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'sundry', 'names':record.sundry_hrs_worked, })   
        for record in work_update_obj.selected_other_workers_update_line_ids:
            tea_list_data.append({'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, })
        tea_v = {tea_list_data}
       
        daily_transaction_master = self.pool.get('bpl.daily.transaction.master')
        daily_transaction_master.create(cr, uid, tea_v, context=context)
        return True
priyanka_hdp
 
Posts: 5
Joined: Thu Mar 28, 2013 10:35 am

Re: How to add dictionary (list object) to dictionary object

Postby Mekire » Wed May 08, 2013 10:04 am

Currently you don't appear to be doing what you are trying to do (which I'm not sure what is to be honest). You are at the moment creating a list that is comprised of a series of dictionaries. Then you are taking this list and appear to be trying to place it in a set. Which the interpreter of course objects to as lists and dictionaries are unhashable (cannot go in sets). What is the final format that you want your list of dictionaries to look like?

You already have a list of these dictionaries. Is the final desired output a single dictionary with all this data? a tuple? the list you already have?

-Mek
User avatar
Mekire
 
Posts: 1021
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: How to add dictionary (list object) to dictionary object

Postby priyanka_hdp » Wed May 08, 2013 10:27 am

thanks
now its sorted with below code

Code: Select all
def temp_posting(self, cr, uid, ids, context=None):
    tea_v = {}
    rubber_v = {}
    sundry_v = {}
    other_v = {}

    if context is None:
        context = {}
    bpl_division_id = context['bpl_division_id']
    work_update_ids = self.pool.get('bpl.work.update').search(cr, uid, [('bpl_division_id', '=', bpl_division_id)])
    work_update_obj = self.pool.get('bpl.work.update').browse(cr, uid, work_update_ids)[0]
    if work_update_obj:
        work_update_obj.write({'state': 'negotiation'})       

    daily_transaction_master = self.pool.get('bpl.daily.transaction.master')

    for record in work_update_obj.selected_tea_workers_update_line_ids:
        tea_v = {'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'tea', 'names': 6.00, 'output':record.tea_total_kgs, 'over_kgs':2, 'scrap':3, 'pss':30.00}
        daily_transaction_master.create(cr, uid, tea_v, context=context)
    for record in work_update_obj.selected_rubber_workers_update_line_ids:
        rubber_v = {'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'rubber', 'names': 6.00, 'output':record.rubber_total_kgs, 'over_kgs':2, 'scrap':3, 'pss':30.00}   
        daily_transaction_master.create(cr, uid, rubber_v, context=context)
    for record in work_update_obj.selected_sundry_workers_update_line_ids:
        sundry_v = {'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'sundry', 'names':record.sundry_hrs_worked, }   
        daily_transaction_master.create(cr, uid, sundry_v, context=context)
    for record in work_update_obj.selected_other_workers_update_line_ids:
        other_v = {'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, }
        daily_transaction_master.create(cr, uid, other_v, context=context)
    return True 
priyanka_hdp
 
Posts: 5
Joined: Thu Mar 28, 2013 10:35 am


Return to General Discussions

Who is online

Users browsing this forum: hustlinghare and 3 guests