Skip to main content
 首页 » 编程设计

python之带有 bundle 的 Smart-on-FHIR Python 客户端

2025年04月02日10thcjp

所以我有一个来自“$everything”操作的 FHIR 患者包 json: https://www.hl7.org/fhir/operation-patient-everything.html

我现在有兴趣在 FHIR Python 客户端模型上使用 Smart 来简化 json 文件的处理。给出的示例如下:

import json 
import fhirclient.models.patient as p 
with open('path/to/patient.json', 'r') as h: 
     pjs = json.load(h) 
patient = p.Patient(pjs) 
patient.name[0].given 
# prints patient's given name array in the first `name` property 

是否可以只用一个通用的 bundle 对象类来实例化一些东西,以便能够访问 bundle 中的不同资源?

请您参考如下方法:

是的,您可以像实例化任何其他模型一样实例化一个Bundle,既可以从 JSON 手动实例化,也可以通过从服务器中读取 read 来实例化。每个 search 也会返回一个 Bundle。然后你可以迭代包的条目并使用它们,比如将它们放在一个数组中:

resources = [] 
if bundle.entry is not None: 
    for entry in bundle.entry: 
        resources.append(entry.resource) 

附注 应该可以对客户端执行任何 $operation ,返回你提到的 Bundle ,但我必须检查我们是否已经暴露了它或者它是否没有被暴露 promise 。


命令行示例:

import fhirclient.models.bundle as b 
import json 
with open('fhir-parser/downloads/bundle-example.json', 'r') as h: 
    js = json.load(h) 
bundle = b.Bundle(js) 
bundle.entry 
[<fhirclient.models.bundle.BundleEntry object at 0x10f40ae48>,  
 <fhirclient.models.bundle.BundleEntry object at 0x10f40ac88>] 
for entry in bundle.entry: 
    print(entry.resource) 
 
// prints 
<fhirclient.models.medicationorder.MedicationOrder object at 0x10f407390> 
<fhirclient.models.medication.Medication object at 0x10f407e48>