Skip to content

Instantly share code, notes, and snippets.

@ir4y
Last active March 8, 2024 00:15
Show Gist options
  • Save ir4y/2d96e269b336ffab71a394552293b5ac to your computer and use it in GitHub Desktop.
Save ir4y/2d96e269b336ffab71a394552293b5ac to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "af5731eb-3173-46bc-9507-9fb9d3daf138",
"metadata": {},
"outputs": [],
"source": [
"from fhirpathpy import evaluate as fhirpath"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0041cabd-d980-4669-affc-c8746eb6d86d",
"metadata": {},
"outputs": [],
"source": [
"patient = {\n",
" \"resourceType\": \"Patient\",\n",
" \"address\": [{}],\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "fd40aace-9970-4a50-841f-e72e9da2fcaf",
"metadata": {},
"outputs": [],
"source": [
"address_nodes = fhirpath(patient,\"Patient.address.first()\")\n",
"address_node = address_nodes[0]\n",
"#FHIRPathpy returns elements of the original object, so you can mutate them to update the original resource\n",
"address_node[\"postalCode\"] = \"9999\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7a4ae1df-9a02-4a51-af8e-1d92bb0e159b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'resourceType': 'Patient', 'address': [{'postalCode': '9999'}]}\n"
]
}
],
"source": [
"print(patient)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "720558b9-569f-44eb-8fe1-0fab851fedf9",
"metadata": {},
"outputs": [],
"source": [
"# For more convenient results you can create the resource on the fly\n",
"# But in this case you will need to create/generate a lazy model structure for FHIR resources\n",
"\n",
"class defaultlist(list):\n",
" def __init__(self, fx):\n",
" self._fx = fx\n",
" self.append(fx())\n",
" def _fill(self, index):\n",
" while len(self) <= index:\n",
" self.append(self._fx())\n",
" def __setitem__(self, index, value):\n",
" self._fill(index)\n",
" list.__setitem__(self, index, value)\n",
" def __getitem__(self, index):\n",
" self._fill(index)\n",
" return list.__getitem__(self, index)\n",
"\n",
"class PatientFactory(dict):\n",
" primitive = [\"_address\"]\n",
" list_keys = [\"address\", \"name\"]\n",
" \n",
" def __init__(self):\n",
" self.resource = {\n",
" \"resourceType\": \"Patient\"\n",
" }\n",
" def __getitem__(self, key):\n",
" return self.get(key)\n",
" \n",
" def get(self, key): \n",
" if key in self.primitive:\n",
" return None\n",
" if key not in self.resource:\n",
" if key in self.list_keys:\n",
" self.resource[key] = defaultlist(dict)\n",
" else:\n",
" self.resource[key] = {}\n",
" \n",
" return self.resource[key] "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "45c80a03-9661-4161-a7d8-16663469ee5c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'resourceType': 'Patient'}\n"
]
}
],
"source": [
"patient = PatientFactory()\n",
"print(patient.resource)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "3b888249-ef33-4db2-ad7b-805a7cfa5162",
"metadata": {},
"outputs": [],
"source": [
"address_nodes = fhirpath(patient,\"Patient.address.first()\")\n",
"address_node = address_nodes[0]\n",
"#FHIRPathpy returns elements of the original object, so you can mutate them to update the original resource\n",
"address_node[\"postalCode\"] = \"9999\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "767400e0-26aa-40d3-9292-63871438986f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'resourceType': 'Patient', 'address': [{'postalCode': '9999'}]}\n"
]
}
],
"source": [
"print(patient.resource)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment