我的Python不是很好,我想看看我的代码是否有意义,或者我如何改进它:)
所以有一些数据来自API,管道使用Python将这些数据带到Postgres。我没有创建代码,但我被要求用新字段扩展它。
这是一个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
}
]
}
我需要从"hs_lead_status"——比;" version& quot;:["时间戳"(仅限最早的时间戳)]。value == "HANDOVER_TO_ADVOCATE">
这是我创建的代码我不确定它是否有意义,当我在本地运行代码时没有失败但我没有得到响应所以
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'))), '')
我怕把它搞砸了,我在PROD中尝试了一下,因为我们这里没有STAGING,如果你能给我任何关于代码的反馈,我将不胜感激!
这是整个代码的片段,以获得更多上下文:
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))
感谢下面是一段可以测试的代码。(在线编译https://www.online-python.com/online_python_compiler)
请…
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']))