Home > OS >  Python Question about getting data from a list
Python Question about getting data from a list

Time:10-01

My Python is not very good and I would like to see if my code makes sense or how can I improve it :)

So there is some data coming from an API, the pipeline is bringing this data to Postgres using Python. I didn't create the code but I was asked to extend it with new fields.

This is a sample JSON:

 "hs_lead_status": {
        "value": "Closure - Successful - Re-Employment",
        "versions": [
            {
                "value": "Closure - Successful - Re-Employment",
                "source-type": "CRM_UI",
                "source-id": "userId:10939396",
                "source-label": null,
                "updated-by-user-id": 10939396,
                "timestamp": 1632381167439,
                "selected": false
            },
            {
                "value": "(CH)_Court_Order_Filed_Advocard",
                "source-type": "CRM_UI",
                "source-id": "userId:10939396",
                "source-label": null,
                "updated-by-user-id": 10939396,
                "timestamp": 1616073472239,
                "selected": false
            },
            {
                "value": "HANDOVER_TO_ADVOCATE",
                "source-type": "CRM_UI",
                "source-id": "userId:10939396",
                "source-label": null,
                "updated-by-user-id": 10939396,
                "timestamp": 1616073465338,
                "selected": false
            },
            {
                "value": "(CH)_Court_Order_Filed_Advocard",
                "source-type": "CRM_UI",
                "source-id": "userId:10939396",
                "source-label": null,
                "updated-by-user-id": 10939396,
                "timestamp": 1616073380530,
                "selected": false
            },
            {
                "value": "HANDOVER_TO_ADVOCATE",
                "source-type": "CRM_UI",
                "source-id": "userId:10939396",
                "source-label": null,
                "updated-by-user-id": 10939396,
                "timestamp": 1616073375133,
                "selected": false
            },
            {
                "value": "(CH)_Court_Order_Filed_Advocard",
                "source-type": "CRM_UI",
                "source-id": "userId:10939396",
                "source-label": null,
                "updated-by-user-id": 10939396,
                "timestamp": 1616073225645,
                "selected": false
            },
            {
                "value": "HANDOVER_TO_ADVOCATE",
                "source-type": "CRM_UI",
                "source-id": "userId:10939396",
                "source-label": null,
                "updated-by-user-id": 10939396,
                "timestamp": 1616073219779,
                "selected": false
            },
            {
                "value": "(CH)_Court_Order_Filed_Advocard",
                "source-type": "CRM_UI",
                "source-id": "userId:10939396",
                "source-label": null,
                "updated-by-user-id": 10939396,
                "timestamp": 1609861682503,
                "selected": false
            },
            {
                "value": "HANDOVER_TO_ADVOCATE",
                "source-type": "CRM_UI",
                "source-id": "userId:12769108",
                "source-label": null,
                "updated-by-user-id": 12769108,
                "timestamp": 1608626160658,
                "selected": false
            }
        ]
    }

I need to get the value from "hs_lead_status" --> "Versions":["Timestamp"(only the earliest timestamp)] where the versions.value == "HANDOVER_TO_ADVOCATE"

This is the code I created I'm not sure if it makes sense or not, when I run the code in my local is not failing but I don't get a response back so

hs_lead_info = data['hs_lead_status']

        def get_hs_lead_info(field_name):
            for field in hs_lead_info or []:
                if field['versions']['value'] == "HANDOVER_TO_ADVOCATE":
                    return field.get(((min('timestamp'))), '')

I'm afraid to mess it up and I try it in PROD because we don't have STAGING here, if you can give me any feedback on code will appreciate it!

This is the snippet of entire the code for more context:

def from_dict(cls, vid, data):
    if data is None:
        return ContactsEntity()
    else:
        
        hs_lead_info = data['hs_lead_status']
        def get_hs_lead_info(field_name):
            for field in hs_lead_info or []:
                if field['versions']['value'] == "HANDOVER_TO_ADVOCATE":
                    return field.get(((min('timestamp'))), '')

        return ContactsEntity(vid=vid,
                              produkt=data.get("properties", {})
                              .get("produkt", {}).get("value", None),
                              email=data.get("properties", {})
                              .get("email", {}).get("value", None),
                              salutation=data.get("properties", {})
                              .get("salutation", {}).get("value", None),
                              name="{} {}".format(data.get("properties", {})
                                                  .get("fd_nachname", {}).get("value", ""),
                                                  data.get("properties", {}).get("fd_vorname", {})
                                                  .get("value", "")),
                              verantwortlicher_anwalt=data.get("properties", {})
                              .get("verantwortlicher_anwalt", {}).get("value", None),
                              cc_legalinsurance=data.get("properties", {})
                              .get("cc_legalinsurance", {}).get("value", None),
                              rechtsschutzversicherer=data.get("properties", {})
                              .get("rechtsschutzversicherer", {}).get("value", None),
                              fallkategorie__c=data.get("properties", {})
                              .get("fallkategorie", {}).get("value", None),
                              cc_customer_problem=data.get("properties", {})
                              .get("cc_customer_problem", {}).get("value", None),
                              selbstbeteiligung=data.get("properties", {})
                              .get("selbstbeteiligung", {}).get("value", None),
                              fd_arbeitsbeginn=data.get("properties", {}).get("fd_arbeitsbeginn", {})
                              .get("value", None),
                              cc_empnum=data.get("properties", {})
                              .get("cc_empnum", {}).get("value", None),
                              fd_mandant_anschrift_plz=data.get("properties", {})
                              .get("fd_mandant_anschrift_plz", {}).get("value", None),
                              salesforcecontactid=data.get("properties", {})
                              .get("salesforcecontactid", {}).get("value", None),
                              hs_lead_status=data.get("properties", {})
                              .get("hs_lead_status", {}).get("value" , None),
                              hand_over_advocate_date=get_hs_lead_info("timestamp"),
                              lawcus_uuid=data.get("properties", {})
                              .get("lawcus_uuid", {}).get("value", None),
                              closure_date=data.get("properties", {})
                              .get("closure_date", {}).get("value", None),
                              local_counsel_email=data.get("properties", {})
                              .get("local_counsel_email", {}).get("value", None),
                              local_counsel_name="{} {}".format(data.get("properties", {})
                                                  .get("terminsvertreter_nachname", {}).get("value", ""),
                                                  data.get("properties", {}).get("terminsvertreter_vorname", {})
                                                  .get("value", "")),
                              compensation_amount_final=data.get("properties", {})
                              .get("abfindungssumme_final", {}).get("value", None),
                              createdate=data.get("properties", {})
                              .get("createdate", {}).get("value", None),
                              mandant_familienstand=data.get("properties", {})
                              .get("mandant_familienstand", {}).get("value", None),
                              closure_verfahrensschritt=data.get("properties", {})
                              .get("closure_verfahrensschritt", {}).get("value", None),
                              sonderkuendigungsschutz=data.get("properties", {})
                              .get("sonderkuendigungsschutz", {}).get("value", None),
                              tarifvertrag=data.get("properties", {})
                              .get("tarifvertrag", {}).get("value", None),
                              fd_betriebsrat=data.get("properties", {})
                              .get("fd_betriebsrat", {}).get("value", None),
                              closure_type=data.get("properties", {})
                              .get("closure_type", {}).get("value", None),
                              mandant_geburtsdatum=data.get("properties", {})
                              .get("mandant_geburtsdatum", {}).get("value", None),
                              fd_bruttomonatsgehalt_docs=data.get("properties", {})
                              .get("fd_bruttomonatsgehalt_docs", {}).get("value", None))

Thanks

CodePudding user response:

Below is a piece of code that you can test. (online compiler https://www.online-python.com/online_python_compiler)

Please... do not put into PROD without testing before ...

import json

data = """
{"hs_lead_status": {
        "value": "Closure - Successful - Re-Employment",
        "versions": [
            {
                "value": "HANDOVER_TO_ADVOCATE",
                "source-type": "CRM_UI",
                "source-id": "userId:10939396",
                "source-label": null,
                "updated-by-user-id": 10939396,
                "timestamp": 1032381167430,
                "selected": false
            },
            {
                "value": "HANDOVER_TO_ADVOCATE",
                "source-type": "CRM_UI",
                "source-id": "userId:10939396",
                "source-label": null,
                "updated-by-user-id": 10939396,
                "timestamp": 1616073472239,
                "selected": false
            }
        ]
    }
}
"""

def get_hs_lead_info(datas):
    min = 9999999999999
    for field in datas['versions'] or []:
        if field['value'] == 'HANDOVER_TO_ADVOCATE':
            if field['timestamp'] < min:
                min = field['timestamp']
    return min

info = json.loads(data) 
print(get_hs_lead_info(info['hs_lead_status']))
  • Related