Skip to main content

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

Metadata

Release files for fhirpathpy 3.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fhirpathpy 3.0.2
File Size Uploaded Provenance Actions
fhirpathpy-3.0.2.tar.gz 221.5 kB Without provenance Details for fhirpathpy-3.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fhirpathpy 3.0.2
File Interpreter ABI Platform Actions
fhirpathpy-3.0.2-py3-none-any.whl Python 3 none any Details for fhirpathpy-3.0.2-py3-none-any.whl

Total release size: 457.4 kB

Release files / fhirpathpy-3.0.2.tar.gz

Download URL fhirpathpy-3.0.2.tar.gz
Size 221.5 kB
Tags Source
SHA-256 checksum
How to use checksums
194bcce77772efe2e32af275d28a8d3dacb8bd9c961c767e2dae6376de34fb62
BLAKE2b-256 checksum
How to use checksums
a6749fbc7feecd3e94f015c2df40825652e17ef7de5f97ebc74c9549fbaeb1fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / fhirpathpy-3.0.2-py3-none-any.whl

Download URL fhirpathpy-3.0.2-py3-none-any.whl
Size 235.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
311e456e35761168707330904a390dd6daccef0cf3325cce0e5d8c5c5223d287
BLAKE2b-256 checksum
How to use checksums
26ae061c9fc3135be6b001580dc02a2fa1345ad0d3d30d620fc8f22345a239fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for fhirpathpy 3.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page