File: accessor.py

package info (click to toggle)
python-hl7 0.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 508 kB
  • sloc: python: 3,833; makefile: 160
file content (109 lines) | stat: -rw-r--r-- 2,950 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
from collections import namedtuple


class Accessor(
    namedtuple(
        "Accessor",
        [
            "segment",
            "segment_num",
            "field_num",
            "repeat_num",
            "component_num",
            "subcomponent_num",
        ],
    )
):
    __slots__ = ()

    def __new__(
        cls,
        segment,
        segment_num=1,
        field_num=None,
        repeat_num=None,
        component_num=None,
        subcomponent_num=None,
    ):
        """Create a new instance of Accessor for *segment*. Index numbers start from 1."""
        return super(Accessor, cls).__new__(
            cls,
            segment,
            segment_num,
            field_num,
            repeat_num,
            component_num,
            subcomponent_num,
        )

    @property
    def key(self):
        """Return the string accessor key that represents this instance"""
        seg = (
            self.segment
            if self.segment_num == 1
            else self.segment + str(self.segment_num)
        )
        return ".".join(
            str(f)
            for f in [
                seg,
                self.field_num,
                self.repeat_num,
                self.component_num,
                self.subcomponent_num,
            ]
            if f is not None
        )

    def __str__(self):
        return self.key

    @classmethod
    def parse_key(cls, key):
        """Create an Accessor by parsing an accessor key.

        The key is defined as:

            |   SEG[n]-Fn-Rn-Cn-Sn
            |       F   Field
            |       R   Repeat
            |       C   Component
            |       S   Sub-Component
            |
            |   *Indexing is from 1 for compatibility with HL7 spec numbering.*

        Example:

            |   PID.F1.R1.C2.S2 or PID.1.1.2.2
            |
            |   PID (default to first PID segment, counting from 1)
            |   F1  (first after segment id, HL7 Spec numbering)
            |   R1  (repeat counting from 1)
            |   C2  (component 2 counting from 1)
            |   S2  (component 2 counting from 1)
        """

        def parse_part(keyparts, index, prefix):
            if len(keyparts) > index:
                num = keyparts[index]
                if num[0].upper() == prefix:
                    num = num[1:]
                return int(num)
            else:
                return None

        parts = key.split(".")
        segment = parts[0][:3]
        if len(parts[0]) > 3:
            segment_num = int(parts[0][3:])
        else:
            segment_num = 1
        field_num = parse_part(parts, 1, "F")
        repeat_num = parse_part(parts, 2, "R")
        component_num = parse_part(parts, 3, "C")
        subcomponent_num = parse_part(parts, 4, "S")
        return cls(
            segment, segment_num, field_num, repeat_num, component_num, subcomponent_num
        )