Skip to content

FHIRPath Mixin

FHIRPath mixin components

FHIRPathMixin

Path: fhircraft.fhir.path.mixin.FHIRPathMixin

Enhanced mixin class to incorporate a comprehensive FHIRPath interface to the child class.

This mixin provides convenient methods for working with FHIRPath expressions directly on FHIR resource instances, leveraging the enhanced FHIRPath interface.

Methods:

Name Description
fhirpath_values

Evaluates a FHIRPath expression and returns all matching values as a list.

fhirpath_single

Evaluates a FHIRPath expression and returns exactly one value.

fhirpath_first

Evaluates a FHIRPath expression and returns the first matching value.

fhirpath_last

Evaluates a FHIRPath expression and returns the last matching value.

fhirpath_exists

Checks if a FHIRPath expression matches any values.

fhirpath_is_empty

Checks if a FHIRPath expression matches no values.

fhirpath_count

Returns the number of values that match a FHIRPath expression.

fhirpath_update_values

Evaluates a FHIRPath expression and sets all matching locations to the given value.

fhirpath_update_single

Evaluates a FHIRPath expression and sets exactly one matching location to the given value.

Attributes:

Name Type Description
fhirpath FhirPathParser

Initialized FHIRPath engine instance

fhirpath property

fhirpath: FhirPathParser

Initialized FHIRPath engine instance

fhirpath_values

fhirpath_values(expression: str, environment: dict | None = None) -> List[Any]

Evaluates a FHIRPath expression and returns all matching values as a list.

Parameters:

Name Type Description Default
expression str

FHIRPath expression to evaluate

required

Returns:

Name Type Description
List[Any]

List[Any]: A list of all values that match the FHIRPath expression. Returns an empty list if no matches are found.

environment dict | None

Optional environment variables for evaluation

Source code in fhircraft/fhir/path/mixin.py
def fhirpath_values(
    self, expression: str, environment: dict | None = None
) -> List[Any]:
    """
    Evaluates a FHIRPath expression and returns all matching values as a list.

    Args:
        expression (str): FHIRPath expression to evaluate

    Returns:
        List[Any]: A list of all values that match the FHIRPath expression.
                  Returns an empty list if no matches are found.
        environment (dict | None): Optional environment variables for evaluation
    """
    return self.fhirpath.parse(expression).values(
        self,
        environment={
            **self._generate_fhirpath_environment(),
            **(environment or {}),
        },
    )

fhirpath_single

fhirpath_single(expression: str, default: Any = None, environment: dict | None = None) -> Any

Evaluates a FHIRPath expression and returns exactly one value.

Parameters:

Name Type Description Default
expression str

FHIRPath expression to evaluate

required
default Any

The default value to return if no matches are found

None
environment dict | None

Optional environment variables for evaluation

None

Returns:

Name Type Description
Any Any

The single matching value

Raises:

Type Description
FHIRPathRuntimeError

If more than one value is found

Source code in fhircraft/fhir/path/mixin.py
def fhirpath_single(
    self, expression: str, default: Any = None, environment: dict | None = None
) -> Any:
    """
    Evaluates a FHIRPath expression and returns exactly one value.

    Args:
        expression (str): FHIRPath expression to evaluate
        default: The default value to return if no matches are found
        environment (dict | None): Optional environment variables for evaluation

    Returns:
        Any: The single matching value

    Raises:
        FHIRPathRuntimeError: If more than one value is found
    """
    return self.fhirpath.parse(expression).single(
        self,
        default=default,
        environment={
            **self._generate_fhirpath_environment(),
            **(environment or {}),
        },
    )

fhirpath_first

fhirpath_first(expression: str, default: Any = None, environment: dict | None = None) -> Any

Evaluates a FHIRPath expression and returns the first matching value.

Parameters:

Name Type Description Default
expression str

FHIRPath expression to evaluate

required
default Any

The default value to return if no matches are found

None
environment dict | None

Optional environment variables for evaluation

None

Returns:

Name Type Description
Any Any

The first matching value, or the default if no matches

Source code in fhircraft/fhir/path/mixin.py
def fhirpath_first(
    self, expression: str, default: Any = None, environment: dict | None = None
) -> Any:
    """
    Evaluates a FHIRPath expression and returns the first matching value.

    Args:
        expression (str): FHIRPath expression to evaluate
        default: The default value to return if no matches are found
        environment (dict | None): Optional environment variables for evaluation

    Returns:
        Any: The first matching value, or the default if no matches
    """
    return self.fhirpath.parse(expression).first(
        self,
        default=default,
        environment={
            **self._generate_fhirpath_environment(),
            **(environment or {}),
        },
    )

fhirpath_last

fhirpath_last(expression: str, default: Any = None, environment: dict | None = None) -> Any

Evaluates a FHIRPath expression and returns the last matching value.

Parameters:

Name Type Description Default
expression str

FHIRPath expression to evaluate

required
default Any

The default value to return if no matches are found

None
environment dict | None

Optional environment variables for evaluation

None

Returns:

Name Type Description
Any Any

The last matching value, or the default if no matches

Source code in fhircraft/fhir/path/mixin.py
def fhirpath_last(
    self, expression: str, default: Any = None, environment: dict | None = None
) -> Any:
    """
    Evaluates a FHIRPath expression and returns the last matching value.

    Args:
        expression (str): FHIRPath expression to evaluate
        default: The default value to return if no matches are found
        environment (dict | None): Optional environment variables for evaluation

    Returns:
        Any: The last matching value, or the default if no matches
    """
    return self.fhirpath.parse(expression).last(
        self,
        default=default,
        environment={
            **self._generate_fhirpath_environment(),
            **(environment or {}),
        },
    )

fhirpath_exists

fhirpath_exists(expression: str, environment: dict | None = None) -> bool

Checks if a FHIRPath expression matches any values.

Parameters:

Name Type Description Default
expression str

FHIRPath expression to evaluate

required
environment dict | None

Optional environment variables for evaluation

None

Returns:

Name Type Description
bool bool

True if at least one value matches, False otherwise

Source code in fhircraft/fhir/path/mixin.py
def fhirpath_exists(self, expression: str, environment: dict | None = None) -> bool:
    """
    Checks if a FHIRPath expression matches any values.

    Args:
        expression (str): FHIRPath expression to evaluate
        environment (dict | None): Optional environment variables for evaluation

    Returns:
        bool: True if at least one value matches, False otherwise
    """
    return self.fhirpath.parse(expression).exists(
        self,
        environment={
            **self._generate_fhirpath_environment(),
            **(environment or {}),
        },
    )

fhirpath_is_empty

fhirpath_is_empty(expression: str, environment: dict | None = None) -> bool

Checks if a FHIRPath expression matches no values.

Parameters:

Name Type Description Default
expression str

FHIRPath expression to evaluate

required
environment dict | None

Optional environment variables for evaluation

None

Returns:

Name Type Description
bool bool

True if no values match, False otherwise

Source code in fhircraft/fhir/path/mixin.py
def fhirpath_is_empty(
    self, expression: str, environment: dict | None = None
) -> bool:
    """
    Checks if a FHIRPath expression matches no values.

    Args:
        expression (str): FHIRPath expression to evaluate
        environment (dict | None): Optional environment variables for evaluation

    Returns:
        bool: True if no values match, False otherwise
    """
    return self.fhirpath.parse(expression).is_empty(
        self,
        environment={
            **self._generate_fhirpath_environment(),
            **(environment or {}),
        },
    )

fhirpath_count

fhirpath_count(expression: str, environment: dict | None = None) -> int

Returns the number of values that match a FHIRPath expression.

Parameters:

Name Type Description Default
expression str

FHIRPath expression to evaluate

required
environment dict | None

Optional environment variables for evaluation

None

Returns:

Name Type Description
int int

The number of matching values

Source code in fhircraft/fhir/path/mixin.py
def fhirpath_count(self, expression: str, environment: dict | None = None) -> int:
    """
    Returns the number of values that match a FHIRPath expression.

    Args:
        expression (str): FHIRPath expression to evaluate
        environment (dict | None): Optional environment variables for evaluation

    Returns:
        int: The number of matching values
    """
    return self.fhirpath.parse(expression).count(
        self,
        environment={
            **self._generate_fhirpath_environment(),
            **(environment or {}),
        },
    )

fhirpath_update_values

fhirpath_update_values(expression: str, value: Any, environment: dict | None = None) -> None

Evaluates a FHIRPath expression and sets all matching locations to the given value.

Parameters:

Name Type Description Default
expression str

FHIRPath expression to evaluate

required
value Any

The value to set at all matching locations

required
environment dict | None

Optional environment variables for evaluation

None

Raises:

Type Description
RuntimeError

If no matching locations are found or if locations cannot be set

Source code in fhircraft/fhir/path/mixin.py
def fhirpath_update_values(
    self, expression: str, value: Any, environment: dict | None = None
) -> None:
    """
    Evaluates a FHIRPath expression and sets all matching locations to the given value.

    Args:
        expression (str): FHIRPath expression to evaluate
        value (Any): The value to set at all matching locations
        environment (dict | None): Optional environment variables for evaluation

    Raises:
        RuntimeError: If no matching locations are found or if locations cannot be set
    """
    self.fhirpath.parse(expression).update_values(
        self,
        value,
        environment={
            **self._generate_fhirpath_environment(),
            **(environment or {}),
        },
    )

fhirpath_update_single

fhirpath_update_single(expression: str, value: Any, environment: dict | None = None) -> None

Evaluates a FHIRPath expression and sets exactly one matching location to the given value.

Parameters:

Name Type Description Default
expression str

FHIRPath expression to evaluate

required
value Any

The value to set at the matching location

required
environment dict | None

Optional environment variables for evaluation

None

Raises:

Type Description
FHIRPathError

If zero or more than one matching locations are found

RuntimeError

If the location cannot be set

Source code in fhircraft/fhir/path/mixin.py
def fhirpath_update_single(
    self, expression: str, value: Any, environment: dict | None = None
) -> None:
    """
    Evaluates a FHIRPath expression and sets exactly one matching location to the given value.

    Args:
        expression (str): FHIRPath expression to evaluate
        value (Any): The value to set at the matching location
        environment (dict | None): Optional environment variables for evaluation

    Raises:
        FHIRPathError: If zero or more than one matching locations are found
        RuntimeError: If the location cannot be set
    """
    self.fhirpath.parse(expression).update_single(
        self,
        value,
        environment={
            **self._generate_fhirpath_environment(),
            **(environment or {}),
        },
    )