Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Compressed FHIR

A Python library for storing FHIR JSON resources in a compressed form in memory, optimizing memory usage while maintaining fast access to FHIR data.

Overview

Compressed FHIR is a specialized library that provides efficient memory storage for FHIR (Fast Healthcare Interoperability Resources) JSON resources. It uses zlib or msgpack for compression while ensuring quick access to the stored healthcare data.

Features

  • Efficient memory storage of FHIR resources
  • Fast access to compressed FHIR data
  • Compatible with standard FHIR JSON formats
  • Minimal memory footprint

Installation

You can install the package using pip:

pip install compressedfhir

Or using uv:

uv add compressedfhir

Requirements

See pyproject.toml for the supported Python versions and dependencies.

Usage

from typing import Any

from compressedfhir.fhir.fhir_resource import FhirResource
from compressedfhir.utilities.compressed_dict.v1.compressed_dict_storage_mode import CompressedDictStorageMode


resource1 = FhirResource(
    initial_dict={"resourceType": "Observation", "id": "456"},
    storage_mode=CompressedDictStorageMode.default(),
)

my_dict: dict[str, Any] = resource1.dict()  # plain nested dicts (fast)
my_plain_dict: dict[str, Any] = resource1.to_plain_dict()
my_json: str = resource1.json()  # compact orjson text (fast)
my_exact_json: str = resource1.json(byte_identical=True)  # exactly the text 4.0.0 and earlier wrote

with resource1.transaction():
   assert "email" not in resource1
   assert resource1.get("name") == "Custom Mode User"
   assert resource1.get("active") is True

Upgrading to 5.0.0

5.0.0 is several times faster than 4.0.0 because JSON is parsed and written with orjson. It changes the default output of json(), so it is a major version:

Call 4.0.0 5.0.0 default To keep the 4.0.0 text
json() on resources, bundles, bundle entries, resource lists and FhirResourceMap stdlib text (", " separators, \uXXXX escapes, 1e-07) compact orjson text (raw UTF-8, 1e-7) json(byte_identical=True)
FhirClientJsonHelpers.convert_dict_to_fhir_json() the same stdlib text compact orjson text byte_identical=True

dict() already returns plain nested dicts since 4.0.0 and still does.

Details:

  • The JSON is equivalent for any parser.
  • NaN and Infinity cannot be written in FHIR JSON, so dict() and json() now omit them, and compressed storage records them as null when the resource is built (raw_dict() returns None for them). json(byte_identical=True) still writes a NaN held in raw storage as NaN, but a compressed resource no longer has it.
  • A plain Enum or a UUID inside a resource is now accepted and stored as its value or string (it raised TypeError before).
  • convert_dict_to_fhir_json() also writes a plain Enum as its value (it wrote str(enum)), a dataclass as a dict (it wrote its repr), and NaN as null.
  • Integers beyond 64 bits and non-string keys still work and stay exact: they fall back to the standard library.
  • Reading stored data rebuilds only the built-in types (datetime, date, time, Decimal, bytes, set, complex) and decoders you pass through custom_decoders. A custom object stored in a resource (written with its class name and attributes) reads back as its attributes dict in compressed storage, and no module is imported while reading.
  • Stored data: new compressed blobs start with one space, which JSON parsers ignore, so 4.0.0 can still read them, and blobs written by older versions still load.
  • Upgrading from 3.x: 4.0.0 already changed dict() and raw_dict() to plain dicts instead of OrderedDict, removed the OrderedDict-specific helper functions, and made CompressedDict equality ignore key order at every level.

The reasoning and the alternatives are in adrs/0002-faster-json-with-orjson.md.

Development Setup

  1. Clone the repository:

    git clone https://github.com/icanbwell/compressed-fhir.git
    cd compressed-fhir
    
  2. Install dependencies using uv:

    uv sync --group dev
    
  3. Set up pre-commit hooks:

    pre-commit install
    

Docker Support

The project includes Docker support for development and deployment:

# Build the Docker image
docker build -t compressed-fhir .

# Run using docker-compose
docker-compose up

Contributing

Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Authors

Support

For support, please open an issue in the GitHub repository or contact the maintainers.

Project Status

Current status: Beta

The project is under active development. Please check the GitHub repository for the latest updates and changes.

Metadata

Release files for compressedfhir 5.0.0rc1

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

Source distribution (sdist)

Source distribution for compressedfhir 5.0.0rc1
File Size Uploaded Provenance Actions
compressedfhir-5.0.0rc1.tar.gz 66.4 kB Without provenance Details for compressedfhir-5.0.0rc1.tar.gz

Built distribution (wheel)

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

Total release size: 151.5 kB

Release files / compressedfhir-5.0.0rc1.tar.gz

Download URL compressedfhir-5.0.0rc1.tar.gz
Size 66.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c69beeb55780fd43e5e5567334b57146505f0421be174dca75857b4063ae4da1
BLAKE2b-256 checksum
How to use checksums
8d3dde571e39c9a0ce5a77109bd3061d43c21b903eed6bcfe8760e99f5ecb03a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / compressedfhir-5.0.0rc1-py3-none-any.whl

Download URL compressedfhir-5.0.0rc1-py3-none-any.whl
Size 85.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9252123cdb58654f15f1e185e94e4baf3eccaa1eb1aa6fb077fa950fa3528f31
BLAKE2b-256 checksum
How to use checksums
833a06d698963736e0d4e1093e2cfc197b03ef45f0047f87ac20f5f49c332d68
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for compressedfhir 5.0.0rc1

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