Добавление добавления словаря к списку внутри другого multiprocessing.managers.DictProxy - PullRequest
0 голосов
/ 26 мая 2020

У меня есть словарь multiprocessing.managers.DictProxy. Словарь выглядит так:

manager = multiprocessing.Manager()
data_set = manager.dict()
def write_company(company):

    try:
        entity = company["registration"]["name"]
    except Exception:
        entity = ""
        pass

    try:
        registration_number = company["registration"]["registration_number"]
    except Exception:
        registration_number = ""
        pass

    try:
        fei_number = company["registration"]["fei_number"]
    except Exception:
        fei_number = ""
        pass
    try:
        operator_number = company["registration"]["owner_operator"]["owner_operator_number"]
    except Exception:
        operator_number = ""
        pass
    try:
        iso_country_code = company["registration"]["iso_country_code"]
        country_name = pycountry.countries.get(alpha_2=iso_country_code).name #Retrieves country name
    except Exception:
        country_name = ""
        pass
    try:
        city = company["registration"]["city"]
    except Exception:
        city = ""
        pass

    try:
        zipcode = company["registration"]["postal_code"]
    except Exception:
        zipcode = ""
        pass

    try:
        address = company["registration"]["address_line_1"]
    except Exception:
        address = ""
        pass

    try:
        address2 = company["registration"]["address_line_2"]
    except Exception:
        address2 = ""
        pass
    try:
        state_code = company["registration"]["state_code"]
    except Exception:
        state_code = ""
        pass

    try:
        reg_expiry_date_year = company["registration"]["reg_expiry_date_year"]
        reg_expiry_date_year = reg_expiry_date_year + "/12/31"
    except Exception:
        reg_expiry_date_year = ""
        pass

    try:
        establishment_type = company["establishment_type"][0]
    except Exception:
        establishment_type = ""
        pass

    data_set[(registration_number, entity, operator_number)] = {
        "entity": entity,
        "registration_name": registration_number,
        "operator_number": operator_number,
        "fei_number": fei_number,
        "country_name": country_name,
        "city": city,
        "zipcode": zipcode,
        "address": address,
        "address2": address2,
        "state": state_code,
        "reg_expiry_date_year": reg_expiry_date_year,
        "fda_product": list()
    }

В другой функции я собираю продукты для списка fda_product, а затем добавляю их в список.

def write_products(key, products):
    for product in products:
        #Establishment Type
        establishment_type = ""
        try:
            length = len(product["establishment_type"])
            for i in range(length):
                establishment_type += product["establishment_type"][i] + ";"
        except Exception:
            pass

        product_name = ""
        try:
            len_proprietary = len(product["proprietary_name"])
            for i in range(len_proprietary):
                product_name += product["proprietary_name"][i] + ";"
        except Exception:
            pass

        try:
            pma_number = product["pma_number"]
        except Exception:
            pma_number = ""
            pass

        try:
            k_number = product["k_number"]
        except Exception:
            k_number = ""
            pass
        try:
            product_code = [item['product_code'] for item in product["products"]][0]
        except Exception:
            product_code = ""
            pass

        try:
            created_date = [item['created_date'] for item in product["products"]][0]
        except Exception:
            created_date = ""
            pass
        try:
            device_name = [item["openfda"]['device_name'] for item in product["products"]][0]
        except Exception:
            device_name = ""
            pass

        try:
            exempt = [item["exempt"] for item in product["products"]][0]
        except Exception:
            exempt = ""
            pass

        product_data = {
            "product_name": product_name,
            "product_code": product_code,
            "k_number": k_number,
            "pma_number": pma_number,
            "exempt": exempt,
            "device_name": device_name,
            "created_date": created_date,
            "establishment_type": establishments

        data_set[key]["fda_product"].append(product_data)

Я проверил, чтобы убедиться, что что данные о продукте содержат данные и что ключ правильный. когда я печатаю список, он не добавляется к списку, и в результате получается пустой список. Спасибо за помощь

print(product_data)
{'k_number': u'', 'created_date': u'1991-07-16', 'exempt': u'', 'pma_number': u'', 'establishment_type': u'Develop Specifications But Do Not Manufacture At This Facility;', 'product_code': u'EIY', 'product_name': u'PREMIER PLASTIC FILLIN;', 'device_name': u'Instrument, Filling, Plastic, Dental'}

print(data_set[key]["fda_product"])
[]

1 Ответ

1 голос
/ 26 мая 2020

Я нашел ответ. Похоже, что multiprocessing.managers.DictProxy не может быть обновлен напрямую. Чтобы выполнить sh задачу, вам нужно получить локальную копию словаря. Обновите локальную копию и добавьте ее обратно в DictProxy. Вот как будет выглядеть код функции write_product ()

def write_products(key, products):
    fda_product = []
    for product in products:
        ...
        ...
        ...
        product_data = {
            "product_name": product_name,
            "product_code": product_code,
            "k_number": k_number,
            "pma_number": pma_number,
            "exempt": exempt,
            "device_name": device_name,
            "created_date": created_date,
            "establishment_type": establishment_type
        }

        fda_product.append(product_data)

    local_dict = data_set[key]
    local_dict["fda_product"] = fda_product
    data_set[key] = local_dict
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...