Skip to main content

UKRDC-SQLA

SQLAlchemy models for the UKRDC and related databases.

Installation

pip install ukrdc-sqla poetry add ukrdc-sqla

Example Usage

from datetime import datetime

from ukrdc_sqla.ukrdc import LabOrder, PatientNumber, PatientRecord, ResultItem


def commit_extra_resultitem(session):
    patient_record = PatientRecord(
        pid="PYTEST01:LABORDERS:00000000L",
        sendingfacility="PATIENT_RECORD_SENDING_FACILITY_1",
        sendingextract="PV",
        localpatientid="00000000L",
        ukrdcid="000000001",
        repository_update_date=datetime(2020, 3, 16),
        repository_creation_date=datetime(2020, 3, 16),
    )
    patient_number = PatientNumber(
        id=2,
        pid="PYTEST01:LABORDERS:00000000L",
        patientid="111111111",
        organization="NHS",
        numbertype="NI",
    )
    laborder = LabOrder(
        id="LABORDER_TEST2_1",
        pid="PYTEST01:LABORDERS:00000000L",
        external_id="EXTERNAL_ID_TEST2_1",
        order_category="ORDER_CATEGORY_TEST2_1",
        specimen_collected_time=datetime(2020, 3, 16),
    )
    resultitem = ResultItem(
        id="RESULTITEM_TEST2_1",
        order_id="LABORDER_TEST2_1",
        service_id_std="SERVICE_ID_STD_TEST2_1",
        service_id="SERVICE_ID_TEST2_1",
        service_id_description="SERVICE_ID_DESCRIPTION_TEST2_1",
        value="VALUE_TEST2_1",
        value_units="VALUE_UNITS_TEST2_1",
        observation_time=datetime(2020, 3, 16),
    )

    session.add(patient_record)
    session.add(patient_number)
    session.add(laborder)
    session.add(resultitem)

    session.commit()

Developer Notes

Computed Fields

Three field types for deriving values on SQLAlchemy models. All are defined inline on the model and delegate their logic to functions in post_calculations.py.

computed_field

Pure Python. No session required. Cached permanently on the instance — survives expire() and commit().

Use when the value is derivable from already-loaded columns only (arithmetic, date calculations, string formatting).

session_computed_field

Session-aware. Cache is cleared on expire() and commit() so DB-derived values are never stale.

Use when the calculation requires a DB query — joins, lookups against reference tables, aggregates over related rows.

computed_hybrid

Wraps hybrid_property. Not cached — recalculates on every access. The only type usable in queries (WHERE, ORDER BY, filter()).

Use when you need the value available at the query level, not just on loaded instances.

Quick Reference

computed_field session_computed_field computed_hybrid
Requires session No Yes No
Cached Yes — permanent Yes — cleared on expiry No
Usable in queries No No Yes
Use for Arithmetic, dates Joins, DB lookups Query-level filtering

Example

# post_calculations.py


def _calc_age(self) -> Optional[int]:
    if self.birthtime is None:
        return None
    today = date.today()
    born = self.birthtime.date()
    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))


def _calc_prepost(self, session: Session) -> str:
    # looks up sibling results on the same date to determine PRE/POST dialysis
    ...


def _numeric_value_py(self) -> Optional[float]:
    return float(self.resultvalue) if self.resultvalue else None


def _numeric_value_expr(cls):
    return cast(cls.resultvalue, Numeric)
# ukrdc.py


class ResultItem(Base):
    ...

    # No DB query needed — birthtime is already loaded
    age: int = computed_field(_calc_age)

    # Queries sibling rows — cache cleared on expire/commit
    prepost: str = session_computed_field(_calc_prepost)

    # Usable in WHERE/ORDER BY across a patient cohort
    numeric_value: float = computed_hybrid(_numeric_value_py, _numeric_value_expr)
# usage

record = session.get(ResultItem, "R1")
print(record.age)  # 47  — from loaded columns, no query
print(record.prepost)  # "PRE" — queried DB, cached until next commit

# computed_hybrid can be used in queries — the other two cannot
high_results = session.scalars(
    select(ResultItem)
    .where(ResultItem.numeric_value > 10.0)
    .order_by(ResultItem.numeric_value.desc())
).all()

Publish Updates

  • Iterate the version number (poetry version major/minor/patch)
  • Push to GitHub repo
  • Create a GitHub release
    • GitHub Actions will automatically publish the release to PyPI

Download files

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

Source Distribution

ukrdc_sqla-4.6.1.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

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

ukrdc_sqla-4.6.1-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file ukrdc_sqla-4.6.1.tar.gz.

File metadata

  • Download URL: ukrdc_sqla-4.6.1.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.2 CPython/3.10.21 Linux/6.17.0-1022-azure

File hashes

Hashes for ukrdc_sqla-4.6.1.tar.gz
Algorithm Hash digest
SHA256 08da4a89c9dfcbe7a5292c6e805dcb8604125cf65eb999de99e69a4cff404829
MD5 bc1beb9262918f0041a9eab1f5a41e23
BLAKE2b-256 f39f3d4fd9407b63f46c9a134973bbfdaaa99a2a438ec8cb531605d734bf0f46

See more details on using hashes here.

File details

Details for the file ukrdc_sqla-4.6.1-py3-none-any.whl.

File metadata

  • Download URL: ukrdc_sqla-4.6.1-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.2 CPython/3.10.21 Linux/6.17.0-1022-azure

File hashes

Hashes for ukrdc_sqla-4.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 92b9cae45c45294c6e370005ff717fbedf614653288f8e65e4c0a5b695317209
MD5 8b50969e35c6d48b448f49b9dadbba2b
BLAKE2b-256 16add0f1035319ea3410172f9744848cbfa0bb770649fdc0de8299b71140edef

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

4.6.1 This release

2 files

4.3.1

2 files

4.3.0

2 files

4.2.2

2 files

4.2.1

2 files

4.2.0

2 files

4.1.0

2 files

4.0.0

2 files

3.1.0

2 files

3.0.3

2 files

3.0.2

2 files

3.0.1

2 files

3.0.0

2 files

2.9.2

2 files

2.9.1

2 files

2.9.0

2 files

2.8.1

2 files

2.8.0

2 files

2.7.1

2 files

2.7.0

2 files

2.6.5

2 files

2.6.4

2 files

2.6.3

2 files

2.6.2

2 files

2.6.1

2 files

2.6.0

2 files

2.5.1

2 files

2.5.0

2 files

2.4.0

2 files

2.3.0

2 files

2.2.1

2 files

2.2.0

2 files

2.1.0

2 files

2.0.0

2 files

1.13.0

2 files

1.12.3

2 files

1.12.2

2 files

1.12.1

2 files

1.11.0

2 files

1.10.2

2 files

1.10.1

2 files

1.10.0

2 files

1.9.2

2 files

1.9.1

2 files

1.9.0

2 files

1.8.2

2 files

1.8.1

2 files

1.8.0

2 files

1.7.0

2 files

1.6.0

2 files

1.5.0

2 files

1.4.0

2 files

1.3.0

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.1

2 files

1.1.0

2 files

1.0.1

2 files

1.0.0

2 files

0.1.0

2 files

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