MCPcopy Index your code
hub / github.com/beda-software/fhirpath-py

github.com/beda-software/fhirpath-py @v2.2.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.2.3 ↗ · + Follow
686 symbols 1,457 edges 38 files 16 documented · 2%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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

Core symbols most depended-on inside this repo

evaluate
called by 35
fhirpathpy/__init__.py
ensure_string_singleton
called by 14
fhirpathpy/engine/invocations/strings.py
is_empty
called by 13
fhirpathpy/engine/invocations/math.py
ensure_number_singleton
called by 13
fhirpathpy/engine/invocations/math.py
create_node
called by 9
fhirpathpy/engine/nodes.py
compare
called by 8
fhirpathpy/engine/nodes.py
_calculatePrecision
called by 7
fhirpathpy/engine/nodes.py
parse
called by 6
fhirpathpy/parser/__init__.py

Shape

Method 372
Function 238
Class 72
Route 4

Languages

Python100%

Modules by API surface

fhirpathpy/parser/generated/FHIRPathParser.py254 symbols
fhirpathpy/parser/generated/FHIRPathListener.py83 symbols
fhirpathpy/engine/nodes.py64 symbols
fhirpathpy/engine/evaluators/__init__.py29 symbols
tests/test_evaluators.py25 symbols
fhirpathpy/engine/invocations/math.py19 symbols
fhirpathpy/engine/invocations/equality.py19 symbols
fhirpathpy/engine/util.py18 symbols
fhirpathpy/engine/invocations/strings.py18 symbols
tests/test_helpers.py15 symbols
fhirpathpy/engine/invocations/misc.py15 symbols
fhirpathpy/engine/invocations/existence.py15 symbols

For agents

$ claude mcp add fhirpath-py \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact