Skip to main content

FHIRPath implementation in Python

Project description

fhirpath.py

Build Status codecov pypi Supported Python version

FHIRPath implementation in Python

Parser was generated with antlr4

Getting started

Install

pip install fhirpathpy

Usage

from fhirpathpy import evaluate

patient = {
  "resourceType": "Patient",
  "id": "example",
  "name": [
    {
      "use": "official",
      "given": [
        "Peter",
        "James"
      ],
      "family": "Chalmers"
    },
    {
      "use": "usual",
      "given": [
        "Jim"
      ]
    },
    {
      "use": "maiden",
      "given": [
        "Peter",
        "James"
      ],
      "family": "Windsor",
      "period": {
        "end": "2002"
      }
    }
  ]
}

# Evaluating FHIRPath
result = evaluate(patient, "Patient.name.where(use='usual').given.first()", {})
# result: `['Jim']`

evaluate

Evaluates the "path" FHIRPath expression on the given resource, using data from "context" for variables mentioned in the "path" expression.

Parameters

resource (dict|list): FHIR resource, bundle as js object or array of resources This resource will be modified by this function to add type information.

path (string): fhirpath expression, sample 'Patient.name.given'

context (dict): a hash of variable name/value pairs.

model (dict): The "model" data object specific to a domain, e.g. R4. See Using data models documentation below.

options (dict) - Custom options.

options.userInvocationTable - a user invocation table used to replace any existing functions or define new ones. See User-defined functions documentation below.

compile

Returns a function that takes a resource and an optional context hash (see "evaluate"), and returns the result of evaluating the given FHIRPath expression on that resource. The advantage of this function over "evaluate" is that if you have multiple resources, the given FHIRPath expression will only be parsed once

Parameters

path (string) - the FHIRPath expression to be parsed.

model (dict) - The "model" data object specific to a domain, e.g. R4. See Using data models documentation below.

options (dict) - Custom options

options.userInvocationTable - a user invocation table used to replace any existing functions or define new ones. See User-defined functions documentation below.

Using data models

The fhirpathpy library comes with pre-defined data models for FHIR versions DSTU2, STU3, R4, and R5. These models can be used within your project.

Example:

from fhirpathpy.models import models


r4_model = models["r4"]

patient = {
  "resourceType": "Patient",
  "deceasedBoolean": false,
}

result = evaluate(patient, "Patient.deceased", {}, r4_model)

# result: `[False]`

User-defined functions

The FHIRPath specification includes a set of built-in functions. However, if you need to extend the functionality by adding custom logic, you can define your own functions by providing a table of user-defined functions.

Example:

user_invocation_table = {
    "pow": {
        "fn": lambda inputs, exp=2: [i**exp for i in inputs],
        "arity": {0: [], 1: ["Integer"]},
    }
}

result = evaluate(
    {"a": [5, 6, 7]},
    "a.pow()",
    options={"userInvocationTable": user_invocation_table},
)

# result: `[25, 36, 49]`

It works similarly to fhirpath.js

Development

To activate git pre-commit hook: autohooks activate

To run tests: pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fhirpathpy-2.2.2.tar.gz (218.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fhirpathpy-2.2.2-py3-none-any.whl (232.6 kB view details)

Uploaded Python 3

File details

Details for the file fhirpathpy-2.2.2.tar.gz.

File metadata

  • Download URL: fhirpathpy-2.2.2.tar.gz
  • Upload date:
  • Size: 218.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fhirpathpy-2.2.2.tar.gz
Algorithm Hash digest
SHA256 740cd8e8a49ef1d0c22a5581457bc9a630c64a133f4a113a8078a6db6fa83b01
MD5 6e5189eb32c435967d4052e577f1c5ff
BLAKE2b-256 76ac7f4df3f91f73a99a8369cbe471e2a4edbc4437980824b990f37f6214976d

See more details on using hashes here.

File details

Details for the file fhirpathpy-2.2.2-py3-none-any.whl.

File metadata

  • Download URL: fhirpathpy-2.2.2-py3-none-any.whl
  • Upload date:
  • Size: 232.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fhirpathpy-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 45d1bda264b0c9ef02d5f7f58d98d08d1896854c74c62cdbf7751d6bc30fe182
MD5 d66a080c2408c3230c1f760ddbee4577
BLAKE2b-256 9b83f0c605b360c04f050c4e5bd89edbf29941f7c62893f4e368ab095b7bde0a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page